如何实现监听器?

发布于 2024-08-26 23:01:39 字数 628 浏览 12 评论 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,并在每次节点选择更改时更改它。

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

Now, I need put name of selected node to jLabel, and change it every time when selection of node changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-09-02 23:01:39

假设您使用的是 Swing JTree 类,您应该定义一个 TreeSelectionListener 并将其添加到底层 TreeModel 中。如果您希望使用 ActionListener,则需要编写一些适配器代码来将 TreeSelectionEvent 转换为 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!");
  }
});

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 TreeSelectionEvents into ActionEvents (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!");
  }
});
烙印 2024-09-02 23:01:39

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

链接有一个关于高级示例的教程如何更改图标、背景等。您需要的是一个比这简单得多的版本。

您感兴趣的代码是

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

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 );
            ....
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文