fixed component heights at 1.25x, 1.75x and 2.25x scaling factors (Java 8 only) so that Button, ComboBox, Spinner and TextField components (including subclasses) have same heights

This commit is contained in:
Karl Tauber
2021-07-05 15:06:06 +02:00
parent 0b127caa83
commit b576f473e5
7 changed files with 92 additions and 61 deletions

View File

@@ -18,6 +18,12 @@ FlatLaf Change Log
of text now positions caret to first character instead of opening ComboBox
popup; mouse cursor is now of type "text" within the whole component, except
for arrow buttons). (issue #330)
- Fixed component heights at `1.25x`, `1.75x` and `2.25x` scaling factors (Java
8 only) so that Button, ComboBox, Spinner and TextField components (including
subclasses) have same heights. This increases heights of Button and TextField
components by:
- `2px` at `1.75x` in **Light** and **Dark** themes
- `2px` at `1.25x` and `2.25x` in **IntelliJ** and **Darcula** themes
## 1.3

View File

@@ -23,8 +23,8 @@ plugins {
dependencies {
testImplementation( "org.junit.jupiter:junit-jupiter-api:5.7.2" )
testImplementation( "org.junit.jupiter:junit-jupiter-params" )
testRuntimeOnly( "org.junit.jupiter:junit-jupiter-engine" )
testRuntimeOnly( "org.junit.jupiter:junit-jupiter-params" )
}
java {

View File

@@ -176,13 +176,14 @@ public class FlatBorder
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
float focusWidth = scale( (float) getFocusWidth( c ) );
float ow = focusWidth + scale( (float) getLineWidth( c ) );
int ow = Math.round( focusWidth + scale( (float) getLineWidth( c ) ) );
insets = super.getBorderInsets( c, insets );
insets.top = Math.round( scale( (float) insets.top ) + ow );
insets.left = Math.round( scale( (float) insets.left ) + ow );
insets.bottom = Math.round( scale( (float) insets.bottom ) + ow );
insets.right = Math.round( scale( (float) insets.right ) + ow );
insets.top = scale( insets.top ) + ow;
insets.left = scale( insets.left ) + ow;
insets.bottom = scale( insets.bottom ) + ow;
insets.right = scale( insets.right ) + ow;
if( isCellEditor( c ) ) {
// remove top and bottom insets if used as cell editor

View File

@@ -49,7 +49,7 @@ public class TestFlatComponentSizes
}
static float[] factors() {
return new float[] { 1f, 1.25f, 1.5f, 1.75f, 2f, 2.25f, 2.5f, 3f, 4f };
return TestUtils.FACTORS;
}
@ParameterizedTest
@@ -57,12 +57,6 @@ public class TestFlatComponentSizes
void sizes( float factor ) {
TestUtils.scaleFont( factor );
// TODO on some scale factors the combobox/spinner sizes are slightly different
// because different size calculation methods and rounding
boolean testComboBoxAndSpinner = (UIManager.getInt( "Component.focusWidth" ) > 0)
? (factor != 1.25 && factor != 2.25)
: (factor != 1.75);
// should have same default size (minimumWidth is 64)
JTextField textField = new JTextField();
@@ -73,8 +67,7 @@ public class TestFlatComponentSizes
Dimension textFieldSize = textField.getPreferredSize();
assertEquals( textFieldSize, formattedTextField.getPreferredSize() );
assertEquals( textFieldSize, passwordField.getPreferredSize() );
if( testComboBoxAndSpinner )
assertEquals( textFieldSize, spinner.getPreferredSize() );
assertEquals( textFieldSize, spinner.getPreferredSize() );
// should have same default size (minimumWidth is 72)
@@ -84,10 +77,8 @@ public class TestFlatComponentSizes
comboBoxEditable.setEditable( true );
Dimension buttonSize = button.getPreferredSize();
if( testComboBoxAndSpinner ) {
assertEquals( buttonSize, comboBox.getPreferredSize() );
assertEquals( buttonSize, comboBoxEditable.getPreferredSize() );
}
assertEquals( buttonSize, comboBox.getPreferredSize() );
assertEquals( buttonSize, comboBoxEditable.getPreferredSize() );
// should have same height

View File

@@ -27,6 +27,8 @@ import com.formdev.flatlaf.FlatSystemProperties;
*/
public class TestUtils
{
public static final float[] FACTORS = new float[] { 1f, 1.25f, 1.5f, 1.75f, 2f, 2.25f, 2.5f, 2.75f, 3f, 3.25f, 3.5f, 3.75f, 4f, 5f, 6f };
public static void setup( boolean withFocus ) {
System.setProperty( FlatSystemProperties.UI_SCALE, "1x" );
if( withFocus )

View File

@@ -51,6 +51,7 @@ public class FlatBaselineTest
JFormattedTextField formattedTextField1 = new JFormattedTextField();
JPasswordField passwordField1 = new JPasswordField();
JComboBox<String> comboBox1 = new JComboBox<>();
JComboBox<String> comboBox2 = new JComboBox<>();
JSpinner spinner1 = new JSpinner();
JLabel label6 = new JLabel();
JScrollPane scrollPane1 = new JScrollPane();
@@ -88,7 +89,7 @@ public class FlatBaselineTest
//======== this ========
setLayout(new MigLayout(
"insets dialog,hidemode 3,debug",
"insets dialog,hidemode 3",
// columns
"[fill]" +
"[fill]" +
@@ -96,6 +97,7 @@ public class FlatBaselineTest
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]",
// rows
"[]" +
@@ -130,7 +132,7 @@ public class FlatBaselineTest
//---- textField4 ----
textField4.setText("Dext field");
add(textField4, "cell 6 0");
add(textField4, "cell 7 0");
//---- label2 ----
label2.setText("Dext");
@@ -147,8 +149,20 @@ public class FlatBaselineTest
//---- passwordField1 ----
passwordField1.setText("Dext");
add(passwordField1, "cell 3 1");
//---- comboBox1 ----
comboBox1.setModel(new DefaultComboBoxModel<>(new String[] {
"Dext"
}));
add(comboBox1, "cell 4 1");
add(spinner1, "cell 5 1");
//---- comboBox2 ----
comboBox2.setModel(new DefaultComboBoxModel<>(new String[] {
"Dext"
}));
comboBox2.setEditable(true);
add(comboBox2, "cell 5 1");
add(spinner1, "cell 6 1");
//---- label6 ----
label6.setText("Dext");
@@ -171,7 +185,7 @@ public class FlatBaselineTest
//---- textField2 ----
textField2.setText("Dext field");
add(textField2, "cell 6 2");
add(textField2, "cell 7 2");
//---- label7 ----
label7.setText("Dext");
@@ -230,18 +244,18 @@ public class FlatBaselineTest
//---- textField3 ----
textField3.setText("Dext field");
add(textField3, "cell 6 3");
add(textField3, "cell 7 3");
//---- label3 ----
label3.setText("Dext");
add(label3, "cell 0 4");
add(slider1, "cell 1 4 6 1");
add(slider1, "cell 1 4 7 1");
//---- slider6 ----
slider6.setPaintTicks(true);
slider6.setMajorTickSpacing(25);
slider6.setMinorTickSpacing(5);
add(slider6, "cell 1 4 6 1");
add(slider6, "cell 1 4 7 1");
//---- label8 ----
label8.setText("Dext");
@@ -251,14 +265,14 @@ public class FlatBaselineTest
slider7.setPaintLabels(true);
slider7.setMajorTickSpacing(25);
slider7.setMinorTickSpacing(5);
add(slider7, "cell 1 5 6 1");
add(slider7, "cell 1 5 7 1");
//---- slider8 ----
slider8.setPaintLabels(true);
slider8.setPaintTicks(true);
slider8.setMajorTickSpacing(25);
slider8.setMinorTickSpacing(5);
add(slider8, "cell 1 5 6 1");
add(slider8, "cell 1 5 7 1");
//---- label4 ----
label4.setText("Dext");
@@ -266,13 +280,13 @@ public class FlatBaselineTest
//---- progressBar1 ----
progressBar1.setValue(30);
add(progressBar1, "cell 1 6 6 1");
add(progressBar1, "cell 1 6 7 1");
//---- progressBar3 ----
progressBar3.setStringPainted(true);
progressBar3.setValue(30);
add(progressBar3, "cell 1 6 6 1");
add(separator1, "cell 1 6 6 1");
add(progressBar3, "cell 1 6 7 1");
add(separator1, "cell 1 6 7 1");
//---- label5 ----
label5.setText("Dext");
@@ -280,26 +294,26 @@ public class FlatBaselineTest
//---- slider2 ----
slider2.setOrientation(SwingConstants.VERTICAL);
add(slider2, "cell 1 7 6 1");
add(slider2, "cell 1 7 7 1");
//---- slider3 ----
slider3.setOrientation(SwingConstants.VERTICAL);
slider3.setPaintTicks(true);
slider3.setMajorTickSpacing(25);
slider3.setMinorTickSpacing(5);
add(slider3, "cell 1 7 6 1");
add(slider3, "cell 1 7 7 1");
//---- progressBar2 ----
progressBar2.setOrientation(SwingConstants.VERTICAL);
progressBar2.setValue(30);
add(progressBar2, "cell 1 7 6 1");
add(progressBar2, "cell 1 7 7 1");
//---- progressBar4 ----
progressBar4.setOrientation(SwingConstants.VERTICAL);
progressBar4.setStringPainted(true);
progressBar4.setValue(30);
add(progressBar4, "cell 1 7 6 1");
add(hSpacer1, "cell 1 7 6 1,growx");
add(progressBar4, "cell 1 7 7 1");
add(hSpacer1, "cell 1 7 7 1,growx");
//---- label9 ----
label9.setText("Dext");
@@ -310,7 +324,7 @@ public class FlatBaselineTest
slider4.setPaintLabels(true);
slider4.setMajorTickSpacing(25);
slider4.setMinorTickSpacing(5);
add(slider4, "cell 1 8 6 1");
add(slider4, "cell 1 8 7 1");
//---- slider5 ----
slider5.setOrientation(SwingConstants.VERTICAL);
@@ -318,8 +332,8 @@ public class FlatBaselineTest
slider5.setPaintTicks(true);
slider5.setMajorTickSpacing(25);
slider5.setMinorTickSpacing(5);
add(slider5, "cell 1 8 6 1");
add(hSpacer2, "cell 1 8 6 1,growx");
add(slider5, "cell 1 8 7 1");
add(hSpacer2, "cell 1 8 7 1,growx");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.2.0.298" Java: "15" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.4.0.360" Java: "16" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -7,8 +7,8 @@ new FormModel {
"JavaCodeGenerator.defaultVariableLocal": true
}
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets dialog,hidemode 3,debug"
"$columnConstraints": "[fill][fill][fill][fill][fill][fill][fill]"
"$layoutConstraints": "insets dialog,hidemode 3"
"$columnConstraints": "[fill][fill][fill][fill][fill][fill][fill][fill]"
"$rowConstraints": "[][][50][::80][][][][][]"
} ) {
name: "this"
@@ -46,7 +46,7 @@ new FormModel {
name: "textField4"
"text": "Dext field"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 6 0"
"value": "cell 7 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label2"
@@ -74,16 +74,33 @@ new FormModel {
} )
add( new FormComponent( "javax.swing.JComboBox" ) {
name: "comboBox1"
"model": new javax.swing.DefaultComboBoxModel {
selectedItem: "Dext"
addElement( "Dext" )
}
auxiliary() {
"JavaCodeGenerator.typeParameters": "String"
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 4 1"
} )
add( new FormComponent( "javax.swing.JComboBox" ) {
name: "comboBox2"
"model": new javax.swing.DefaultComboBoxModel {
selectedItem: "Dext"
addElement( "Dext" )
}
"editable": true
auxiliary() {
"JavaCodeGenerator.typeParameters": "String"
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 1"
} )
add( new FormComponent( "javax.swing.JSpinner" ) {
name: "spinner1"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 1"
"value": "cell 6 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label6"
@@ -112,7 +129,7 @@ new FormModel {
name: "textField2"
"text": "Dext field"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 6 2"
"value": "cell 7 2"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label7"
@@ -184,7 +201,7 @@ new FormModel {
name: "textField3"
"text": "Dext field"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 6 3"
"value": "cell 7 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label3"
@@ -195,7 +212,7 @@ new FormModel {
add( new FormComponent( "javax.swing.JSlider" ) {
name: "slider1"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4 6 1"
"value": "cell 1 4 7 1"
} )
add( new FormComponent( "javax.swing.JSlider" ) {
name: "slider6"
@@ -203,7 +220,7 @@ new FormModel {
"majorTickSpacing": 25
"minorTickSpacing": 5
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4 6 1"
"value": "cell 1 4 7 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label8"
@@ -217,7 +234,7 @@ new FormModel {
"majorTickSpacing": 25
"minorTickSpacing": 5
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 5 6 1"
"value": "cell 1 5 7 1"
} )
add( new FormComponent( "javax.swing.JSlider" ) {
name: "slider8"
@@ -226,7 +243,7 @@ new FormModel {
"majorTickSpacing": 25
"minorTickSpacing": 5
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 5 6 1"
"value": "cell 1 5 7 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label4"
@@ -238,19 +255,19 @@ new FormModel {
name: "progressBar1"
"value": 30
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 6 6 1"
"value": "cell 1 6 7 1"
} )
add( new FormComponent( "javax.swing.JProgressBar" ) {
name: "progressBar3"
"stringPainted": true
"value": 30
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 6 6 1"
"value": "cell 1 6 7 1"
} )
add( new FormComponent( "javax.swing.JSeparator" ) {
name: "separator1"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 6 6 1"
"value": "cell 1 6 7 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label5"
@@ -262,7 +279,7 @@ new FormModel {
name: "slider2"
"orientation": 1
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 7 6 1"
"value": "cell 1 7 7 1"
} )
add( new FormComponent( "javax.swing.JSlider" ) {
name: "slider3"
@@ -271,14 +288,14 @@ new FormModel {
"majorTickSpacing": 25
"minorTickSpacing": 5
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 7 6 1"
"value": "cell 1 7 7 1"
} )
add( new FormComponent( "javax.swing.JProgressBar" ) {
name: "progressBar2"
"orientation": 1
"value": 30
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 7 6 1"
"value": "cell 1 7 7 1"
} )
add( new FormComponent( "javax.swing.JProgressBar" ) {
name: "progressBar4"
@@ -286,12 +303,12 @@ new FormModel {
"stringPainted": true
"value": 30
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 7 6 1"
"value": "cell 1 7 7 1"
} )
add( new FormComponent( "com.jformdesigner.designer.wrapper.HSpacer" ) {
name: "hSpacer1"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 7 6 1,growx"
"value": "cell 1 7 7 1,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label9"
@@ -306,7 +323,7 @@ new FormModel {
"majorTickSpacing": 25
"minorTickSpacing": 5
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 8 6 1"
"value": "cell 1 8 7 1"
} )
add( new FormComponent( "javax.swing.JSlider" ) {
name: "slider5"
@@ -316,12 +333,12 @@ new FormModel {
"majorTickSpacing": 25
"minorTickSpacing": 5
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 8 6 1"
"value": "cell 1 8 7 1"
} )
add( new FormComponent( "com.jformdesigner.designer.wrapper.HSpacer" ) {
name: "hSpacer2"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 8 6 1,growx"
"value": "cell 1 8 7 1,growx"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )