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);
}
}
如何使组合框项目以其所有文本可见的方式显示?
现在我有这样的事情:
我不想在折叠时改变组合框的大小
我只是想增加扩展部分的宽度。
答案 0 :(得分:7)
我认为this可能有所帮助。
来自链接的代码
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);
}
}
答案 1 :(得分:2)
更复杂的解决方法是Combo Box Popup by @camickr,实现
setScrollBarRequired
- 如果为true,则会在必要时自动显示水平滚动条setPopupWider
- 如果为true,则弹出窗口的宽度将基于组合框中的项目。宽度永远不会小于组合框。setMaximumWidth
- 可以控制弹出窗口的宽度,以防万一你有一个不合理的长项目。setPopupAbove
- 如果为true,弹出窗口将显示在组合框上方。PopupMenuListener
答案 2 :(得分: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;
}
}
答案 3 :(得分: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;
}
}
答案 4 :(得分:0)
您没有使用Netbeans,拖放。然后简单地使用:
JComboBox cb = new JComboBox();
cb.seBounds(10,10,200,30);
答案 5 :(得分:0)
您正在创建一个JComboBox
对象但不修复它的宽度和长度等等。
创建JComboBox
使用setBound()
方法的对象后。像这样:
JComboBox cb = new JComboBox();
cb.setBounds(10,10,200,30);