6
import javax.swing.*;

public class test
{   
    public static void main(String[] args) throws Exception
    {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(120,80);      
        JComboBox cb = new JComboBox();
        cb.addItem("A very long combo-box item that doesn't fit no. 1");
        cb.addItem("A very long combo-box item that doesn't fit no. 2");
        frame.add(cb);
        frame.validate();
        frame.setVisible(true);
    }
}

如何使组合框项目以所有文本可见的方式出现?
现在我有这样的事情:
在此处输入图像描述
我不想在折叠时更改组合框的大小。
我只想增加扩展部分的宽度。

4

6 回答 6

8

我认为可能会有所帮助。

来自链接的代码

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

    }
}
于 2012-06-30T22:54:31.440 回答
2

更复杂的解决方法是@camickr 的 Combo Box Popup,实现

  • setScrollBarRequired- 当为 true 时,会在必要时自动显示水平滚动条
  • setPopupWider– 如果为 true,则弹出窗口的宽度将基于组合框中的项目。宽度永远不会小于组合框。
  • setMaximumWidth- 可以控制弹出窗口的宽度,以防万一你有一个不合理的长项目要渲染。
  • setPopupAbove– 当为 true 时,弹出窗口将显示在组合框上方。
  • 听从PopupMenuListener
于 2012-06-30T23:09:19.567 回答
1

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;
    }
}
于 2014-03-19T16:32:30.103 回答
1

您没有使用 Netbeans,拖放。然后只需使用:

JComboBox cb = new JComboBox();
cb.seBounds(10,10,200,30);
于 2017-12-15T05:26:37.910 回答
1

您正在创建一个JComboBox对象,但没有固定它的宽度和长度。在创建JComboBox使用setBound()方法的对象之后。像这样:

JComboBox cb = new JComboBox();
cb.setBounds(10,10,200,30);
于 2017-12-15T05:55:48.270 回答
0

Warrio的回答对我有用。我添加了一些内容来制作通用版本。

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;
    }
}
于 2017-12-14T20:26:58.870 回答