Netbeans 手风琴组件 [英] Netbeans accordion component

查看:32
本文介绍了Netbeans 手风琴组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在进行的一个项目中,我们决定改变一些数据的显示方式.现在我们正在使用 Netbeans ListView,到目前为止效果很好.但是现在我们想要改变/扩展 ListView 以像手风琴一样工作.我们可以将 ListView 扩展为看起来像手风琴,但是如果我们向每个单元格/行添加按钮或文本输入,我们无法将 actionListener 附加到该按钮.似乎 ListView 侦听器始终位于摆动组件之上.

On one of projects that I'm working on, we decided to change our presentation of some data. Now we are using Netbeans ListView, and works great so far. But now we want to change/expand ListView to works like accordion. We can expand ListView to looks like accordion, but if we add buttons or text inputs to each cell/row we can't attach actionListener to that button. It seems like that ListView listeners are always on top of swing components.

有人对如何使用 Netbeans Explorer API 创建手风琴有任何建议吗?

Does anyone have any suggestions how to create accordion with using Netbeans Explorer API?

我们可以创建自定义的 Swing 组件,它的行为就像这样,实际上网络上已经存在一个.如果我们创建一个,我们需要将 Lookup 与我们的自定义 Swing 组件连接起来,以便与 Netbeans API 一起正常工作.我们只想在没有其他选择的情况下才这样做.谷歌搜索使用 Netbeans API 的此类组件没有给出任何有用的结果.

We could create custom Swing component which acts like that, actually there already exists one on the web. If we create one, we need to connect Lookup with our custom Swing component to works properly with Netbeans APIs. We want to do this only if there are no other options. Googling for such component which uses Netbeans APIs didn't give any useful results.

推荐答案

摘自 Toni Epples 博客

请注意:

  • 这里使用 JXTaskPane
  • 这是一个非动态示例

代码示例

public class TaskPaneView extends JScrollPane {

    private transient ExplorerManager manager;
    // create a taskpanecontainer
    JXTaskPaneContainer taskpanecontainer = new JXTaskPaneContainer();
    /** Listener to nearly everything */
    transient Listener managerListener;
    /** weak variation of the listener for property change on the explorer manager */
    transient PropertyChangeListener wlpc;

    /** True, if the selection listener is attached. */
    transient boolean listenerActive;

    // UI Settings:
    Font labelFont = new Font("Segoe UI", Font.BOLD, 14);
    Painter backgroundPainter = new MattePainter(Color.white);

    public TaskPaneView() {
        setViewportView(taskpanecontainer);
    }

    public void setBackground(Painter background) {
        this.backgroundPainter = background;
    }

    public void setLabelFont(Font labelFont) {
        this.labelFont = labelFont;
    }

    @Override
    public void addNotify() {
        super.addNotify();
        ExplorerManager em = ExplorerManager.find(this);
        if (em != manager) {
            if (manager != null) {
                manager.removePropertyChangeListener(wlpc);
            }
            manager = em;
            manager.addPropertyChangeListener(wlpc = WeakListeners.propertyChange(managerListener, manager));
            Node root = manager.getExploredContext();
            setRootNode(root);
        } else {
            // bugfix #23509, the listener were removed --> add it again
            if (!listenerActive && (manager != null)) {
                manager.addPropertyChangeListener(wlpc = WeakListeners.propertyChange(managerListener, manager));
            }
        }
    }

    /** Removes listeners.
     */
    @Override
    public void removeNotify() {
        super.removeNotify();
        listenerActive = false;
        // bugfix #23509, remove useless listeners
        if (manager != null) {
            manager.removePropertyChangeListener(wlpc);
        }
    }

    private void setRootNode(Node root) {
        //throw new UnsupportedOperationException("Not yet implemented");
        taskpanecontainer.removeAll();

        System.out.println("root node set " + root);
        Node[] children = root.getChildren().getNodes();
        for (int i = 0; i < children.length; i++) {
            Node node = children[i];
            JXTaskPane taskPane = new JXTaskPane();
            taskPane.setName(node.getName());
            taskPane.setCollapsed(true);
            taskPane.setTitle(node.getDisplayName());
            taskPane.setIcon(new ImageIcon(node.getIcon(BeanInfo.ICON_COLOR_16x16)));
            Action [] actions = node.getActions(true);
            for (int j = 0; j < actions.length; j++) {
                Action action = actions[j];
                taskPane.add(action);
            }
            taskpanecontainer.add(taskPane);
        }
    }

    private final class Listener implements PropertyChangeListener {

        public void propertyChange(PropertyChangeEvent evt) {

            if (ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) {
                setRootNode(manager.getExploredContext());
                return;
            }
        }
    }
}

这篇关于Netbeans 手风琴组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆