This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
swing:jtree_demo_01 [2015/05/17 16:54] gthanos created |
swing:jtree_demo_01 [2015/05/18 02:44] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | <code java TreeDemo.java> | ||
| - | package components; | ||
| - | /** | ||
| - | * This application that requires the following additional files: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | */ | ||
| - | import javax.swing.JEditorPane; | ||
| - | import javax.swing.JFrame; | ||
| - | import javax.swing.JPanel; | ||
| - | import javax.swing.JScrollPane; | ||
| - | import javax.swing.JSplitPane; | ||
| - | import javax.swing.UIManager; | ||
| - | import javax.swing.JTree; | ||
| - | import javax.swing.tree.DefaultMutableTreeNode; | ||
| - | import javax.swing.tree.TreeSelectionModel; | ||
| - | import javax.swing.event.TreeSelectionEvent; | ||
| - | import javax.swing.event.TreeSelectionListener; | ||
| - | import javax.swing.event.*; | ||
| - | |||
| - | import java.net.URL; | ||
| - | import java.io.IOException; | ||
| - | import java.awt.Dimension; | ||
| - | import java.awt.GridLayout; | ||
| - | |||
| - | public class TreeDemo extends JPanel | ||
| - | implements TreeSelectionListener, | ||
| - | private JEditorPane htmlPane; | ||
| - | private JTree tree; | ||
| - | private URL helpURL; | ||
| - | private static boolean DEBUG = false; | ||
| - | |||
| - | // | ||
| - | //" | ||
| - | private static boolean playWithLineStyle = false; | ||
| - | private static String lineStyle = " | ||
| - | | ||
| - | // | ||
| - | private static boolean useSystemLookAndFeel = false; | ||
| - | |||
| - | public TreeDemo() { | ||
| - | super(new GridLayout(1, | ||
| - | |||
| - | //Create the nodes. | ||
| - | DefaultMutableTreeNode top = | ||
| - | new DefaultMutableTreeNode(" | ||
| - | createNodes(top); | ||
| - | |||
| - | //Create a tree that allows one selection at a time. | ||
| - | tree = new JTree(top); | ||
| - | tree.setEditable(true); | ||
| - | tree.getSelectionModel().setSelectionMode | ||
| - | (TreeSelectionModel.SINGLE_TREE_SELECTION); | ||
| - | |||
| - | //Listen for when the selection changes. | ||
| - | tree.addTreeSelectionListener(this); | ||
| - | |||
| - | if (playWithLineStyle) { | ||
| - | System.out.println(" | ||
| - | tree.putClientProperty(" | ||
| - | } | ||
| - | |||
| - | //Create the scroll pane and add the tree to it. | ||
| - | JScrollPane treeView = new JScrollPane(tree); | ||
| - | |||
| - | //Create the HTML viewing pane. | ||
| - | htmlPane = new JEditorPane(); | ||
| - | htmlPane.setEditable(false); | ||
| - | initHelp(); | ||
| - | JScrollPane htmlView = new JScrollPane(htmlPane); | ||
| - | |||
| - | //Add the scroll panes to a split pane. | ||
| - | JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); | ||
| - | splitPane.setTopComponent(treeView); | ||
| - | splitPane.setBottomComponent(htmlView); | ||
| - | |||
| - | Dimension minimumSize = new Dimension(100, | ||
| - | htmlView.setMinimumSize(minimumSize); | ||
| - | treeView.setMinimumSize(minimumSize); | ||
| - | splitPane.setDividerLocation(100); | ||
| - | splitPane.setPreferredSize(new Dimension(500, | ||
| - | |||
| - | //Add the split pane to this panel. | ||
| - | add(splitPane); | ||
| - | } | ||
| - | |||
| - | /** Required by TreeSelectionListener interface. */ | ||
| - | public void valueChanged(TreeSelectionEvent e) { | ||
| - | DefaultMutableTreeNode node = (DefaultMutableTreeNode) | ||
| - | | ||
| - | |||
| - | if (node == null) return; | ||
| - | |||
| - | Object nodeInfo = node.getUserObject(); | ||
| - | if (node.isLeaf()) { | ||
| - | BookInfo book = (BookInfo)nodeInfo; | ||
| - | displayURL(book.bookURL); | ||
| - | if (DEBUG) { | ||
| - | System.out.print(book.bookURL + ": | ||
| - | } | ||
| - | } else { | ||
| - | displayURL(helpURL); | ||
| - | } | ||
| - | if (DEBUG) { | ||
| - | System.out.println(nodeInfo.toString()); | ||
| - | } | ||
| - | } | ||
| - | | ||
| - | public void treeNodesChanged(TreeModelEvent e){ | ||
| - | System.out.println(" | ||
| - | } | ||
| - | public void treeNodesInserted(TreeModelEvent e){ | ||
| - | System.out.println(" | ||
| - | } | ||
| - | public void treeNodesRemoved(TreeModelEvent e){ | ||
| - | System.out.println(" | ||
| - | } | ||
| - | public void treeStructureChanged(TreeModelEvent e){ | ||
| - | System.out.println(" | ||
| - | } | ||
| - | |||
| - | private class BookInfo { | ||
| - | public String bookName; | ||
| - | public URL bookURL; | ||
| - | |||
| - | public BookInfo(String book, String filename) { | ||
| - | bookName = book; | ||
| - | bookURL = getClass().getResource(" | ||
| - | if (bookURL == null) { | ||
| - | System.err.println(" | ||
| - | + filename); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | public String toString() { | ||
| - | return bookName; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | private void initHelp() { | ||
| - | String s = " | ||
| - | helpURL = getClass().getResource(s); | ||
| - | if (helpURL == null) { | ||
| - | System.err.println(" | ||
| - | } else if (DEBUG) { | ||
| - | System.out.println(" | ||
| - | } | ||
| - | |||
| - | displayURL(helpURL); | ||
| - | } | ||
| - | |||
| - | private void displayURL(URL url) { | ||
| - | try { | ||
| - | if (url != null) { | ||
| - | htmlPane.setPage(url); | ||
| - | } else { //null url | ||
| - | htmlPane.setText(" | ||
| - | if (DEBUG) { | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | } catch (IOException e) { | ||
| - | System.err.println(" | ||
| - | } | ||
| - | } | ||
| - | |||
| - | private void createNodes(DefaultMutableTreeNode top) { | ||
| - | DefaultMutableTreeNode category = null; | ||
| - | DefaultMutableTreeNode book = null; | ||
| - | |||
| - | category = new DefaultMutableTreeNode(" | ||
| - | top.add(category); | ||
| - | |||
| - | //original Tutorial | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The Java Tutorial: A Short Course on the Basics", | ||
| - | " | ||
| - | category.add(book); | ||
| - | |||
| - | //Tutorial Continued | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The Java Tutorial Continued: The Rest of the JDK", | ||
| - | " | ||
| - | category.add(book); | ||
| - | |||
| - | //JFC Swing Tutorial | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The JFC Swing Tutorial: A Guide to Constructing GUIs", | ||
| - | " | ||
| - | category.add(book); | ||
| - | |||
| - | //Bloch | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | (" | ||
| - | " | ||
| - | category.add(book); | ||
| - | |||
| - | // | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The Java Programming Language", | ||
| - | category.add(book); | ||
| - | |||
| - | //Chan | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The Java Developers Almanac", | ||
| - | " | ||
| - | category.add(book); | ||
| - | |||
| - | category = new DefaultMutableTreeNode(" | ||
| - | top.add(category); | ||
| - | |||
| - | //VM | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The Java Virtual Machine Specification", | ||
| - | " | ||
| - | category.add(book); | ||
| - | |||
| - | //Language Spec | ||
| - | book = new DefaultMutableTreeNode(new BookInfo | ||
| - | ("The Java Language Specification", | ||
| - | " | ||
| - | category.add(book); | ||
| - | } | ||
| - | | ||
| - | /** | ||
| - | * Create the GUI and show it. For thread safety, | ||
| - | * this method should be invoked from the | ||
| - | * event dispatch thread. | ||
| - | */ | ||
| - | private static void createAndShowGUI() { | ||
| - | if (useSystemLookAndFeel) { | ||
| - | try { | ||
| - | UIManager.setLookAndFeel( | ||
| - | UIManager.getSystemLookAndFeelClassName()); | ||
| - | } catch (Exception e) { | ||
| - | System.err.println(" | ||
| - | } | ||
| - | } | ||
| - | |||
| - | //Create and set up the window. | ||
| - | JFrame frame = new JFrame(" | ||
| - | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| - | |||
| - | //Add content to the window. | ||
| - | frame.add(new TreeDemo()); | ||
| - | |||
| - | //Display the window. | ||
| - | frame.pack(); | ||
| - | frame.setVisible(true); | ||
| - | } | ||
| - | |||
| - | public static void main(String[] args) { | ||
| - | //Schedule a job for the event dispatch thread: | ||
| - | //creating and showing this application' | ||
| - | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
| - | public void run() { | ||
| - | createAndShowGUI(); | ||
| - | } | ||
| - | }); | ||
| - | } | ||
| - | } | ||
| - | </ | ||