IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

[JTree] comment ajouter une branche dans un arbre quand on click sur un bouton


Sujet :

Composants Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Août 2007
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 2
    Points : 2
    Points
    2
    Par défaut [JTree] comment ajouter une branche dans un arbre quand on click sur un bouton
    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.

    Merci de votre aide dela fait 24 h que je suis dessus mais en vein....


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    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();
     
     			}
     		});
     	}
     }

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 852
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 852
    Points : 22 869
    Points
    22 869
    Billets dans le blog
    51
    Par défaut
    La classe DefaultTreeModel dispose des methodes insertNodeInto() et removeNodeFromParent(). Il faut apprendre a chercher dans la Javadoc !

Discussions similaires

  1. [D7] Comment ajouter une image dans du RTF ?
    Par Lung dans le forum Delphi
    Réponses: 21
    Dernier message: 02/10/2006, 16h59
  2. [VB.NET] Comment ajouter une table dans une base Access ?
    Par Hakki dans le forum Accès aux données
    Réponses: 1
    Dernier message: 22/09/2006, 16h19
  3. Comment ajouter une image dans une balise div ??
    Par liv dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 05/05/2006, 11h48
  4. comment ajouter une image dans mon HTML??
    Par Mickey.jet dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 22/03/2006, 15h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo