1 如何设定了JComboBox宽度2 如何让JComboBox下来出来的菜单大于本身的宽度!假设设定了JComboBox宽度,所以如果下拉选项的内容特别多,就显示不出来了,有没有办法让它都显示出来
解决方案 »
- 在JAVA中,不用API中的函数,如何将String类型的转换成int类型数据,急急急!
- 问个基础问题,大家表笑。。。
- 如何通过给定日期格式的字符串得到Date对象
- 怎样让自定义的组件支持 shift & ctrl 的多选
- Java内部使用的是utf-8吗?
- 用C语言编写的typedef unsigned char BYTE;在java中怎么表示,谢谢了!
- 关于jb8显示字符的问题
- java与mfc的通讯问题!
- 推荐:<Jakata Poi HSSF:纯java的Excel解决方案>
- 如何将我要在APPLET中添加的LABEL放置到一定的位置(我的分不多)
- 帮忙引导引导
- 求一款免费好用的GUI画图工具
@Override
protected ComboPopup createPopup() {
BasicComboPopup bc = new BasicComboPopup(jcombobox);
bc.setPopupSize(x, y); // 设置大小
return new BasicComboPopup(jcombobox);
}
}
jcombobox.setUI(new DateUI());
代码不多,自己试试
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))
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;
}
}
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());