2010-03-29 171 views
0

我有一些动态树。现在我需要实现一些功能,每次都会发生,只需单击节点即可。 (我的意思是在节点只有一个点击,这“使得蓝”)如何实现监听器?

** EDIT2:**我用beanTreeView和封装openide

如何实现这个动作的监听器?

编辑 - 添加伪

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"); 
    } 

现在,我需要把选择的节点的名称,JLabel的,而且每次改变它时的节点改变选择。

+1

也许你已经有一些代码,我们将开始?目前可以给你链接http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html与来自太阳的教程(实际上它应该是足够的) – Roman 2010-03-29 13:11:54

+0

除了上面罗马所说的,一旦你理解了听众,我会建议检查匿名内部类。 http://www.developer.com/java/other/article.phpr/3300881/The-Essence-of-OOP-using-Java-Anonymous-Classes.htm 他们可以帮助听众减少一点恼​​人的执行IMO。 – CheesePls 2010-03-29 13:27:08

回答

0

我假设它是一棵摇摆树。您可以通过使用CustomRenderer组件或使用TreeSelectionListener接口来实现此目的。

这个link有一个关于如何改变图标,背景等高级例子的教程。你需要的是一个比这更简单的版本。

你会感兴趣的代码是

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); 
      .... 
    } 
+0

我需要interclass actionListener。查看我提出的问题的代码 – venom 2010-03-29 13:28:47

1

假设你正在使用的Swing JTree类,你应该定义一个TreeSelectionListener,并将其添加到下面的TreeModel。如果您希望使用ActionListener,您需要编写一些适配器代码将TreeSelectionEvent s转换为ActionEvent s(尽管这实际上是毫无意义的)。

/** 
* 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!"); 
    } 
});