ComboBox: made popup list wider if list item texts are longer than combo box width

This commit is contained in:
Karl Tauber
2019-09-26 10:35:19 +02:00
parent 84dc96d41a
commit fbfde043b3
5 changed files with 64 additions and 0 deletions

View File

@@ -341,6 +341,22 @@ public class FlatComboBoxUI
super( combo );
}
@Override
protected Rectangle computePopupBounds( int px, int py, int pw, int ph ) {
// get maximum display size of all items, ignoring prototype value
Object prototype = comboBox.getPrototypeDisplayValue();
if( prototype != null )
comboBox.setPrototypeDisplayValue( null );
Dimension displaySize = getDisplaySize();
if( prototype != null )
comboBox.setPrototypeDisplayValue( prototype );
// make popup wider if necessary
pw = Math.max( pw, displaySize.width );
return super.computePopupBounds( px, py, pw, ph );
}
@Override
protected void configurePopup() {
super.configurePopup();

View File

@@ -75,6 +75,7 @@ public class FlatComponentsTest
JComboBox<String> comboBox2 = new JComboBox<>();
JComboBox<String> comboBox3 = new JComboBox<>();
JComboBox<String> comboBox4 = new JComboBox<>();
JComboBox<String> comboBox5 = new JComboBox<>();
JLabel spinnerLabel = new JLabel();
JSpinner spinner1 = new JSpinner();
JSpinner spinner2 = new JSpinner();
@@ -365,6 +366,16 @@ public class FlatComponentsTest
comboBox4.setEnabled(false);
add(comboBox4, "cell 4 5,growx");
//---- comboBox5 ----
comboBox5.setPrototypeDisplayValue("12345");
comboBox5.setModel(new DefaultComboBoxModel<>(new String[] {
"wide popup if text is longer",
"aa",
"bbb",
"cccc"
}));
add(comboBox5, "cell 5 5,growx");
//---- spinnerLabel ----
spinnerLabel.setText("JSpinner:");
add(spinnerLabel, "cell 0 6");

View File

@@ -240,6 +240,19 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 5,growx"
} )
add( new FormComponent( "javax.swing.JComboBox" ) {
name: "comboBox5"
"prototypeDisplayValue": "12345"
"model": new javax.swing.DefaultComboBoxModel {
selectedItem: "wide popup if text is longer"
addElement( "wide popup if text is longer" )
addElement( "aa" )
addElement( "bbb" )
addElement( "cccc" )
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 5,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "spinnerLabel"
"text": "JSpinner:"