1 /* TreeDemo.java -- Demostrates JTree
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.swing;
41 import java.awt.BorderLayout;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
45 import javax.swing.ButtonGroup;
46 import javax.swing.JButton;
47 import javax.swing.JCheckBox;
48 import javax.swing.JComponent;
49 import javax.swing.JFrame;
50 import javax.swing.JLabel;
51 import javax.swing.JPanel;
52 import javax.swing.JRadioButton;
53 import javax.swing.JScrollPane;
54 import javax.swing.JTree;
55 import javax.swing.SwingUtilities;
56 import javax.swing.event.TreeSelectionEvent;
57 import javax.swing.event.TreeSelectionListener;
58 import javax.swing.tree.DefaultMutableTreeNode;
59 import javax.swing.tree.DefaultTreeModel;
60 import javax.swing.tree.DefaultTreeSelectionModel;
61 import javax.swing.tree.TreePath;
65 implements ActionListener
74 private void createContent()
77 DefaultMutableTreeNode root = new DefaultMutableTreeNode("Exotic Subsistence");
78 DefaultMutableTreeNode fruit = new DefaultMutableTreeNode("Interesting Fruit");
79 DefaultMutableTreeNode veg = new DefaultMutableTreeNode("Extraordinary Vegetables");
80 DefaultMutableTreeNode liq = new DefaultMutableTreeNode("Peculiar Liquids");
83 DefaultMutableTreeNode f1 = new DefaultMutableTreeNode("Abiu");
84 DefaultMutableTreeNode f2 = new DefaultMutableTreeNode("Bamboo Shoots");
85 DefaultMutableTreeNode f3 = new DefaultMutableTreeNode("Breadfruit");
86 DefaultMutableTreeNode f4 = new DefaultMutableTreeNode("Canistel");
87 DefaultMutableTreeNode f5 = new DefaultMutableTreeNode("Duku");
88 DefaultMutableTreeNode f6 = new DefaultMutableTreeNode("Guava");
89 DefaultMutableTreeNode f7 = new DefaultMutableTreeNode("Jakfruit");
90 DefaultMutableTreeNode f8 = new DefaultMutableTreeNode("Quaribea");
92 DefaultMutableTreeNode v1 = new DefaultMutableTreeNode("Amaranth");
93 DefaultMutableTreeNode v2 = new DefaultMutableTreeNode("Kiwano");
94 DefaultMutableTreeNode v3 = new DefaultMutableTreeNode("Leeks");
95 DefaultMutableTreeNode v4 = new DefaultMutableTreeNode("Luffa");
96 DefaultMutableTreeNode v5 = new DefaultMutableTreeNode("Chayote");
97 DefaultMutableTreeNode v6 = new DefaultMutableTreeNode("Jicama");
98 DefaultMutableTreeNode v7 = new DefaultMutableTreeNode("Okra");
100 DefaultMutableTreeNode l1 = new DefaultMutableTreeNode("Alcoholic");
101 DefaultMutableTreeNode l11 = new DefaultMutableTreeNode("Caipirinha");
102 DefaultMutableTreeNode l21 = new DefaultMutableTreeNode("Mojito");
103 DefaultMutableTreeNode l31 = new DefaultMutableTreeNode("Margarita");
104 DefaultMutableTreeNode l41 = new DefaultMutableTreeNode("Martini");
105 DefaultMutableTreeNode l5 = new DefaultMutableTreeNode("Non Alcoholic");
106 DefaultMutableTreeNode l55 = new DefaultMutableTreeNode("Babaji");
107 DefaultMutableTreeNode l65 = new DefaultMutableTreeNode("Chikita");
136 final JTree tree = new JTree(root);
137 tree.setLargeModel(true);
138 tree.setEditable(true);
139 final DefaultTreeSelectionModel selModel = new DefaultTreeSelectionModel();
140 selModel.setSelectionMode(
141 DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
142 tree.setSelectionModel(selModel);
144 // buttons to add and delete
145 JButton add = new JButton("add element");
146 add.addActionListener(new ActionListener()
148 public void actionPerformed(ActionEvent e)
150 for (int i = 0; i < tree.getRowCount(); i++)
152 if (tree.isRowSelected(i))
154 TreePath p = tree.getPathForRow(i);
155 DefaultMutableTreeNode n = (DefaultMutableTreeNode) p.
156 getLastPathComponent();
157 n.add(new DefaultMutableTreeNode("New Element"));
159 // The expansion state of the parent node does not change
160 // by default. We will expand it manually, to ensure that the
161 // added node is immediately visible.
164 // Refresh the tree (.repaint would be not enough both in
165 // Classpath and Sun implementations).
166 DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
174 // Demonstration of the various selection modes
175 final JCheckBox cbSingle = new JCheckBox("single selection");
176 cbSingle.addActionListener(new ActionListener()
178 public void actionPerformed(ActionEvent e)
180 if (cbSingle.isSelected())
181 selModel.setSelectionMode(
182 DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
184 selModel.setSelectionMode(
185 DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
189 // Demonstration of the root visibility changes
190 final JCheckBox cbRoot = new JCheckBox("root");
191 cbRoot.addActionListener(new ActionListener()
193 public void actionPerformed(ActionEvent e)
195 tree.setRootVisible(cbRoot.isSelected());
198 cbRoot.setSelected(true);
200 // Demonstration of the tree selection listener.
201 final JLabel choice = new JLabel("Make a choice");
202 tree.getSelectionModel().addTreeSelectionListener(
203 new TreeSelectionListener()
205 public void valueChanged(TreeSelectionEvent event)
207 TreePath was = event.getOldLeadSelectionPath();
208 TreePath now = event.getNewLeadSelectionPath();
210 was == null ? "none":was.getLastPathComponent().toString();
212 now == null ? "none":now.getLastPathComponent().toString();
213 choice.setText("From "+swas+" to "+snow);
218 setLayout(new BorderLayout());
220 JPanel p2 = new JPanel();
225 tree.getSelectionModel().
226 setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
228 // Panel for selecting line style.
229 ActionListener l = new ActionListener()
231 public void actionPerformed(ActionEvent e)
233 JRadioButton b = (JRadioButton) e.getSource();
234 tree.putClientProperty("JTree.lineStyle", b.getText());
238 JPanel lineStylePanel = new JPanel();
239 ButtonGroup buttons = new ButtonGroup();
240 lineStylePanel.add(new JLabel("Line style: "));
241 JRadioButton none = new JRadioButton("None");
242 lineStylePanel.add(none);
244 none.addActionListener(l);
245 JRadioButton angled = new JRadioButton("Angled");
246 lineStylePanel.add(angled);
248 angled.addActionListener(l);
249 JRadioButton horizontal = new JRadioButton("Horizontal");
250 lineStylePanel.add(horizontal);
251 buttons.add(horizontal);
252 horizontal.addActionListener(l);
253 p2.add(lineStylePanel);
255 add(p2, BorderLayout.NORTH);
257 add(new JScrollPane(tree), BorderLayout.CENTER);
258 add(choice, BorderLayout.SOUTH);
261 public void actionPerformed(ActionEvent e)
263 if (e.getActionCommand().equals("CLOSE"))
270 * When the demo is run independently, the frame is displayed, so we should
271 * initialise the content panel (including the demo content and a close
272 * button). But when the demo is run as part of the Swing activity board,
273 * only the demo content panel is used, the frame itself is never displayed,
274 * so we can avoid this step.
276 void initFrameContent()
278 JPanel closePanel = new JPanel();
279 JButton closeButton = new JButton("Close");
280 closeButton.setActionCommand("CLOSE");
281 closeButton.addActionListener(this);
282 closePanel.add(closeButton);
283 add(closePanel, BorderLayout.SOUTH);
286 public static void main(String[] args)
288 SwingUtilities.invokeLater
293 TreeDemo app = new TreeDemo();
294 app.initFrameContent();
295 JFrame frame = new JFrame("Tree Demo");
296 frame.getContentPane().add(app);
298 frame.setVisible(true);
304 * Returns a DemoFactory that creates a TreeDemo.
306 * @return a DemoFactory that creates a TreeDemo
308 public static DemoFactory createDemoFactory()
310 return new DemoFactory()
312 public JComponent createDemo()
314 return new TreeDemo();