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

后端 未结 6 2040
自闭症患者
自闭症患者 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:31

    Here is a great solution by Santhosh Kumar, without the need to mess with UI's and other nasty stuff like that!

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

    import javax.swing.*; 
    import java.awt.*; 
    import java.util.Vector; 
    
    // got this workaround from the following bug: 
    //      http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4618607 
    public class WideComboBox extends JComboBox{ 
    
        public WideComboBox() { 
        } 
    
        public WideComboBox(final Object items[]){ 
            super(items); 
        } 
    
        public WideComboBox(Vector items) { 
            super(items); 
        } 
    
            public WideComboBox(ComboBoxModel aModel) { 
            super(aModel); 
        } 
    
        private boolean layingOut = false; 
    
        public void doLayout(){ 
            try{ 
                layingOut = true; 
                    super.doLayout(); 
            }finally{ 
                layingOut = false; 
            } 
        } 
    
        public Dimension getSize(){ 
            Dimension dim = super.getSize(); 
            if(!layingOut) 
                dim.width = Math.max(dim.width, getPreferredSize().width); 
            return dim; 
        } 
    }
    

提交回复
热议问题