如何在JComboBox中加载Tree?

时间:2011-03-15 15:43:29

标签: java swing tree jcombobox

如何在JComboBox弹出窗口中显示树?

以下是示例树:

Theoretical computer science 
           Mathematical logic 
            Automata theory 
Algorithms and data structures
           Analysis of algorithms
           Algorithms

4 个答案:

答案 0 :(得分:2)

没有默认方法将树放在组合框中。有几种选择:

如果您可以允许扩展节点,则可以通过在标准JComobBox中的某些选项之前添加空格来实现类似的效果。甚至是叶子选项前的空间和破折号。

如果您需要扩展节点,那么更好的选择是添加一个弹出窗口,该弹出窗口显示在按钮中,该按钮用于监听树中项目的选择。根据GUI的布局方式,这样的事情可能是更好的选择。

答案 1 :(得分:0)

当swinglabs处于活动状态时,曾经有JXComboBox允许您在下拉列表中拥有其他组件,如JTable / JTree。 查看herehere您可以找到它的来源或文档。

答案 2 :(得分:0)

您可以编写自己的渲染器,并在子节点的返回标签前放置一个treenode图片。

类似的东西:

private static class NodeComboBoxRenderer implements ListCellRenderer {

    protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if(value.isSubNode()) { //something to find out
            renderer.setIcon("here comes the resource");
        }
        return renderer;
    }
}

答案 3 :(得分:0)

我使用了TreeComboBox思维游戏(link)。它只需要同一个项目的类AbstractComboBoxUI