开发者

how to load Tree inside JComboBox?

开发者 https://www.devze.com 2023-02-17 10:54 出处:网络
How can I show a tree inside a JComboBox popup? Here is exam开发者_运维问答ple tree: Theoretical computer science

How can I show a tree inside a JComboBox popup?

Here is exam开发者_运维问答ple tree:

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


There is no default way to put a tree in a combo box. There are a couple of options:

If you can give allowing expansion of nodes, you can achieve a similar effect by adding space before some of the options in a standard JComobBox. Or even space and a dash in front of leaf options.

If you need expansion of nodes, then a better option would be to add a popup that appears below a button that listens for selections of items in the tree. Something like this might be a better choice depending on how your GUI is laid out.


When swinglabs was active there used to be JXComboBox that allows you to have other components in the dropdown like a JTable/JTree. Check here or here you may find the source for it or the documentation.


You coud write your own renderer and put a treenode picture in front of the returning label for the subnodes.

Something like:

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


I use the TreeComboBox of mindgame (link). It just requires the class AbstractComboBoxUI of the same project.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号