开发者

JComboBox.addItem for null objects

HI,

I have a JComboBox to which I'm adding my custom object items. But sometimes the object added are empty. So, when the comboBox has empty items in it, it collapses and becomes very thin. But once populated, bec开发者_开发百科omes of noral height. Can somebody please suggest semething to keep the height of the JComboBox maintained even when no items or empty items added.

private final JComboBox comboField = new JComboBox (); comboField.removeAllItems(); comboField.addItem(getFirstConfig()); comboField.addItem(getSecConfig());

Thanks


You can do it by setting the minimum size, but correct value is font dependent. You can guess at the value, or you can set it from the addNotify with some help from FontMetrics.

I've generally found it's easier to do it by making the first item something like: "--Select Config--", or, if you know there are no items to choose from: "--No Configs Available--"

Update:

Since you can't use a placeholder, your alternatives depend on the layout manager and the LAF in use.

Mostly this amounts to setting the minimum and/or preferred size of the JComponent. It's inexact, but I generally use GridBagLayout and have good results with this approach:

    @Override
    public void addNotify() {
        super.addNotify();
        combo.setMinimumSize(atLeast(combo.getMinimumSize(), 100, 20));
        combo.setPreferredSize(atLeast(combo.getPreferredSize(), 100, 20));
    }

    private Dimension atLeast(Dimension d, int minWidth, int minHeight) {
        d.width = Math.max(minWidth, d.width);
        d.height = Math.max(minHeight, d.height);
        return d;
    }

Replace the 100,20 with minimum value that work for you.


Just added a check that if the options for the Jcombobox are null, then add an empty string "". This keeps the ComboBox from collapsing.


Are you talking about the drop down list or the box itself. If it is just the box, can't you just set a minimum height on it?

Update: Based on your comment, I did some experiment with it. Depending on what layout manager you are using, the behavior of the components size is different. One way it might work is to wrap your combo box in a flow layout panel and set preferred size, as showing below. If this still doesn't help, please tell me what's your layout manager.

code (an executable demo):

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

public class JComboBoxTest {
    private static JComboBox combo;
    private static String[] labelStrs = new String[5];

    private static void createAndShowGUI() {
        final JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 5; i += 1) {
            labelStrs[i] = "I am label #" + i;
        }

        combo = new JComboBox(labelStrs);

        //------------------------------------------------
        combo.setPreferredSize(new Dimension(100, 20));
        //------------------------------------------------

        JButton remove = new JButton("remove");
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                combo.removeAllItems();
                frame.repaint();
            }
        });

        JButton add = new JButton("add");
        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < 5; i += 1) {
                    combo.addItem(labelStrs[i]);
                }
                frame.repaint();
            }
        });

        Panel container = new Panel();
        Panel wrapper = new Panel();
        Panel btns = new Panel();

        container.setLayout(new FlowLayout());
        container.add(combo);
        wrapper.add(container); 
        btns.add(remove);
        btns.add(add);

        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(wrapper, BorderLayout.CENTER);
        frame.getContentPane().add(btns, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }   
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜