动态 JComboBox 大小

Dynamic JComboBox Size

我有一个 JComboBox,因为它包含的 String 太长,占用了太多空间。我只需要在选择时看到它们的全长。所以,我希望 JComboBox 只有在那时才是完整尺寸,否则会更短。

以下代码似乎有效。但是,由于我是一个 Java 新手,我只是想知道是否有更好或更标准的方法来做到这一点。

谢谢。

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class DynamicJComboBox{
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setLayout(new FlowLayout());
        frame.setSize(250, 100);

        JComboBox box = new JComboBox();
        box.addItem("Really Long Line Number One");
        box.addItem("Really Long Line Number Two");
        box.addItem("Really Long Line Number Three");
        box.addItem("Really Long Line Number Four");
        box.setPreferredSize(new Dimension(100, 30));
        box.addPopupMenuListener (new PopupMenuListener() {
            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {}

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                box.setSize(100,30);
            }
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                box.setSize(200,30);    
            }
        });
        frame.add(box);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception exc) {}

        SwingUtilities.invokeLater(() -> {
        createAndShowGUI();
        });
    }
}

The following code seems to work

不是真的,因为您更改了组合框的大小,这会导致组合框覆盖其右侧的任何组件显示。此外,组合框箭头绘制在组合框的中间。

查看 Combo Box Popup。该解决方案还使用了 PopupMenuListener(因此您走在了正确的轨道上),但它仅在显示时增加了弹出窗口的宽度,而不是组合框。

使弹出菜单足够大以显示项目的小技巧,即使组合框的大小可能更小

来源:http://www.jroller.com/santhosh/entry/make_jcombobox_popup_wide_enough

    import java.awt.Dimension;
    import java.util.Vector;

    import javax.swing.ComboBoxModel;
    import javax.swing.JComboBox;

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

        public ComboBoxFullMenu(E[] items) {
            super(items);
            addActionListener(this);
        }

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

        public ComboBoxFullMenu(ComboBoxModel<E> aModel) {
            super(aModel);
            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; 
        }
    }