how to implement listener?
I have some dynamic tree. And now I need to implement some functionality, that happend every time, when simply click on node. (I mean only one click on node, which "makes it blue")
**EDIT2: ** I use beanTreeView and package openide
How to implement listener of this action?
EDIT - added pseudocode
public class MyNode extends AbstractNode{ //openide package
private String name;
public MyNode(String nameOfNode){
super (new Children.LEAF);
name = nameOfNode;
}
....
....
}
public class IWantNameOfSelectedNode extends JPanel{
private JLabel jLnameOfNode;
public IWantNameOfSelectedNode(){
jLnameOfNode.setText("wiating for node selection");
开发者_Go百科 }
Now, I need put name of selected node to jLabel, and change it every time when selection of node changes.
Assuming you're using the Swing JTree
class you should define a TreeSelectionListener
and add it to the underlying TreeModel
. If you wish to use ActionListener
instead you'll need to write some adapter code to translate TreeSelectionEvent
s into ActionEvent
s (although this would actually be fairly pointless).
Example
/**
* Adapter class responsible for translating TreeSelectionEvents into
* ActionEvents.
*/
public class TreeSelectionAdapter implements TreeSelectionListener {
private final AtomicInteger nextId = new AtomicInteger(0);
// Prefer CopyOnWriteArrayList to avoid ConcurrentModificationException if an
// ActionListener removes itself as a listener during notification.
private final CopyOnWriteArrayList<ActionListener> listeners;
public TreeSelectionAdapter() {
this.listeners = new CopyOnWriteArrayList<ActionListener();
}
public void addActionListener(ActionListener l) {
this.listeners.add(l);
}
public void removeActionListener(ActionListener l) {
this.listeners.remove(l);
}
public void valueChanged(TreeSelectionEvent evt) {
// Create new ActionEvent which corresponds to incoming TreeSelectionEvent
// and notify registered ActionListeners.
ActionEvent aEvt = new ActionEvent(evt.getSource(),
nextId.getAndIncrement(), "selectionChanged");
for (ActionListener listener : listeners) {
listener.actionPerformed(listener);
}
}
}
TreeNode rootNode = createTreeModel(); // Create custom model
JTree tree = new JTree(rootNode); // Install model into JTree.
// Add adapter listener to underlying selection model.
tree.getSelectionModel().addTreeSelectionListener(adapter);
// Register ActionListener with adapter listener.
adapter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.err.println("Selection has changed in some way!");
}
});
I am assuming it is a Swing Tree. You could achieve this by using a CustomRenderer component or using a TreeSelectionListener interface.
This link has a tutorial for an advanced Example on how to alter the icons, backgrounds etc. What you need is a much simpler version than that.
The code you will be interested in is
public Component getTreeCellRendererComponent( JTree tree,
Object value, boolean bSelected, boolean bExpanded,
boolean bLeaf, int iRow, boolean bHasFocus )
{
// Find out which node we are rendering and get its text
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
String labelText = (String)node.getUserObject();
this.bSelected = bSelected;
// Set the correct foreground color
if( !bSelected )
setForeground( Color.black );
else
setForeground( Color.white );
....
}
精彩评论