Example usage for javax.swing.event TreeSelectionListener TreeSelectionListener

List of usage examples for javax.swing.event TreeSelectionListener TreeSelectionListener

Introduction

In this page you can find the example usage for javax.swing.event TreeSelectionListener TreeSelectionListener.

Prototype

TreeSelectionListener

Source Link

Usage

From source file:ComponentTree.java

/**
 * This main() method demonstrates the use of the ComponentTree class: it
 * puts a ComponentTree component in a Frame, and uses the ComponentTree to
 * display its own GUI hierarchy. It also adds a TreeSelectionListener to
 * display additional information about each component as it is selected
 *//*  w ww  .j  av  a2  s .c o  m*/
public static void main(String[] args) {
    // Create a frame for the demo, and handle window close requests
    JFrame frame = new JFrame("ComponentTree Demo");
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Create a scroll pane and a "message line" and add them to the
    // center and bottom of the frame.
    JScrollPane scrollpane = new JScrollPane();
    final JLabel msgline = new JLabel(" ");
    frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
    frame.getContentPane().add(msgline, BorderLayout.SOUTH);

    // Now create the ComponentTree object, specifying the frame as the
    // component whose tree is to be displayed. Also set the tree's font.
    JTree tree = new ComponentTree(frame);
    tree.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Only allow a single item in the tree to be selected at once
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Add an event listener for notifications when
    // the tree selection state changes.
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            // Tree selections are referred to by "path"
            // We only care about the last node in the path
            TreePath path = e.getPath();
            Component c = (Component) path.getLastPathComponent();
            // Now we know what component was selected, so
            // display some information about it in the message line
            if (c.isShowing()) {
                Point p = c.getLocationOnScreen();
                msgline.setText("x: " + p.x + "  y: " + p.y + "  width: " + c.getWidth() + "  height: "
                        + c.getHeight());
            } else {
                msgline.setText("component is not showing");
            }
        }
    });

    // Now that we've set up the tree, add it to the scrollpane
    scrollpane.setViewportView(tree);

    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:MainClass.java

public MainClass(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, dir));

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }//  w  w  w .j  a  v a 2  s  .c  om
    });

    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(BorderLayout.CENTER, scrollpane);
}

From source file:Main.java

public Main(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, projectFile));
    tree.setCellRenderer(new MyTreeCellRenderer());
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }/*from  ww w.j  a  v a2s  .com*/
    });

    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(scrollpane, BorderLayout.CENTER);
}

From source file:FileTreeFrame.java

public FileTreeFrame(String directory) {
    super("JTree FileSystem Viewer");
    fileDetailsTextArea.setEditable(false);
    fileSystemModel = new FileSystemModel(new File(directory));
    fileTree = new JTree(fileSystemModel);
    fileTree.setEditable(true);/* w w w.jav  a 2 s.com*/
    fileTree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent event) {
            File file = (File) fileTree.getLastSelectedPathComponent();
            fileDetailsTextArea.setText(getFileDetails(file));
        }
    });
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(fileTree),
            new JScrollPane(fileDetailsTextArea));
    getContentPane().add(splitPane);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(640, 480);
    setVisible(true);
}

From source file:de.quadrillenschule.azocamsyncd.gui.ExploreWifiSDPanel.java

/**
 * Creates new form ExploreWifiSDPanel/*from  ww  w  . j a va  2s.  c om*/
 */
public ExploreWifiSDPanel() {
    initComponents();
    rootNode = new DefaultMutableTreeNode("/");
    DefaultTreeModel dtm = new DefaultTreeModel(rootNode);
    remotejTree.setModel(dtm);
    remotejTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            updateSingleView();
        }
    });
    remotejTree.setCellRenderer(new TreeCellRenderer() {

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            JLabel retval = new JLabel(value.toString());
            AZoFTPFile myaffile = null;
            if (afs == null) {
                return retval;
            }
            for (AZoFTPFile af : afs) {
                if (new String(af.dir + af.ftpFile.getName()).equals(value.toString())) {
                    myaffile = af;
                    break;
                }
            }
            try {
                if (!myaffile.ftpFile.isDirectory()) {
                    if (!localStorage.getLocalFile(myaffile).exists()) {

                        if (!localStorage.isFileSynced(myaffile)) {
                            retval.setForeground(new Color(0, 20, 100));
                        }
                    } else {
                        retval.setForeground(new Color(20, 100, 0));
                    }
                }
            } catch (Exception ex) {
                //           Logger.getLogger(ExploreWifiSDPanel.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (selected) {
                retval.setOpaque(true);
                retval.setBackground(Color.darkGray);
            }
            return retval;
        }
    });
}

From source file:TreeIt.java

public TreeIt() {
    JFrame f = new JFrame();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
    DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
    root.add(months);/*w ww . j av  a2 s . c  o  m*/
    String monthLabels[] = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };
    for (int i = 0, n = monthLabels.length; i < n; i++)
        months.add(new DefaultMutableTreeNode(monthLabels[i]));
    DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
    root.add(weeks);
    String weekLabels[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    for (int i = 0, n = weekLabels.length; i < n; i++)
        weeks.add(new DefaultMutableTreeNode(weekLabels[i]));
    JTree jt = new JTree(root);
    jt.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getPath();
            System.out.println("Picked: " + path.getLastPathComponent());
            Object elements[] = path.getPath();
            for (int i = 0, n = elements.length; i < n; i++) {
                System.out.print("->" + elements[i]);
            }
            System.out.println();
        }
    });

    DefaultMutableTreeNode lastLeaf = root.getLastLeaf();
    TreePath path = new TreePath(lastLeaf.getPath());
    jt.setSelectionPath(path);

    jt.setCellRenderer(new MyCellRenderer());

    JScrollPane jsp = new JScrollPane(jt);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    f.setSize(250, 250);
    f.show();
}

From source file:ec.nbdemetra.ui.demo.ComponentsDemo.java

public ComponentsDemo() {
    initStaticResources();/*from w  w w . j  av  a2s.co  m*/

    final Map<Id, Component> demoData = lookupComponents();

    final JPanel main = new JPanel(new BorderLayout());
    final JTree tree = new JTree();
    tree.setRootVisible(false);
    tree.setCellRenderer(new IdRenderer(demoData));

    IdsTree.fill(tree, Lists.newArrayList(demoData.keySet()));
    expandAll(tree);

    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            TreePath p = tree.getSelectionPath();
            if (p != null) {
                main.removeAll();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) p.getLastPathComponent();
                Id id = IdsTree.translate(node);
                Component c = demoData.get(id);
                main.add(c != null ? c : new JPanel());
                main.validate();
                main.repaint();
            }
        }
    });

    JTsList dragDrop = new JTsList();
    dragDrop.setShowHeader(false);
    dragDrop.setInformation(new ITsList.InfoType[] { ITsList.InfoType.TsIdentifier, ITsList.InfoType.Data });
    dragDrop.setPreferredSize(new Dimension(200, 200));
    dragDrop.setTsAction(DemoTsActions.DO_NOTHING);

    JSplitPane left = NbComponents.newJSplitPane(JSplitPane.VERTICAL_SPLIT, NbComponents.newJScrollPane(tree),
            dragDrop);
    JSplitPane splitPane = NbComponents.newJSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, main);
    splitPane.getLeftComponent().setPreferredSize(new Dimension(200, 400));

    setLayout(new BorderLayout());
    add(splitPane, BorderLayout.CENTER);
}

From source file:FileTree.java

/** Construct a FileTree */
public FileTree(File dir) {
    setLayout(new BorderLayout());

    // Make a tree list with all the nodes, and make it a JTree
    JTree tree = new JTree(addNodes(null, dir));

    // Add a listener
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }// w  w  w . j  a  va 2  s . c om
    });

    // Lastly, put the JTree into a JScrollPane.
    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(BorderLayout.CENTER, scrollpane);
}

From source file:net.sf.housekeeper.swing.CategoriesView.java

protected JComponent createControl() {
    tree = new JTree();

    tree.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    final BeanTreeCellRenderer renderer = new BeanTreeCellRenderer(Category.class, "name");
    renderer.setOpenIcon(null);//from  www .  ja va 2  s  .co  m
    renderer.setClosedIcon(null);
    renderer.setLeafIcon(null);
    tree.setCellRenderer(renderer);
    tree.setRootVisible(true);

    tree.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (node == null)
                return;

            Object nodeInfo = node.getUserObject();
            Category cat = (Category) nodeInfo;
            publishSelectionEvent(cat);
        }
    });

    refresh();
    return tree;
}

From source file:ClassTree.java

public ClassTreeFrame() {
    setTitle("ClassTree");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // the root of the class tree is Object
    root = new DefaultMutableTreeNode(java.lang.Object.class);
    model = new DefaultTreeModel(root);
    tree = new JTree(model);

    // add this class to populate the tree with some data
    addClass(getClass());//from   www . j  a  v  a2 s  .  c  o  m

    // set up node icons
    ClassNameTreeCellRenderer renderer = new ClassNameTreeCellRenderer();
    renderer.setClosedIcon(new ImageIcon("red-ball.gif"));
    renderer.setOpenIcon(new ImageIcon("yellow-ball.gif"));
    renderer.setLeafIcon(new ImageIcon("blue-ball.gif"));
    tree.setCellRenderer(renderer);

    // set up selection mode
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent event) {
            // the user selected a different node--update description
            TreePath path = tree.getSelectionPath();
            if (path == null)
                return;
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            Class<?> c = (Class<?>) selectedNode.getUserObject();
            String description = getFieldDescription(c);
            textArea.setText(description);
        }
    });
    int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
    tree.getSelectionModel().setSelectionMode(mode);

    // this text area holds the class description
    textArea = new JTextArea();

    // add tree and text area
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    panel.add(new JScrollPane(tree));
    panel.add(new JScrollPane(textArea));

    add(panel, BorderLayout.CENTER);

    addTextField();
}