How can I make combobox's list wider?

风格不统一 提交于 2019-12-05 10:59:01

I think this may help.

Code from link

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);

    }
}

more little bit complex workaround is Combo Box Popup by @camickr, implements

  • setScrollBarRequired – when true, a horizontal scroll bar will automatically be displayed when necessary
  • setPopupWider – when true, the width of the popup will be based on the items in the combo box. The width will never be smaller than the combo box.
  • setMaximumWidth – can control the width of the popup just in case you have an unreasonably long item to render.
  • setPopupAbove – when true the popup will display above the combo box.
  • listening by PopupMenuListener

reduced version of the code given by Pshemo:

import javax.swing.JComboBox;
import java.awt.Dimension;

public class JComboBoxWider extends JComboBox {
    private int listWidth=0;

    public JComboBoxWider(Object[] objs) {
        super(objs);
    }

    public Dimension getSize() {
        Dimension dim = super.getSize();
        if (listWidth>0)
            dim.width = listWidth;
        return dim;
    }

    public void setListWidth(int listWidth) {
        this.listWidth = listWidth;
    }
}
Amit

You are not using Netbeans, drag and drop. Then Simply use :

JComboBox cb = new JComboBox();
cb.seBounds(10,10,200,30);

You are creating a JComboBox object but not fixing it's width and lengh whatever.. After creating an object of JComboBox use setBound() method. Like this:

JComboBox cb = new JComboBox();
cb.setBounds(10,10,200,30);

Warrio's answer worked for me. I added a bit to make a generic version.

import java.awt.Dimension;

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


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

    private static final long serialVersionUID = 1L;
    private int listWidth=0;

    public JComboBoxWider(ComboBoxModel<E> objs) {
        super(objs);
    }

    public Dimension getSize() {
        Dimension dim = super.getSize();
        if (listWidth > 0)
            dim.width = listWidth;
        return dim;
    }

    public void setListWidth(int listWidth) {
        this.listWidth = listWidth;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!