【问题标题】:How do I combine a Combo Box with a Tree in Swing?如何在 Swing 中组合组合框和树?
【发布时间】:2008-12-10 21:49:52
【问题描述】:

对于我的应用程序,我想要一个Combo Box,它在下拉为Tree 时显示其元素。问题是,我对 Swing 不够精通,不知道如何去做。至少不会最终从头开始编写新的小部件,或者类似的东西。

如果不从头开始创建一个,我该如何做这样的事情?

【问题讨论】:

    标签: java swing combobox tree


    【解决方案1】:

    我想我会将它实现为 JViewPort 中的 JTree 组件,然后是扩展按钮。折叠时,它看起来像一个组合框。当您单击展开按钮时,视口将展开,允许您滚动并选择 JTree 中的节点。当您选择节点时,视口会折叠回来,只显示所选节点和展开按钮。

    【讨论】:

    • 听起来有点工作,但我想这样就可以了。我只需要玩弄它。
    【解决方案2】:

    嘿,你猜怎么着!这是你的幸运日。

    我过去使用过这个框架。它非常完整。我不知道他们有这个 已经。

    JIDE Soft

    alt text http://img89.imageshack.us/img89/8324/combotreejj1.png

    不是太贵,但要花一些时间来理解 API(这并不复杂,但他们创造了很多新东西)

    【讨论】:

    • 很酷,但我不确定我是否愿意为我只会使用一点点的东西付费。不过,感谢您的发现。
    • 是的。我同意。然后建议使用 jtree 显示/隐藏面板。我很确定他们就是这样做的。
    【解决方案3】:

    覆盖 getListCellRendererComponent 方法并按级别顺序创建组件。 对于每个树级别,将绘制的字符串向右移动 3 个空格。

    例子:

    1

    。一个

    。 b

    2

    。 c

    您可以查看的原始实现

    public Component getListCellRendererComponent(
                                           JList list,
                                           Object value,
                                           int index,
                                           boolean isSelected,
                                           boolean cellHasFocus) {
            //Get the selected index. (The index param isn't
            //always valid, so just use the value.)
            int selectedIndex = ((Integer)value).intValue();
    
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
    
        //Set the icon and text.  If icon was null, say so.
        ImageIcon icon = images[selectedIndex];
        String pet = petStrings[selectedIndex];
        setIcon(icon);
        if (icon != null) {
            setText(pet);
            setFont(list.getFont());
        } else {
            setUhOhText(pet + " (no image available)",
                        list.getFont());
        }
    
        return this;
    }
    

    【讨论】:

    • 但这不会失去展开折叠行为
    • 您可以对从Java Tutorial on ComboBox 找到的代码稍作更改以匹配您的解释...
    【解决方案4】:

    您可以创建一个 ComboBoxEditor,其组件(由 getEditorComponent 返回)是一个 JTree

    虽然您可能已经尝试过。

    我不知道它会是什么样子。如果您使它起作用,请发布屏幕截图。 :)

    编辑

    我给它一个快速肮脏的尝试。它很糟糕,但只是一个开始。

    alt text http://img120.imageshack.us/img120/2563/yiakxk2.png

    这是代码,值得。 :(

    也许您应该开始考虑替代方案。当按下隐藏面板时,会出现一个没有边框的 JButton 假 Combo 并显示树。

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ComboTree {
        public static void main( String [] args ) { 
            JComboBox c = new JComboBox( new String [] { "Hello", "there"});
            c.setModel( new CustomComboModel() );
            c.setEditor( new TreeComboEditor() );
            c.setRenderer( new TreeComboEditor() );
            JFrame frame = new JFrame();
            frame.add( c , BorderLayout.NORTH ) ;
            frame.pack();
            frame.setVisible( true );
    
        }
    }
    
    class CustomComboModel implements ComboBoxModel {
         public Object  getSelectedItem() { return ":P"; }
         public void    setSelectedItem(Object anItem) {}
         public void    addListDataListener(ListDataListener l) {}
         public Object  getElementAt(int index)  { return "at " + index ; }
         public int getSize()  { return 2; }
         public void    removeListDataListener(ListDataListener l)  {}
    }
    class TreeComboEditor implements ComboBoxEditor, ListCellRenderer {
    
         // Editor interface
         public void addActionListener(ActionListener l) {}
         public Component   getEditorComponent() {
             return new JTree() ;
             }
         public Object  getItem() { return "";}
         public void    removeActionListener(ActionListener l) {}
         public void    selectAll() {}
         public void    setItem(Object anObject) {}
    
         // Render interface
         public Component getListCellRendererComponent(JList list,
                                               Object value,
                                               int index,
                                               boolean isSelected,
                                               boolean cellHasFocus) {
            return new JTree();
        }
    }
    

    【讨论】:

    • 是的,这正是我的想法,但我不确定它将如何处理折叠/展开与选择元素。我很快就会试一试。
    • 别这样。看起来很可怕。 :S
    • 这绝对是无用的代码。它没有做需要做的事情,而是以最糟糕的方式做到了。 (-1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多