| Author |
JTree double click
|
Bob Green
Ranch Hand
Joined: Feb 29, 2004
Posts: 93
|
|
I have the following issue: I have two components on my application; leftComponent and rightComponent. The leftComponent has a JTree with 3 nodes. If a user selects node 1, the application is supposed to display something on the rightComponent. For some reason, I need to click the node twice to have something displayed on the rightcomponent. I did see it called valueChanged when I select a node. Any idea? BTW, I update the rightcomponent in the valueChanged() method. Thanks in advance!
|
 |
Bob Green
Ranch Hand
Joined: Feb 29, 2004
Posts: 93
|
|
Here's my code: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JTree; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.plaf.basic.BasicTreeUI; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; public class SimpleSelection1 extends JFrame implements TreeSelectionListener { public static void main(String[] args) { new SimpleSelection1(); } private JTree tree; private JPanel rightComponent; public SimpleSelection1() { super("SimpleSelection1"); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode child; DefaultMutableTreeNode grandChild; child = new DefaultMutableTreeNode("Servers "); root.add(child); child = new DefaultMutableTreeNode("Clients "); root.add(child); child = new DefaultMutableTreeNode("Destinations "); root.add(child); child = new DefaultMutableTreeNode("Messages "); root.add(child); tree = new JTree(root); tree.addTreeSelectionListener(this); tree.setBackground(Color.white); DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree .getCellRenderer(); renderer.setOpenIcon(null); renderer.setClosedIcon(null); renderer.setLeafIcon(null); BasicTreeUI ui = (BasicTreeUI) tree.getUI(); ui.setExpandedIcon(null); ui.setCollapsedIcon(null); JPanel leftComponent = new JPanel(); leftComponent.setBackground(Color.white); leftComponent.setBorder( BorderFactory.createLineBorder(Color.lightGray, 2)); leftComponent.add(tree); rightComponent = new JPanel(); rightComponent.setBackground(Color.LIGHT_GRAY); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); splitPane.setOneTouchExpandable(true); splitPane.setLeftComponent(leftComponent); splitPane.setRightComponent(rightComponent); Container content = getContentPane(); content.add(splitPane, BorderLayout.CENTER); setDefaultLookAndFeelDecorated(true); setSize(700, 450); setLocation(100, 100); setVisible(true); } public void valueChanged(TreeSelectionEvent event) { System.out.println("valueChanged is called"); rightComponent.removeAll(); String currentSelection = tree.getLastSelectedPathComponent().toString().trim(); if (currentSelection.equals("Servers")) { rightComponent.add(new JLabel("Servers is selected")); } else if (currentSelection.equals("Clients")) { rightComponent.add(new JLabel("Clients is selected")); } else if (currentSelection.equals("Destinations")) { rightComponent.add(new JLabel("Destinations is selected")); } else { rightComponent.add(new JLabel("Invalid selection")); } } }
|
 |
 |
|
|
subject: JTree double click
|
|
|