简体   繁体   English

JComboBox对于JFrame太长

[英]JComboBox too long for JFrame

I have a problem with JComboBoxes and long Strings. 我对JComboBoxes和长字符串有问题。 The result is that the JComboBox is too long for the JFrame so it doesn't display the whole box. 结果是JComboBox对于JFrame来说太长了,因此它无法显示整个框。 Here i have a little code which shows my problem: 在这里,我有一些代码来显示我的问题:

    JFrame frame = new JFrame();
    frame.setSize(500, 500);
    JPanel panel = new JPanel();
    DesignGridLayout layout = new DesignGridLayout(panel);
    Vector<ArrayList<String>> content = new Vector<ArrayList<String>>();
    ArrayList<String> list = new ArrayList<String>();
    list.add("12345");
    list.add("abcde");
    list.add("12345");
    list.add("abcde");
    list.add("12345");
    list.add("abcde");
    list.add("12345");
    list.add("abcde");
    list.add("12345");
    list.add("abcde");
    list.add("12345");
    list.add("abcde");
    content.add(list);
    ComboBoxFullMenu<ArrayList<String>> combobox = new ComboBoxFullMenu<ArrayList<String>>(content);
    combobox.setRenderer(new DefaultListCellRenderer() {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
        {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            ArrayList<String> array = (ArrayList<String>) value;
            if (!array.isEmpty())
            {
                String text = "";
                for (String info : array)
                {
                    text += info + "; ";
                }
                setText(text);
            }
            return this;
        }
    });
    AutoCompleteDecorator.decorate(combobox, new ObjectToStringConverter()
    {

        @Override
        public String getPreferredStringForItem(Object object)
        {
            if (object != null)
            {
                if (object instanceof ArrayList)
                {
                    ArrayList<String> list = (ArrayList<String>) object;
                    String text = "";
                    for (String info : list)
                    {
                        text += info + "; ";
                    }
                    return text;
                }
                else
                {
                    return object.toString();
                }
            }
            return null;
        }
    });
    layout.row().grid().add(combobox);
    frame.add(panel);
    frame.setVisible(true);

And here is the class ComboBoxFullMenu: 这是ComboBoxFullMenu类:

public class ComboBoxFullMenu<E> extends JComboBox<E>
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ComboBoxFullMenu(Vector<E> items) 
    {
        super(items);
        addActionListener(this);
    }

    public ComboBoxFullMenu()
    {
        super();
        addActionListener(this);
    }

    public ComboBoxFullMenu (DefaultComboBoxModel<E> defaultComboBoxModel)
    {
        super(defaultComboBoxModel);
        addActionListener(this);
    }

    /**
     * Small hack to get pop up menu size bigger enough to show items even though
     * the combo box size could be smaller
     * */
    private boolean layingOut = false; 

    @Override
    public void doLayout()
    { 
        try
        { 
            layingOut = true; 
            super.doLayout(); 
        }
        finally
        { 
            layingOut = false; 
        } 
    } 

    @Override
    public Dimension getSize()
    { 
        Dimension dim = super.getSize(); 
        if ( !layingOut ) 
        {
            dim.width = Math.max(dim.width, getPreferredSize().width);
        }
        return dim; 
    }
}

I also tried using DefaultListCellRenderer and ObjectTroStringConverter to display the selected item in a shorter way, but i need to see all informations in the dropdownmenu and i think the JComboBox calculates its width about the longest item? 我还尝试使用DefaultListCellRenderer和ObjectTroStringConverter以较短的方式显示所选项目,但是我需要在dropdownmenu中查看所有信息,并且我认为JComboBox会计算最长项目的宽度? I hope you understand my problem otherwise let me know. 希望您能理解我的问题,否则请告诉我。

Check out Combo Box Popup is allows you to control the width of the popup. 检出组合框弹出窗口可让您控制弹出窗口的宽度。 You can set the width 您可以设置宽度

  1. to the size of the largest string, or 到最大字符串的大小,或者
  2. specify a maximum width. 指定最大宽度。

You can then turn on scrolling in the popup if the string is larger than the specified width. 然后,如果字符串大于指定的宽度,则可以在弹出窗口中打开滚动。

Try using this WideComboBox . 尝试使用此WideComboBox It allows you to set the width of the JComboBox but when the popup is show it shows the entire item. 它允许您设置JComboBox的宽度,但是当显示弹出窗口时,它将显示整个项目。 Meaning the popup will be wider than the JComboBox . 这意味着弹出窗口将比JComboBox宽。 You also may find this helpful 您可能还会发现很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM