修复JComboBox宽度

时间:2011-10-19 07:52:51

标签: java swing jcombobox dimensions nimbus

我有一个带有一些选项的JComboBox。当我在另一个组件上做出选择时,我正在改变JComboBox的内容。首先,我调用removeAllItems()方法,然后逐个添加我现在需要的字符串。

问题是,默认情况下我有一些选项,其中一个是较大的文本,因此JComboBox获得了正确显示该选项所需的宽度。当我更改JComboBox内容时,该文本选项消失了,现在一个较小的文本给了JComboBox的宽度,所以当我更改内容时它会变小。

我的第一种方法是调用myComboBox.setPreferredSize(myComboBox.getSize()),然后它的尺寸是固定的,但不正确:它的高度和宽度略小一些。我认为这是因为我使用的是Nimbus Look& Feel,而我从getSize()方法得到的维度是Java默认的Look%Feel给出的维度。

我也尝试myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))同样的结果。

我该如何解决这个问题?

我添加了一个代码示例,说明我如何使用布局:

    private String[] comboEventDOutputStrings = { "Run", "Stop", "Pause", "Conditioning time", "Deposition time", "Equilibration time", "Measurement time"};
    private String[] comboEventDInputStrings = { "Run", "Stop", "Pause"};
    // The first String array is the default set of values. It's obvious that the JComboBox will get smaller
    // when I change to the second array of contents

        //...   
            JPanel pane = new JPanel(new GridBagLayout());
            JPanel jPanelExterno = new JPanel(new GridBagLayout());
            GridBagConstraints cExterna = new GridBagConstraints();
            GridBagConstraints c = new GridBagConstraints();
            Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
            jPanelExterno.setBorder(loweredetched);
            jPanelExterno.setName("");

            cExterna.fill = GridBagConstraints.BOTH;
            cExterna.anchor = GridBagConstraints.NORTH;
            cExterna.gridx = 0;
            cExterna.gridy = 0;
            cExterna.insets = new Insets(10,10,5,5);

                JPanel jPanel1 = new JPanel(new GridBagLayout());
                jPanel1.setBorder(loweredetched);
                jPanel1.setName("PIO 1");

                jCheckBox1 = new JCheckBox("Enable");
                jCheckBox1.setSelected(false);
                jCheckBox1.setName("1");
                jCheckBox1.addActionListener(new PIOCheckListener());

                c.fill = GridBagConstraints.BOTH;
                c.anchor = GridBagConstraints.NORTH;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(10,5,10,10);
                jPanel1.add(jCheckBox1, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                JLabel label1 = new JLabel("IO Type");
                jPanel1.add(label1, c);
                c.gridx++;
                c.insets = new Insets(5,5,5,10);

                combo1 = new JComboBox(comboIOTypeStrings);
                combo1.setEnabled(false);
                combo1.setSelectedIndex(0);
                combo1.setName("1");
                combo1.addActionListener (new PIOComboListener());
                jPanel1.add(combo1, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label2 = new JLabel("Active level");
                jPanel1.add(label2, c);
                c.gridx++;
                c.insets = new Insets(5,5,5,10);

                combo2 = new JComboBox(comboActiveLevelStrings);
                combo2.setEnabled(false);
                combo2.setSelectedIndex(0);
                jPanel1.add(combo2, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label3 = new JLabel("Event");
                jPanel1.add(label3, c);
                c.gridx++;
                c.insets = new Insets(5,5,10,10);

                combo3 = new JComboBox(comboEventDOutputStrings);
                combo3.setEnabled(false);
                combo3.setSelectedIndex(0);
                jPanel1.add(combo3, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label4 = new JLabel("Node");
                jPanel1.add(label4, c);
                c.gridx++;
                c.insets = new Insets(5,5,10,10);

                combo4 = new JComboBox(comboNodeStrings);
                combo4.setEnabled(false);
                combo4.setSelectedIndex(0);
                jPanel1.add(combo4, c);

            jPanelExterno.add(jPanel1, cExterna);

        pioDialog.add(pane);
        pioDialog.pack();
        pioDialog.setLocationRelativeTo(null);
        pioDialog.setVisible(true);
    //...
}   


    class PIOComboListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent a) {
            JComboBox cb = (JComboBox)a.getSource();
            JComboBox target1 = null;
            JComboBox target2 = null;
            JComboBox target3 = null;
            switch(Integer.parseInt(cb.getName())){
                case 1:
                    target1 = combo2;
                    target2 = combo3;
                    target3 = combo4;
                    break;
                default:
                    Register.debug("PIODialog error: No target for comboBoxes");
                    break;
            }

            if(cb.getSelectedIndex()==2){ //Analog input
                target1.setEnabled(false);
                target2.setEnabled(false);
                target3.setEnabled(false);
            }
            else{
                target1.setEnabled(true);
                target2.setEnabled(true);
                target3.setEnabled(true);
                target2.removeAllItems();
                if(cb.getSelectedIndex()==0){
                    for(int i=0; i<comboEventDOutputStrings.length; i++)
                        target2.addItem(comboEventDOutputStrings[i]);
                } else {
                    for(int i=0; i<comboEventDInputStrings.length; i++)
                        target2.addItem(comboEventDInputStrings[i]);
                }
            }

        }
    }

它基本上是一个带有6个JPanel的GridBagLayout,每个都有一个新的GridBagLayout。我刚刚在这里写了jPanel1以简化操作。我希望它不是很混乱。

3 个答案:

答案 0 :(得分:13)

最后我找到了一个简单的解决方案:

jComboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXX");

并且非常适合我想要的东西

答案 1 :(得分:0)

在将JComboBox添加到jpanel1之前,请尝试将fill的{​​{1}}属性设置为GridBagConstraints(而不是NONE)。这应该(希望)阻止组合框调整大小。

BOTH

答案 2 :(得分:0)

NetBeans通过组件的右键单击上下文菜单中的几个复选框为您完成此操作:自动调整大小 - &gt;水平和垂直。

我将其关闭,并在我的修订控制历史记录中比较了NetBeans生成的代码。有一些令人惊讶的变化我无法与组件的大小相关,但我确实看到组合框如何添加到包含布局组的更改。基本上,

....addComponent(destinationFolderComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

已更改为

....addComponent(myComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)

JComboBox.setPreferredSize()永远不会被显式调用,因此我假设您通过告诉布局管理器首选大小和重新调整大小属性来获得更好的结果,而不是告诉JComboBox并希望布局管理器在后台读取这些属性

(顺便说一下,....addComponent(),我的意思是addComponent()createParallelGroup()createSequentialGroup()的长级联序列中调用,从{{1}开始我提出了上面的简短表格,因为我不想深入了解自动生成的NetBeans代码的细节,我无法手动编写或解释。你想要整个事情吗?你无法处理整件事!这里是:

new javax.swing.GroupLayout(myPanel)