如何让JComboBox下来出来的菜单大于本身的宽度

小小都不懂 2010-04-14 04:15:52

1 如何设定了JComboBox宽度

2 如何让JComboBox下来出来的菜单大于本身的宽度!假设设定了JComboBox宽度,所以如果下拉选项的内容特别多,就显示不出来了,有没有办法让它都显示出来
...全文
341 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
约翰羊 2010-08-26
  • 打赏
  • 举报
回复


class MyComboBoxUI extends WindowsComboBoxUI {
protected ComboPopup createPopup() {
return new MyComboBoxPopup(comboBox);
}
}

class MyComboBoxPopup extends BasicComboPopup {
public MyComboBoxPopup(JComboBox combo) {
super(combo);
}
protected JScrollPane createScroller() {
JScrollPane sp = new JScrollPane( list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
return sp;
}

public void show() {
setListSelection(comboBox.getSelectedIndex());
Insets insets = getInsets();
int popupPrefWid = list.getPreferredSize().width + insets.left + insets.right;

Dimension scrollSize = new Dimension(
comboBox.getWidth(), getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
if (popupPrefWid > scrollSize.width) {
scrollSize.height += scroller.getHorizontalScrollBar().getPreferredSize().height;
}
scroller.setMaximumSize( scrollSize );
scroller.setPreferredSize( scrollSize );
scroller.setMinimumSize( scrollSize );
list.revalidate();
show( comboBox, 0, comboBox.getHeight());
}

private void setListSelection(int selectedIndex) {
if ( selectedIndex == -1 ) {
list.clearSelection();
}else{
list.setSelectedIndex(selectedIndex);
list.ensureIndexIsVisible(selectedIndex);
}
}
}


用的时候
jComboBox1.setUI(new MyComboBoxUI());

小小都不懂 2010-04-20
  • 打赏
  • 举报
回复
没人知道吗?
小小都不懂 2010-04-19
  • 打赏
  • 举报
回复
没人知道吗?
For_suzhen 2010-04-15
  • 打赏
  • 举报
回复
我也贴段代码。仿照html的。带Value的。可能不是很好。仅供参考


import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Vector;

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

public class WideComboBox extends JComboBox{

/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<String> arrvalue = new ArrayList<String>();
public WideComboBox() {
}

public WideComboBox(final Object items[], final String[] values){
super(items);
for(int i=0; i < values.length; i++){
this.arrvalue.add(values[i]);
}
}

public WideComboBox(Vector items) {
super(items);
}

public WideComboBox(ComboBoxModel aModel) {
super(aModel);
}
public void addItem(Object anObject,String value){
super.addItem(anObject);
this.arrvalue.add(value);
}
public String getValue(){
int x = this.getSelectedIndex();
return (String)arrvalue.get(x);
}
private boolean layingOut = false;

public void doLayout(){
try{
layingOut = true;
super.doLayout();
}finally{
layingOut = false;
}
}

public Dimension getSize(){
Dimension dim = super.getSize();
if(!layingOut)
dim.width = Math.max(dim.width, getPreferredSize().width);
return dim;
}
}







draem0507 2010-04-15
  • 打赏
  • 举报
回复


package april.test0414;

import java.awt.Dimension;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestJcombox extends JFrame{


String str[]={"aa","bb","cc","dd","ee"};
public TestJcombox() {
JComboBox box=new JComboBox(str);

box.setPreferredSize(new Dimension(10,20));//设置下拉框的高和宽
box.setEditable(false);

this.add(new JPanel().add(box));
this.pack();
this.setVisible(true);


}


public static void main(String[] args) {
new TestJcombox();
}


}



主要是这个方法setPreferredSize(new Dimension(int width,int heigh))
yueguangkai001 2010-04-15
  • 打赏
  • 举报
回复
getComponent(1)就是下面LIST的组建,getComponent(0)就是上面的按钮

有控件了,你干什么都应该不成问题
小小都不懂 2010-04-15
  • 打赏
  • 举报
回复
setPreferredSize(new Dimension(int width,int heigh))

这个只是设置Jcombox 的大小

我是想设置 它的下拉框里面的值的显示大小
小小都不懂 2010-04-14
  • 打赏
  • 举报
回复
自己顶吧
希望明天来了 能有人回答我!!!
小小都不懂 2010-04-14
  • 打赏
  • 举报
回复
设置大小OK了

那我的第二个问题呢?

大家帮帮忙!!!!!!

如何让JComboBox下来出来的菜单大于本身的宽度!假设设定了JComboBox宽度,所以如果下拉选项的内容特别多,就显示不出来了,有没有办法让它都显示出来
ZRBRZB 2010-04-14
  • 打赏
  • 举报
回复
class DateUI extends MetalComboBoxUI {
@Override
protected ComboPopup createPopup() {
BasicComboPopup bc = new BasicComboPopup(jcombobox);
bc.setPopupSize(x, y); // 设置大小
return new BasicComboPopup(jcombobox);
}
}
jcombobox.setUI(new DateUI());
代码不多,自己试试
James.Ji 2010-04-14
  • 打赏
  • 举报
回复
没做过UI的东西,把代码发出来看看,他自身应该有很多方法吧,这个setMaximumRowCount(int count) 是不是你想要的?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧