2010-03-29 166 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!"); 
    } 
});