如何使JComboBox的下拉部分比它自己的框宽啊?原来默认是一样宽的!(高分)

zswu 2004-08-05 10:44:48
如何使JComboBox的下拉部分比它自己的框宽啊?原来默认是一样宽的!(高分)
...全文
134 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zswu 2004-08-12
  • 打赏
  • 举报
回复
多谢,功能已经实现,不过下拉框回车后,样子办的很难看
nwpulipeng 2004-08-09
  • 打赏
  • 举报
回复
这也能实现么???有点不可思议
Acylas 2004-08-09
  • 打赏
  • 举报
回复
比较笨的方法:
public void actionPerformed(ActionEvent event) {
UI ui = new UI();
jComboBox1.setUI(ui);
((Popup)ui.getPopup()).setDisplaySize(100, 50);
}

class UI extends javax.swing.plaf.basic.BasicComboBoxUI {
protected javax.swing.plaf.basic.ComboPopup createPopup() {
Popup popup = new Popup( comboBox );
popup.getAccessibleContext().setAccessibleParent(comboBox);
return popup;
}
public javax.swing.plaf.basic.ComboPopup getPopup() {
return popup;
}
}

class Popup extends javax.swing.plaf.basic.BasicComboPopup {
public Popup(JComboBox combo) {
super(combo);
}
public void setDisplaySize(int width, int height) {
scroller.setSize(width, height);
scroller.setPreferredSize(new Dimension(width, height));
}
public void show() {
setListSelection(comboBox.getSelectedIndex());
Point location = getPopupLocation();
show( comboBox, location.x, location.y );
}
private void setListSelection(int selectedIndex) {
if ( selectedIndex == -1 ) {
list.clearSelection();
}
else {
list.setSelectedIndex( selectedIndex );
list.ensureIndexIsVisible( selectedIndex );
}
}

private Point getPopupLocation() {
Dimension popupSize = comboBox.getSize();
Insets insets = getInsets();

// reduce the width of the scrollpane by the insets so that the popup
// is the same width as the combo box.
popupSize.setSize(popupSize.width - (insets.right + insets.left),
getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
Rectangle popupBounds = computePopupBounds( 0, comboBox.getBounds().height,
popupSize.width, popupSize.height);
Dimension scrollSize = popupBounds.getSize();
Point popupLocation = popupBounds.getLocation();

// scroller.setMaximumSize( scrollSize );
// scroller.setPreferredSize( scrollSize );
// scroller.setMinimumSize( scrollSize );

list.revalidate();

return popupLocation;
}
}
zswu 2004-08-09
  • 打赏
  • 举报
回复
还没高手么,100分都没人要么?
Acylas 2004-08-09
  • 打赏
  • 举报
回复
肯定可以实现楼主想要的,原理很简单:
Popup类其实是copy BasicComboPopup,
然后注释掉
// scroller.setMaximumSize( scrollSize );
// scroller.setPreferredSize( scrollSize );
// scroller.setMinimumSize( scrollSize );
UI类看起来就跟简单了。
看看jdk源码就很容易明白的。
icy_csdn 2004-08-05
  • 打赏
  • 举报
回复
没见过,是不是根布局管理器有关?

要不贴点代码看看:)

62,614

社区成员

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

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