How can I change the width of a JComboBox dropdown list?

后端 未结 6 2134
自闭症患者
自闭症患者 2020-11-30 04:19

I have an editable JComboBox which contains a list of single letter values. Because of that the combobox is very small.

Every letter has a special mean

6条回答
  •  既然无缘
    2020-11-30 04:46

    Here is a nice solution from tutiez.

    Before setting up the Dimension of popup list, it gets the biggest item from it and calculated the width needed to show it completely.

    public class WiderDropDownCombo extends JComboBox {
    
        private String type;
        private boolean layingOut = false;
        private int widestLengh = 0;
        private boolean wide = false;
    
        public WiderDropDownCombo(Object[] objs) {
            super(objs);
        }
    
        public boolean isWide() {
            return wide;
        }
    
        // Setting the JComboBox wide
        public void setWide(boolean wide) {
            this.wide = wide;
            widestLengh = getWidestItemWidth();
    
        }
    
        public Dimension getSize() {
            Dimension dim = super.getSize();
            if (!layingOut && isWide())
                dim.width = Math.max(widestLengh, dim.width);
            return dim;
        }
    
        public int getWidestItemWidth() {
    
            int numOfItems = this.getItemCount();
            Font font = this.getFont();
            FontMetrics metrics = this.getFontMetrics(font);
            int widest = 0;
            for (int i = 0; i < numOfItems; i++) {
                Object item = this.getItemAt(i);
                int lineWidth = metrics.stringWidth(item.toString());
                widest = Math.max(widest, lineWidth);
            }
    
            return widest + 5;
        }
    
        public void doLayout() {
            try {
                layingOut = true;
                super.doLayout();
            } finally {
                layingOut = false;
            }
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String t) {
            type = t;
        }
    
        public static void main(String[] args) {
            String title = "Combo Test";
            JFrame frame = new JFrame(title);
    
            String[] items = {
                    "I need lot of width to be visible , oh am I visible now",
                    "I need lot of width to be visible , oh am I visible now" };
            WiderDropDownCombo simpleCombo = new WiderDropDownCombo(items);
            simpleCombo.setPreferredSize(new Dimension(180, 20));
            simpleCombo.setWide(true);
            JLabel label = new JLabel("Wider Drop Down Demo");
    
            frame.getContentPane().add(simpleCombo, BorderLayout.NORTH);
            frame.getContentPane().add(label, BorderLayout.SOUTH);
            int width = 200;
            int height = 150;
            frame.setSize(width, height);
            frame.setVisible(true);
    
        }
    }
    

    The code above has already a main for a quick test. But notice that the statement below may be adjusted to around 20 if you want to have a vertical scroll.

    return widest + 5;
    

    Hope it is useful for future reference!

提交回复
热议问题