比较笨的方法:
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;
}
}
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;
}
}
Popup类其实是copy BasicComboPopup,
然后注释掉
// scroller.setMaximumSize( scrollSize );
// scroller.setPreferredSize( scrollSize );
// scroller.setMinimumSize( scrollSize );
UI类看起来就跟简单了。
看看jdk源码就很容易明白的。