class LargerComboBoxUI extends BasicComboBoxUI {
public void installUI(JComponent comboBox) {
super.installUI(comboBox);
}
protected ComboPopup createPopup() {
return new LargerComboPopup(comboBox);
} public class LargerComboPopup extends BasicComboPopup {
public LargerComboPopup(JComboBox comboBox) {
super(comboBox);
} public void show() {
int selectedIndex = comboBox.getSelectedIndex();
if (selectedIndex == -1) {
list.clearSelection();
} else {
list.setSelectedIndex(selectedIndex);
list.ensureIndexIsVisible(selectedIndex);
} Insets insets = getInsets();
Dimension listDim = list.getPreferredSize();
boolean hasScrollBar = scroller.getViewport().getViewSize().height != listDim.height;
if (hasScrollBar) {
JScrollBar scrollBar = scroller.getVerticalScrollBar();
listDim.width += scrollBar.getPreferredSize().getWidth();
} int width = Math.max(listDim.width, comboBox.getWidth()
- (insets.right + insets.left));
int height = getPopupHeightForRowCount(comboBox
.getMaximumRowCount());
Rectangle popupBounds = computePopupBounds(0, comboBox.getHeight(),
width, height); Dimension scrollSize = popupBounds.getSize();
scroller.setMaximumSize(scrollSize);
scroller.setPreferredSize(scrollSize);
scroller.setMinimumSize(scrollSize); list.revalidate();
show(comboBox, popupBounds.x, popupBounds.y);
}
}
}这是重写的源代码
public void installUI(JComponent comboBox) {
super.installUI(comboBox);
}
protected ComboPopup createPopup() {
return new LargerComboPopup(comboBox);
} public class LargerComboPopup extends BasicComboPopup {
public LargerComboPopup(JComboBox comboBox) {
super(comboBox);
} public void show() {
int selectedIndex = comboBox.getSelectedIndex();
if (selectedIndex == -1) {
list.clearSelection();
} else {
list.setSelectedIndex(selectedIndex);
list.ensureIndexIsVisible(selectedIndex);
} Insets insets = getInsets();
Dimension listDim = list.getPreferredSize();
boolean hasScrollBar = scroller.getViewport().getViewSize().height != listDim.height;
if (hasScrollBar) {
JScrollBar scrollBar = scroller.getVerticalScrollBar();
listDim.width += scrollBar.getPreferredSize().getWidth();
} int width = Math.max(listDim.width, comboBox.getWidth()
- (insets.right + insets.left));
int height = getPopupHeightForRowCount(comboBox
.getMaximumRowCount());
Rectangle popupBounds = computePopupBounds(0, comboBox.getHeight(),
width, height); Dimension scrollSize = popupBounds.getSize();
scroller.setMaximumSize(scrollSize);
scroller.setPreferredSize(scrollSize);
scroller.setMinimumSize(scrollSize); list.revalidate();
show(comboBox, popupBounds.x, popupBounds.y);
}
}
}这是重写的源代码
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货