Swing JTree的示例
转自Oracle官网上的Swing Tutorial:
1 package components; 2 3 /** 4 * This application that requires the following additional files: 5 * TreeDemoHelp.html 6 * arnold.html 7 * bloch.html 8 * chan.html 9 * jls.html 10 * swingtutorial.html 11 * tutorial.html 12 * tutorialcont.html 13 * vm.html 14 */ 15 import javax.swing.JEditorPane; 16 import javax.swing.JFrame; 17 import javax.swing.JPanel; 18 import javax.swing.JScrollPane; 19 import javax.swing.JSplitPane; 20 import javax.swing.UIManager; 21 22 import javax.swing.JTree; 23 import javax.swing.tree.DefaultMutableTreeNode; 24 import javax.swing.tree.TreeSelectionModel; 25 import javax.swing.event.TreeSelectionEvent; 26 import javax.swing.event.TreeSelectionListener; 27 28 import java.net.URL; 29 import java.io.IOException; 30 import java.awt.Dimension; 31 import java.awt.GridLayout; 32 33 public class TreeDemo extends JPanel 34 implements TreeSelectionListener { 35 private JEditorPane htmlPane; 36 private JTree tree; 37 private URL helpURL; 38 private static boolean DEBUG = false; 39 40 //Optionally play with line styles. Possible values are 41 //"Angled" (the default), "Horizontal", and "None". 42 private static boolean playWithLineStyle = false; 43 private static String lineStyle = "Horizontal"; 44 45 //Optionally set the look and feel. 46 private static boolean useSystemLookAndFeel = false; 47 48 public TreeDemo() { 49 super(new GridLayout(1,0)); 50 51 //Create the nodes. 52 DefaultMutableTreeNode top = 53 new DefaultMutableTreeNode("The Java Series"); 54 createNodes(top); 55 56 //Create a tree that allows one selection at a time. 57 tree = new JTree(top); 58 tree.getSelectionModel().setSelectionMode 59 (TreeSelectionModel.SINGLE_TREE_SELECTION); 60 61 //Listen for when the selection changes. 62 tree.addTreeSelectionListener(this); 63 64 if (playWithLineStyle) { 65 System.out.println("line style = " + lineStyle); 66 tree.putClientProperty("JTree.lineStyle", lineStyle); 67 } 68 69 //Create the scroll pane and add the tree to it. 70 JScrollPane treeView = new JScrollPane(tree); 71 72 //Create the HTML viewing pane. 73 htmlPane = new JEditorPane(); 74 htmlPane.setEditable(false); 75 initHelp(); 76 JScrollPane htmlView = new JScrollPane(htmlPane); 77 78 //Add the scroll panes to a split pane. 79 JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 80 splitPane.setTopComponent(treeView); 81 splitPane.setBottomComponent(htmlView); 82 83 Dimension minimumSize = new Dimension(100, 50); 84 htmlView.setMinimumSize(minimumSize); 85 treeView.setMinimumSize(minimumSize); 86 splitPane.setDividerLocation(100); 87 splitPane.setPreferredSize(new Dimension(500, 300)); 88 89 //Add the split pane to this panel. 90 add(splitPane); 91 } 92 93 /** Required by TreeSelectionListener interface. */ 94 public void valueChanged(TreeSelectionEvent e) { 95 DefaultMutableTreeNode node = (DefaultMutableTreeNode) 96 tree.getLastSelectedPathComponent(); 97 98 if (node == null) return; 99 100 Object nodeInfo = node.getUserObject(); 101 if (node.isLeaf()) { 102 BookInfo book = (BookInfo)nodeInfo; 103 displayURL(book.bookURL); 104 if (DEBUG) { 105 System.out.print(book.bookURL + ": \n "); 106 } 107 } else { 108 displayURL(helpURL); 109 } 110 if (DEBUG) { 111 System.out.println(nodeInfo.toString()); 112 } 113 } 114 115 private class BookInfo { 116 public String bookName; 117 public URL bookURL; 118 119 public BookInfo(String book, String filename) { 120 bookName = book; 121 bookURL = getClass().getResource(filename); 122 if (bookURL == null) { 123 System.err.println("Couldn't find file: " 124 + filename); 125 } 126 } 127 128 public String toString() { 129 return bookName; 130 } 131 } 132 133 private void initHelp() { 134 String s = "TreeDemoHelp.html"; 135 helpURL = getClass().getResource(s); 136 if (helpURL == null) { 137 System.err.println("Couldn't open help file: " + s); 138 } else if (DEBUG) { 139 System.out.println("Help URL is " + helpURL); 140 } 141 142 displayURL(helpURL); 143 } 144 145 private void displayURL(URL url) { 146 try { 147 if (url != null) { 148 htmlPane.setPage(url); 149 } else { //null url 150 htmlPane.setText("File Not Found"); 151 if (DEBUG) { 152 System.out.println("Attempted to display a null URL."); 153 } 154 } 155 } catch (IOException e) { 156 System.err.println("Attempted to read a bad URL: " + url); 157 } 158 } 159 160 private void createNodes(DefaultMutableTreeNode top) { 161 DefaultMutableTreeNode category = null; 162 DefaultMutableTreeNode book = null; 163 164 category = new DefaultMutableTreeNode("Books for Java Programmers"); 165 top.add(category); 166 167 //original Tutorial 168 book = new DefaultMutableTreeNode(new BookInfo 169 ("The Java Tutorial: A Short Course on the Basics", 170 "tutorial.html")); 171 category.add(book); 172 173 //Tutorial Continued 174 book = new DefaultMutableTreeNode(new BookInfo 175 ("The Java Tutorial Continued: The Rest of the JDK", 176 "tutorialcont.html")); 177 category.add(book); 178 179 //JFC Swing Tutorial 180 book = new DefaultMutableTreeNode(new BookInfo 181 ("The JFC Swing Tutorial: A Guide to Constructing GUIs", 182 "swingtutorial.html")); 183 category.add(book); 184 185 //Bloch 186 book = new DefaultMutableTreeNode(new BookInfo 187 ("Effective Java Programming Language Guide", 188 "bloch.html")); 189 category.add(book); 190 191 //Arnold/Gosling 192 book = new DefaultMutableTreeNode(new BookInfo 193 ("The Java Programming Language", "arnold.html")); 194 category.add(book); 195 196 //Chan 197 book = new DefaultMutableTreeNode(new BookInfo 198 ("The Java Developers Almanac", 199 "chan.html")); 200 category.add(book); 201 202 category = new DefaultMutableTreeNode("Books for Java Implementers"); 203 top.add(category); 204 205 //VM 206 book = new DefaultMutableTreeNode(new BookInfo 207 ("The Java Virtual Machine Specification", 208 "vm.html")); 209 category.add(book); 210 211 //Language Spec 212 book = new DefaultMutableTreeNode(new BookInfo 213 ("The Java Language Specification", 214 "jls.html")); 215 category.add(book); 216 } 217 218 /** 219 * Create the GUI and show it. For thread safety, 220 * this method should be invoked from the 221 * event dispatch thread. 222 */ 223 private static void createAndShowGUI() { 224 if (useSystemLookAndFeel) { 225 try { 226 UIManager.setLookAndFeel( 227 UIManager.getSystemLookAndFeelClassName()); 228 } catch (Exception e) { 229 System.err.println("Couldn't use system look and feel."); 230 } 231 } 232 233 //Create and set up the window. 234 JFrame frame = new JFrame("TreeDemo"); 235 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 236 237 //Add content to the window. 238 frame.add(new TreeDemo()); 239 240 //Display the window. 241 frame.pack(); 242 frame.setVisible(true); 243 } 244 245 public static void main(String[] args) { 246 //Schedule a job for the event dispatch thread: 247 //creating and showing this application's GUI. 248 javax.swing.SwingUtilities.invokeLater(new Runnable() { 249 public void run() { 250 createAndShowGUI(); 251 } 252 }); 253 } 254 }