List of usage examples for javax.swing JTree addTreeSelectionListener
public void addTreeSelectionListener(TreeSelectionListener tsl)
TreeSelection
events. From source file:Main.java
public static void main(String[] argv) throws Exception { JTree tree = new JTree(); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent evt) { TreePath[] paths = evt.getPaths(); for (int i = 0; i < paths.length; i++) { if (evt.isAddedPath(i)) { System.out.println("This node has been selected"); } else { System.out.println("This node has been deselected"); }/* w ww .j a va2 s. c om*/ } } }); }
From source file:UsingTreeSelectionListener.java
public static void main(String[] a) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTree tree = new JTree(); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addTreeSelectionListener(new SelectionListener()); frame.add(new JScrollPane(tree)); frame.setSize(300, 200);/*w ww.ja v a 2s . c om*/ frame.setVisible(true); }
From source file:SimpleClient.java
public static void main(String argv[]) { boolean usage = false; for (int optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-L")) { url.addElement(argv[++optind]); } else if (argv[optind].startsWith("-")) { usage = true;/*from www.ja v a 2 s . c o m*/ break; } else { usage = true; break; } } if (usage || url.size() == 0) { System.out.println("Usage: SimpleClient -L url"); System.out.println(" where url is protocol://username:password@hostname/"); System.exit(1); } try { // Set up our Mailcap entries. This will allow the JAF // to locate our viewers. File capfile = new File("simple.mailcap"); if (!capfile.isFile()) { System.out.println("Cannot locate the \"simple.mailcap\" file."); System.exit(1); } CommandMap.setDefaultCommandMap(new MailcapCommandMap(new FileInputStream(capfile))); JFrame frame = new JFrame("Simple JavaMail Client"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Get a Store object SimpleAuthenticator auth = new SimpleAuthenticator(frame); Session session = Session.getDefaultInstance(System.getProperties(), auth); //session.setDebug(true); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); // create a node for each store we have for (Enumeration e = url.elements(); e.hasMoreElements();) { String urlstring = (String) e.nextElement(); URLName urln = new URLName(urlstring); Store store = session.getStore(urln); StoreTreeNode storenode = new StoreTreeNode(store); root.add(storenode); } DefaultTreeModel treeModel = new DefaultTreeModel(root); JTree tree = new JTree(treeModel); tree.addTreeSelectionListener(new TreePress()); /* Put the Tree in a scroller. */ JScrollPane sp = new JScrollPane(); sp.setPreferredSize(new Dimension(250, 300)); sp.getViewport().add(tree); /* Create a double buffered JPanel */ JPanel sv = new JPanel(new BorderLayout()); sv.add("Center", sp); fv = new FolderViewer(null); JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sv, fv); jsp.setOneTouchExpandable(true); mv = new MessageViewer(); JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, mv); jsp2.setOneTouchExpandable(true); frame.getContentPane().add(jsp2); frame.pack(); frame.show(); } catch (Exception ex) { System.out.println("SimpletClient caught exception"); ex.printStackTrace(); System.exit(1); } }
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 *///from www .j a v a 2 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:TreeSelectionRow.java
public static void main(String args[]) { String title = "JTree Sample"; JFrame frame = new JFrame(title); JTree tree = new JTree(); TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); System.out.println("Min: " + treeSource.getMinSelectionRow()); System.out.println("Max: " + treeSource.getMaxSelectionRow()); System.out.println("Lead: " + treeSource.getLeadSelectionRow()); System.out.println("Row: " + treeSource.getSelectionRows()[0]); }//from w ww .j a v a 2 s. c om }; tree.addTreeSelectionListener(treeSelectionListener); JScrollPane scrollPane = new JScrollPane(tree); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JTree tree = new JTree(); TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { @Override/* w w w.j a va2 s .c o m*/ public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); System.out.println("Min: " + treeSource.getMinSelectionRow()); System.out.println("Max: " + treeSource.getMaxSelectionRow()); System.out.println("Lead: " + treeSource.getLeadSelectionRow()); System.out.println("Row: " + treeSource.getSelectionRows()[0]); } }; tree.addTreeSelectionListener(treeSelectionListener); JFrame frame = new JFrame(); frame.add(new JScrollPane(tree)); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JTree Demo"); f.setSize(260, 240);/*from ww w. j av a 2s . co m*/ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A"); root.add(aNode); DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B"); aNode.add(bNode); DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C"); aNode.add(cNode); cNode.add(new DefaultMutableTreeNode("D")); cNode.add(new DefaultMutableTreeNode("E")); DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F"); root.add(fNode); DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G"); fNode.add(gNode); fNode.add(new DefaultMutableTreeNode("H")); gNode.add(new DefaultMutableTreeNode("I")); JTree jtree = new JTree(root); jtree.setEditable(true); TreeSelectionModel tsm = jtree.getSelectionModel(); tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); jtree.addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent tee) { TreePath tp = tee.getPath(); System.out.println("Expansion: " + tp.getLastPathComponent()); } public void treeCollapsed(TreeExpansionEvent tee) { TreePath tp = tee.getPath(); System.out.println("Collapse: " + tp.getLastPathComponent()); } }); jtree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent tse) { TreePath tp = tse.getPath(); System.out.println("Selection event: " + tp.getLastPathComponent()); } }); jtree.getModel().addTreeModelListener(new TreeModelListener() { public void treeNodesChanged(TreeModelEvent tme) { TreePath tp = tme.getTreePath(); Object[] children = tme.getChildren(); DefaultMutableTreeNode changedNode; if (children != null) changedNode = (DefaultMutableTreeNode) children[0]; else changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent(); System.out.println("Model change path: " + tp + "New data: " + changedNode.getUserObject()); } public void treeNodesInserted(TreeModelEvent tme) { } public void treeNodesRemoved(TreeModelEvent tme) { } public void treeStructureChanged(TreeModelEvent tme) { } }); f.add(new JScrollPane(jtree)); f.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { JTree tree = new JTree(); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); int treeSelectedRows[] = { 3, 1 }; tree.setSelectionRows(treeSelectedRows); TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { @Override//ww w . j av a 2 s . c om public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); System.out.println("Min: " + treeSource.getMinSelectionRow()); System.out.println("Max: " + treeSource.getMaxSelectionRow()); System.out.println("Lead: " + treeSource.getLeadSelectionRow()); System.out.println("Row: " + treeSource.getSelectionRows()[0]); } }; tree.addTreeSelectionListener(treeSelectionListener); JFrame frame = new JFrame("JTree With Multi-Discontiguous selection"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(tree)); frame.setPreferredSize(new Dimension(380, 320)); frame.setLocation(150, 150); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) { JTree tree = new JTree(); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); int treeSelectedRows[] = { 3, 1 }; tree.setSelectionRows(treeSelectedRows); TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { @Override/*from w w w . j a v a2 s .c o m*/ public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); System.out.println("Min: " + treeSource.getMinSelectionRow()); System.out.println("Max: " + treeSource.getMaxSelectionRow()); System.out.println("Lead: " + treeSource.getLeadSelectionRow()); System.out.println("Row: " + treeSource.getSelectionRows()[0]); } }; tree.addTreeSelectionListener(treeSelectionListener); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JScrollPane(tree)); frame.setPreferredSize(new Dimension(380, 320)); frame.setLocation(150, 150); frame.pack(); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) { // create a hierarchy of nodes MutableTreeNode root = new DefaultMutableTreeNode("A"); MutableTreeNode bNode = new DefaultMutableTreeNode("B"); MutableTreeNode cNode = new DefaultMutableTreeNode("C"); root.insert(bNode, 0);/*from ww w . ja va 2 s. c om*/ root.insert(cNode, 1); bNode.insert(new DefaultMutableTreeNode("1"), 0); bNode.insert(new DefaultMutableTreeNode("2"), 1); cNode.insert(new DefaultMutableTreeNode("1"), 0); cNode.insert(new DefaultMutableTreeNode("2"), 1); final DefaultTreeModel model = new DefaultTreeModel(root); final JTree tree = new JTree(model); final JTextField nameField = new JTextField("Z"); final JButton button = new JButton("Add"); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath tp = tree.getSelectionPath(); MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent(); int insertIndex = 0; if (insertNode.getParent() != null) { MutableTreeNode parent = (MutableTreeNode) insertNode.getParent(); insertIndex = parent.getIndex(insertNode) + 1; insertNode = parent; } MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText()); model.insertNodeInto(node, insertNode, insertIndex); } }); JPanel addPanel = new JPanel(new GridLayout(2, 1)); addPanel.add(nameField); addPanel.add(button); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath tp = e.getNewLeadSelectionPath(); button.setEnabled(tp != null); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.getContentPane().add(new JScrollPane(tree)); frame.getContentPane().add(addPanel, BorderLayout.SOUTH); frame.setVisible(true); }