bonjour chere collegue du java.
Le but de mon programme est d'avoir une interface graphique qui me permettra de creer directement un arbre dynamique.
Pour l'instant dans mon avancement j'ai seulement créer l'arbre statiquement et je n'arrive pas a rajouter un bouton sur la frame avec du code derriere pour pouvoir ajouter ou supprimer une branche à l'endroit du curseur.
package organigramme;
/* Demonstrate a tree events
*/
import java.awt.*;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
class TreeEventDemo //extends JFrame implements ActionListener
{
JLabel jlab;
TreeEventDemo()
{
// Create a new JFrame container
JFrame jfrm = new JFrame("arbre");
// Use the default border layout manager
// give the frame an initial size
jfrm.setSize(200, 200);
// terminate program when the user closes the application
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create a label that will display the tree selection
jlab = new JLabel();
/* Begin creating the tree be defining the
* structure and realtionship of its nodes*/
// First, create the root node of the ttree
DefaultMutableTreeNode root = new DefaultMutableTreeNode("racine");
/* Next, create twoo subtrees. One contains fruit.
* The other vegetables.*/
// Create the root of the Fruit subtree
DefaultMutableTreeNode fruit = new DefaultMutableTreeNode("1");
root.add(fruit); // add the Fruit node to the tree
/* The Fruit subtree has two subtrees on its own.
* The first is Apples, the second is Pears.*/
// Create an Apples subtree
DefaultMutableTreeNode apples = new DefaultMutableTreeNode("1.1");
fruit.add(apples);
/* Populate the Apples subtree by adding
* apple varieties to the Apples subtree*/
apples.add(new DefaultMutableTreeNode("1.1.1"));
apples.add(new DefaultMutableTreeNode("1.1.2"));
//Create a Pears subtree
DefaultMutableTreeNode pears = new DefaultMutableTreeNode("1.2");
fruit.add(pears);
// Populate the Pears subtree
pears.add(new DefaultMutableTreeNode("1.2.1"));
// Create the root of the Vegetables subtree
DefaultMutableTreeNode veg = new DefaultMutableTreeNode("2");
root.add(veg);
// Populate Vegetables
veg.add(new DefaultMutableTreeNode("2.1"));
veg.add(new DefaultMutableTreeNode("2.2"));
veg.add(new DefaultMutableTreeNode("2.3"));
veg.add(new DefaultMutableTreeNode("2.4"));
/* Now, create a JTree that uses the structure
* defined by the preceding statements*/
JTree jtree = new JTree(root);
// Allow the tree to be edited so that model events can be generated
jtree.setEditable(true);
//Set the tree selection mode to single selection
TreeSelectionModel tsm = jtree.getSelectionModel();
tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
// Finally, wrap the tree in scroll pane
JScrollPane jscrlp = new JScrollPane(jtree);
// Listen for tree expansion events
jtree.addTreeExpansionListener(new TreeExpansionListener()
{
public void treeExpanded(TreeExpansionEvent tse)
{
// Get the path to the expansion point
TreePath tp = tse.getPath();
// Display the node
jlab.setText("Expansion: " + tp.getLastPathComponent() );
}
public void treeCollapsed(TreeExpansionEvent tse)
{
// Get the path to the expansion point
TreePath tp = tse.getPath();
// Display the node
jlab.setText("Collapse: " + tp.getLastPathComponent() );
}
});
// Listen for tree selection events
jtree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent tse)
{
// Get the path to the selection
TreePath tp = tse.getPath();
//Display the selected node
jlab.setText("Selection event: " +tp.getLastPathComponent() );
}
});
// Listen for tree model events.Notice that the listener is registered with tree model
jtree.getModel().addTreeModelListener(new TreeModelListener()
{
public void treeNodesChanged(TreeModelEvent tse)
{
// Get the path to the change
TreePath tp = tse.getTreePath();
// Display the Path
jlab.setText("Model change path: " + tp);
}
/* Empty implementations of the remaining TreeModelEvent
* methods. Implement these if your application
* needs to handle these actions*/
public void treeNodesInserted(TreeModelEvent tse){}
public void treeNodesRemoved(TreeModelEvent tse){}
public void treeStructureChanged(TreeModelEvent tse){}
});
// Add the tree and label to the content pane
jfrm.getContentPane().add(jscrlp, BorderLayout.CENTER);
jfrm.getContentPane().add(jlab, BorderLayout.SOUTH);
// Display the frame
jfrm.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TreeEventDemo();
}
});
}
}
