bonjour mon programme actuel :
il créer un arbre dynamique et il y a un boutton pour pouvoir rajouter un noeud.
1 er probleme
Je n'arrive pas a créer le boutton supprimer et une fonction qui me permet de modifier le nom d'un noeud
2eme probleme
je ne peut que rajouter un un fils par noeud .
Merci de vos reponse
/******************************************************************************/
package test;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class Flo774Sample extends JFrame {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JTree tree;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
Flo774Sample frame = new Flo774Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public Flo774Sample() {
super();
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
DefaultMutableTreeNode root = new DefaultMutableTreeNode("arbre" );
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Flo 774" );
root.add(child1);
tree = new JTree(root);
scrollPane.setViewportView(tree);
final JPanel panel = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
panel.setLayout(flowLayout);
getContentPane().add(panel, BorderLayout.SOUTH);
textField = new JTextField();
textField.setColumns(20);
panel.add(textField);
final JButton addANodeButton = new JButton();
addANodeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(getTextField().getText());
if (getTree().getLastSelectedPathComponent() != null) {
((DefaultMutableTreeNode) getTree().getLastSelectedPathComponent()).add(node);
getTree().repaint();
}
}
});
addANodeButton.setText("Add a node" );
panel.add(addANodeButton);
//
}
protected JTree getTree() {
return tree;
}
protected JTextField getTextField() {
return textField;
}
}