+ Getting an event when a selection on my JTree changes Joris Van den Bogaert Implement a TreeSelectionListener and add it using addTreeSelectionListener. Main.java: import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; import java.awt.*; import java.net.*; import java.awt.event.*; public class Main extends JFrame { public Main() { DefaultMutableTreeNode root = createNodes(); JTree tree = new JTree(root); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getPath(); System.out.println(path.getLastPathComponent()); } }); getContentPane().add(new JScrollPane(tree)); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } public static DefaultMutableTreeNode createNodes() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("Java"); DefaultMutableTreeNode j2se = new DefaultMutableTreeNode("J2SE"); DefaultMutableTreeNode j2ee = new DefaultMutableTreeNode("J2EE"); DefaultMutableTreeNode j2me = new DefaultMutableTreeNode("J2ME"); j2se.add(new DefaultMutableTreeNode("http://java.sun.com/j2se/")); j2ee.add(new DefaultMutableTreeNode("http://java.sun.com/j2ee/")); j2me.add(new DefaultMutableTreeNode("http://java.sun.com/j2me/")); root.add(j2se); root.add(j2ee); root.add(j2me); return root; } public static void main(String []args) { Main main = new Main(); main.setSize(400, 400); main.setVisible(true); } }
Getting an event when a selection on my JTree changes
Joris Van den BogaertImplement a TreeSelectionListener and add it using addTreeSelectionListener.
Main.java:
You might also like
Switching to another display mode
From JDK1.4, you can use the class DisplayMode. Either instantiate a DisplayMode object by specifying...
Get notified when the user has moved...
Main.java:
Changing the color of the title bar...
This worked for me, using the Windows Look and Feel. To change the colors of the title bar, modify some...