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 :

Drag And Drop Jtree


Sujet :

Composants Java

  1. #1
    Candidat au Club
    Inscrit en
    Avril 2007
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Drag And Drop Jtree
    Bonjour tout le monde,
    Je cherche depuis longtemps à programmer un drag and drop sur les noeuds d'un Jtree. J'ai trouvé des exemples de code mais je n'ai absolument rien compris. Je connais le drag and drop sur les composants classiques (JTextField), je connais donc les classes du genre TransderHandler, Transferable...
    Est-ce que quelqu'un aurait un petit bout de code commenté de dnd entre un Jtree et un autre composant simple (JtextField p.ex) pour que je puisse voir quelles sont toutes les classes à utiliser et à quoi elles servent ? Ou si quelqu'un a un cours ou peut simplement me faire un rapide résumé des classes à utiliser et leurs fonctionnements, je le remercie d'avance.

    MKL

  2. #2
    Membre régulier
    Inscrit en
    Février 2004
    Messages
    90
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 90
    Points : 78
    Points
    78
    Par défaut
    Ciao essai cela
    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
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.tree.*;
    import java.io.*;
    //import javax.swing.table.*;
     
    public class BasicDnD extends JPanel implements ActionListener, MouseListener {
        static JFrame frame;
        JTextArea textArea;
        JTextField textField;
        JList list;
        //JTable table;
        JTree tree;
        JCheckBox toggleDnD;
        JLabel label;
     
        public BasicDnD() {
            super(new BorderLayout());
            JPanel rightPanel = new JPanel();
            rightPanel.setLayout(new GridLayout(2,2));
            //Create a table model.
            //LEFT COLUMN
            //Use the table model to create a table.
     
            //Create a color chooser.
     
            //RIGHT COLUMN
            //Create a textfield.
            textField = new JTextField(30);
             textField.addActionListener(this);
    textField.addMouseListener(this);
            textField.setText("Favorite foods:\nPizza, Moussaka, Pot roast");
            rightPanel.add(createPanelForComponent(textField, "JTextField"));
     
            //Create a scrolled text area.
            textArea = new JTextArea(5, 30);
            textArea.setText("Favorite shows:\nBuffy, Alias, Angel");
            //textArea.addActionListener(this);
            textArea.addMouseListener(this);
            JScrollPane scrollPane = new JScrollPane(textArea);
            rightPanel.add(createPanelForComponent(scrollPane, "JTextArea"));
            //Creat a label
     
            //Create a list model and a list.
     
            DefaultListModel listModel = new DefaultListModel();
            listModel.addElement("Martha Washington");
            listModel.addElement("Abigail Adams");
            listModel.addElement("Martha Randolph");
            listModel.addElement("Dolley Madison");
            listModel.addElement("Elizabeth Monroe");
            listModel.addElement("Louisa Adams");
     
            listModel.addElement("Emily Donelson");
     
            list = new JList(listModel);
            list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            JScrollPane listView = new JScrollPane(list);
            listView.setPreferredSize(new Dimension(300, 100));
            rightPanel.add(createPanelForComponent(listView, "JList"));
     
            //Create a tree.
           DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Mia Familia");
            DefaultMutableTreeNode sharon = new DefaultMutableTreeNode("Sharon");
            rootNode.add(sharon);
            DefaultMutableTreeNode maya = new DefaultMutableTreeNode("Maya");
            sharon.add(maya);
     
                DefaultMutableTreeNode anya = new DefaultMutableTreeNode("Anya");
            sharon.add(anya);
            sharon.add(new DefaultMutableTreeNode("Bongo"));
            maya.add(new DefaultMutableTreeNode("Muffin"));
            anya.add(new DefaultMutableTreeNode("Winky"));
            DefaultTreeModel model = new DefaultTreeModel(rootNode);
            tree = new JTree(model);
            //tree.addActionListener(this);
            tree.addMouseListener(this);
            tree.getSelectionModel().setSelectionMode
                  (TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
            JScrollPane treeView = new JScrollPane(tree);
            treeView.setPreferredSize(new Dimension(300, 100));
            rightPanel.add(createPanelForComponent(treeView, "JTree"));
     
            //Create the toggle button.
            toggleDnD = new JCheckBox("Turn on Drag and Drop");
            toggleDnD.setActionCommand("toggleDnD");
            toggleDnD.addActionListener(this);
     
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                                null, rightPanel);
            //splitPane.setOneTouchExpandable(true);
     
            add(splitPane, BorderLayout.CENTER);
            add(toggleDnD, BorderLayout.PAGE_END);
            setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        }
     
        protected JPanel createVerticalBoxPanel() {
            JPanel p = new JPanel();
            p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
            p.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
            return p;
        }
        public JPanel createPanelForComponent(JComponent comp,
                                              String title) {
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(comp, BorderLayout.CENTER);
            if (title != null) {
                panel.setBorder(BorderFactory.createTitledBorder(title));
            }
            return panel;
        }
     
        public void actionPerformed(ActionEvent e) {
            System.out.println("chiamata a actionPerformed");
            if ("toggleDnD".equals(e.getActionCommand())) {
                boolean toggle = toggleDnD.isSelected();
                //boolean toggle = true;
                textArea.setDragEnabled(toggle);
                //label.setDragEnabled(toggle);
                textField.setDragEnabled(toggle);
                list.setDragEnabled(toggle);
                //table.setDragEnabled(toggle);
                tree.setDragEnabled(toggle);
            }
        }
     
           public void mouseEntered(MouseEvent e) {
           System.out.println("chiamata a mouseEntered");
           if(e.getSource() instanceof JTree) {
           System.out.println("Entro in componentei Jtree");
           }
           /*else if(e.getSource() instanceof JCheckBox) {
           System.out.println("Entro in componentei JCheckBox");
           }*/
           else if(e.getSource() instanceof JTextArea) {
           System.out.println("Entro in componentei JTextArea");
           }
           }
           public void mouseExited(MouseEvent e) {
           System.out.println("chiamata a mouseExite");
           if(e.getSource() instanceof JTree) {
            System.out.println("Esco da un componente Jtree");
           }
           /*else if(e.getSource() instanceof JCheckBox) {
           System.out.println("Esco da un componente JCheckBox");
           }*/
           else if(e.getSource() instanceof JTextArea) {
           System.out.println("Esco da un componente JTextArea");
           }
           }
           public void mousePressed(MouseEvent e) {
           System.out.println("chiamata a mousePressed");
           if(e.getSource() instanceof JTree) {
           System.out.println("Presso un componente Jtree");
           }
           /*else if(e.getSource() instanceof JCheckBox) {
           System.out.println("Presso un componente JCheckBox");
           }*/
           else if(e.getSource() instanceof JTextArea) {
           System.out.println("Presso un componente JTextArea");
           }
           }
           public void mouseReleased(MouseEvent e) {
           System.out.println("chiamata a mouseReleased");
           if(e.getSource() instanceof JTree) {
           System.out.println("Relese un  componente Jtree");
           }
           /*else if(e.getSource() instanceof JCheckBox) {
           System.out.println("Relese un  componente JCheckBox");
           }*/
           else if(e.getSource() instanceof JTextArea) {
           System.out.println("Relese un  componente JTextArea");
           }
           }
           public void mouseClicked(MouseEvent e) {
           System.out.println("chiamata a mouseClicked");
           if(e.getSource() instanceof JTree) {
           System.out.println("Click un  componente Jtree");
           }
           else if("JTextArea".equals(e.getSource())) {
           System.out.println("Click un  componente JTextArea");
           }
     
           }
     private static void createAndShowGUI() {
            //Create and set up the window.
           frame = new JFrame("########## Prova Drop & Drag ##########");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            JComponent newContentPane = new BasicDnD();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
     
            //Display the window.
            //frame.setSize(700,700);
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                    createAndShowGUI();
                    }
                    });
        }
    }

  3. #3
    Candidat au Club
    Inscrit en
    Avril 2007
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 3
    Points : 2
    Points
    2
    Par défaut
    Super, merci beaucoup !
    Je le lit et je dissi il y a quelquechose que je ne comprend pas

  4. #4
    Candidat au Club
    Inscrit en
    Avril 2007
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 3
    Points : 2
    Points
    2
    Par défaut Besoin de plus
    En fait, ce que tu m'as donné était bien mais pas assez précis. Le programme de contentait de de faire : "monJtree.setDragEnable(true) ; " . Je cherche à savoir comment on attribut un transferHandler à chaque noeud de l'arbre.

Discussions similaires

  1. Drag and drop jtree
    Par jose.hello dans le forum Composants
    Réponses: 6
    Dernier message: 28/05/2010, 11h30
  2. Drag and Drop Jtree sous jdk 1.5
    Par iRonny dans le forum Composants
    Réponses: 3
    Dernier message: 01/04/2009, 11h10
  3. Drag and Drop Jtree
    Par reno_tidus dans le forum Composants
    Réponses: 14
    Dernier message: 26/09/2007, 10h17
  4. [JTree] Drag and drop
    Par tomca dans le forum Composants
    Réponses: 7
    Dernier message: 01/12/2005, 09h03
  5. Drag and drop sur un JTree
    Par tomca dans le forum Composants
    Réponses: 4
    Dernier message: 02/08/2005, 10h54

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