2
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
8
* - Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
15
* - Neither the name of Oracle nor the names of its
16
* contributors may be used to endorse or promote products derived
17
* from this software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
import javax.swing.JScrollPane;
33
import javax.swing.JTree;
34
import javax.swing.tree.DefaultMutableTreeNode;
36
import java.awt.BorderLayout;
37
import java.awt.Insets;
38
import java.io.BufferedReader;
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.InputStreamReader;
44
import static java.nio.charset.StandardCharsets.UTF_8;
49
* @author Jeff Dinkins
51
public class TreeDemo extends DemoModule {
56
* main method allows us to run as a standalone demo.
58
public static void main(String[] args) {
59
TreeDemo demo = new TreeDemo(null);
64
* TreeDemo Constructor
66
public TreeDemo(SwingSet2 swingset) {
67
// Set the title for this demo, and an icon used to represent this
68
// demo inside the SwingSet2 app.
69
super(swingset, "TreeDemo", "toolbar/JTree.gif");
71
getDemoPanel().add(createTree(), BorderLayout.CENTER);
74
public JScrollPane createTree() {
75
DefaultMutableTreeNode top = new DefaultMutableTreeNode(getString("TreeDemo.music"));
76
DefaultMutableTreeNode category = null;
77
DefaultMutableTreeNode artist = null;
78
DefaultMutableTreeNode record = null;
81
URL url = getClass().getResource("/resources/tree.txt");
84
// convert url to buffered string
85
InputStream is = url.openStream();
86
InputStreamReader isr = new InputStreamReader(is, UTF_8);
87
BufferedReader reader = new BufferedReader(isr);
89
// read one line at a time, put into tree
90
String line = reader.readLine();
92
// System.out.println("reading in: ->" + line + "<-");
93
char linetype = line.charAt(0);
96
category = new DefaultMutableTreeNode(line.substring(2));
100
if(category != null) {
101
category.add(artist = new DefaultMutableTreeNode(line.substring(2)));
106
artist.add(record = new DefaultMutableTreeNode(line.substring(2)));
111
record.add(new DefaultMutableTreeNode(line.substring(2)));
117
line = reader.readLine();
119
} catch (IOException e) {
122
tree = new JTree(top) {
123
public Insets getInsets() {
124
return new Insets(5,5,5,5);
128
tree.setEditable(true);
130
return new JScrollPane(tree);
133
void updateDragEnabled(boolean dragEnabled) {
134
tree.setDragEnabled(dragEnabled);