ComboBox, Spinner and TextField: support round border style (set client property JComponent.roundRect to true)

This commit is contained in:
Karl Tauber
2020-05-15 13:38:45 +02:00
parent 26c77b3118
commit 83fdeb7e0c
12 changed files with 107 additions and 8 deletions

View File

@@ -7,6 +7,8 @@ FlatLaf Change Log
frames. (issue #94) frames. (issue #94)
- Button and ToggleButton: Support round button style (set client property - Button and ToggleButton: Support round button style (set client property
`JButton.buttonType` to `roundRect`). `JButton.buttonType` to `roundRect`).
- ComboBox, Spinner and TextField: Support round border style (set client
property `JComponent.roundRect` to `true`).
- Paint nicely rounded buttons, comboboxes, spinners and text fields when - Paint nicely rounded buttons, comboboxes, spinners and text fields when
setting `Button.arc`, `Component.arc` or `TextComponent.arc` to a large value setting `Button.arc`, `Component.arc` or `TextComponent.arc` to a large value
(e.g. 1000). (e.g. 1000).

View File

@@ -103,6 +103,15 @@ public interface FlatClientProperties
*/ */
String MINIMUM_HEIGHT = "JComponent.minimumHeight"; String MINIMUM_HEIGHT = "JComponent.minimumHeight";
/**
* Paint the component with round edges.
* <p>
* <strong>Components</strong> {@link javax.swing.JComboBox}, {@link javax.swing.JSpinner},
* {@link javax.swing.JTextField}, {@link javax.swing.JFormattedTextField} and {@link javax.swing.JPasswordField}
* <strong>Value type</strong> {@link java.lang.Boolean}
*/
String COMPONENT_ROUND_RECT = "JComponent.roundRect";
/** /**
* Specifies whether a drop shadow is painted if the component is shown in a popup * Specifies whether a drop shadow is painted if the component is shown in a popup
* or if the component is the owner of another component that is shown in a popup. * or if the component is the owner of another component that is shown in a popup.

View File

@@ -208,6 +208,10 @@ public class FlatButtonUI
case MINIMUM_HEIGHT: case MINIMUM_HEIGHT:
b.revalidate(); b.revalidate();
break; break;
case BUTTON_TYPE:
b.repaint();
break;
} }
} }

View File

@@ -243,6 +243,8 @@ public class FlatComboBoxUI
editor.applyComponentOrientation( o ); editor.applyComponentOrientation( o );
} else if( editor != null && FlatClientProperties.PLACEHOLDER_TEXT.equals( propertyName ) ) } else if( editor != null && FlatClientProperties.PLACEHOLDER_TEXT.equals( propertyName ) )
editor.repaint(); editor.repaint();
else if( FlatClientProperties.COMPONENT_ROUND_RECT.equals( propertyName ) )
comboBox.repaint();
} }
}; };
} }

View File

@@ -34,7 +34,6 @@ import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPasswordFieldUI; import javax.swing.plaf.basic.BasicPasswordFieldUI;
import javax.swing.text.Caret; import javax.swing.text.Caret;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
import com.formdev.flatlaf.FlatClientProperties;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JPasswordField}. * Provides the Flat LaF UI delegate for {@link javax.swing.JPasswordField}.
@@ -147,9 +146,7 @@ public class FlatPasswordFieldUI
@Override @Override
protected void propertyChange( PropertyChangeEvent e ) { protected void propertyChange( PropertyChangeEvent e ) {
super.propertyChange( e ); super.propertyChange( e );
FlatTextFieldUI.propertyChange( getComponent(), e );
if( FlatClientProperties.PLACEHOLDER_TEXT.equals( e.getPropertyName() ) )
getComponent().repaint();
} }
@Override @Override

View File

@@ -33,6 +33,6 @@ public class FlatRoundBorder
@Override @Override
protected int getArc( Component c ) { protected int getArc( Component c ) {
return arc; return FlatUIUtils.isRoundRect( c ) ? Short.MAX_VALUE : arc;
} }
} }

View File

@@ -40,6 +40,7 @@ import javax.swing.SwingConstants;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicSpinnerUI; import javax.swing.plaf.basic.BasicSpinnerUI;
import com.formdev.flatlaf.FlatClientProperties;
/** /**
* Provides the Flat LaF UI delegate for {@link javax.swing.JSpinner}. * Provides the Flat LaF UI delegate for {@link javax.swing.JSpinner}.
@@ -394,6 +395,10 @@ public class FlatSpinnerUI
case "enabled": case "enabled":
updateEditorColors(); updateEditorColors();
break; break;
case FlatClientProperties.COMPONENT_ROUND_RECT:
spinner.repaint();
break;
} }
} }
} }

View File

@@ -33,6 +33,6 @@ public class FlatTextBorder
@Override @Override
protected int getArc( Component c ) { protected int getArc( Component c ) {
return arc; return FlatUIUtils.isRoundRect( c ) ? Short.MAX_VALUE : arc;
} }
} }

View File

@@ -127,9 +127,16 @@ public class FlatTextFieldUI
@Override @Override
protected void propertyChange( PropertyChangeEvent e ) { protected void propertyChange( PropertyChangeEvent e ) {
super.propertyChange( e ); super.propertyChange( e );
propertyChange( getComponent(), e );
}
if( FlatClientProperties.PLACEHOLDER_TEXT.equals( e.getPropertyName() ) ) static void propertyChange( JTextComponent c, PropertyChangeEvent e ) {
getComponent().repaint(); switch( e.getPropertyName() ) {
case FlatClientProperties.PLACEHOLDER_TEXT:
case FlatClientProperties.COMPONENT_ROUND_RECT:
c.repaint();
break;
}
} }
@Override @Override

View File

@@ -143,6 +143,11 @@ public class FlatUIUtils
return (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() == c); return (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() == c);
} }
public static boolean isRoundRect( Component c ) {
return c instanceof JComponent && FlatClientProperties.clientPropertyBoolean(
(JComponent) c, FlatClientProperties.COMPONENT_ROUND_RECT, false );
}
/** /**
* Returns the scaled thickness of the outer focus border for the given component. * Returns the scaled thickness of the outer focus border for the given component.
*/ */

View File

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.testing;
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.*; import javax.swing.border.*;
import com.formdev.flatlaf.FlatClientProperties;
import net.miginfocom.swing.*; import net.miginfocom.swing.*;
/** /**
@@ -63,6 +64,26 @@ public class FlatComponentsTest
} }
} }
private void roundRectChanged() {
Boolean roundRect = roundRectCheckBox.isSelected() ? true : null;
for( Component c : getComponents() ) {
if( c instanceof JComponent )
((JComponent)c).putClientProperty( FlatClientProperties.COMPONENT_ROUND_RECT, roundRect );
}
}
private void buttonTypeChanged() {
String buttonType = (String) buttonTypeComboBox.getSelectedItem();
if( "-".equals( buttonType ) )
buttonType = null;
for( Component c : getComponents() ) {
if( c instanceof AbstractButton )
((AbstractButton)c).putClientProperty( FlatClientProperties.BUTTON_TYPE, buttonType );
}
}
private void initComponents() { private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JLabel labelLabel = new JLabel(); JLabel labelLabel = new JLabel();
@@ -104,12 +125,14 @@ public class FlatComponentsTest
JCheckBox checkBox4 = new JCheckBox(); JCheckBox checkBox4 = new JCheckBox();
JToggleButton toggleButton5 = new JToggleButton(); JToggleButton toggleButton5 = new JToggleButton();
JToggleButton toggleButton8 = new JToggleButton(); JToggleButton toggleButton8 = new JToggleButton();
buttonTypeComboBox = new JComboBox<>();
JLabel radioButtonLabel = new JLabel(); JLabel radioButtonLabel = new JLabel();
JRadioButton radioButton1 = new JRadioButton(); JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton(); JRadioButton radioButton2 = new JRadioButton();
JRadioButton radioButton3 = new JRadioButton(); JRadioButton radioButton3 = new JRadioButton();
JRadioButton radioButton4 = new JRadioButton(); JRadioButton radioButton4 = new JRadioButton();
contentAreaFilledCheckBox = new JCheckBox(); contentAreaFilledCheckBox = new JCheckBox();
roundRectCheckBox = new JCheckBox();
JLabel comboBoxLabel = new JLabel(); JLabel comboBoxLabel = new JLabel();
JComboBox<String> comboBox1 = new JComboBox<>(); JComboBox<String> comboBox1 = new JComboBox<>();
JComboBox<String> comboBox2 = new JComboBox<>(); JComboBox<String> comboBox2 = new JComboBox<>();
@@ -481,6 +504,17 @@ public class FlatComponentsTest
toggleButton8.setSelected(true); toggleButton8.setSelected(true);
add(toggleButton8, "cell 5 3"); add(toggleButton8, "cell 5 3");
//---- buttonTypeComboBox ----
buttonTypeComboBox.setModel(new DefaultComboBoxModel<>(new String[] {
"-",
"square",
"roundRect",
"tab",
"help"
}));
buttonTypeComboBox.addActionListener(e -> buttonTypeChanged());
add(buttonTypeComboBox, "cell 6 3");
//---- radioButtonLabel ---- //---- radioButtonLabel ----
radioButtonLabel.setText("JRadioButton:"); radioButtonLabel.setText("JRadioButton:");
add(radioButtonLabel, "cell 0 4"); add(radioButtonLabel, "cell 0 4");
@@ -513,6 +547,11 @@ public class FlatComponentsTest
contentAreaFilledCheckBox.addActionListener(e -> contentAreaFilledChanged()); contentAreaFilledCheckBox.addActionListener(e -> contentAreaFilledChanged());
add(contentAreaFilledCheckBox, "cell 5 4"); add(contentAreaFilledCheckBox, "cell 5 4");
//---- roundRectCheckBox ----
roundRectCheckBox.setText("roundRect");
roundRectCheckBox.addActionListener(e -> roundRectChanged());
add(roundRectCheckBox, "cell 6 4");
//---- comboBoxLabel ---- //---- comboBoxLabel ----
comboBoxLabel.setText("JComboBox:"); comboBoxLabel.setText("JComboBox:");
add(comboBoxLabel, "cell 0 5"); add(comboBoxLabel, "cell 0 5");
@@ -1210,7 +1249,9 @@ public class FlatComponentsTest
} }
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JComboBox<String> buttonTypeComboBox;
private JCheckBox contentAreaFilledCheckBox; private JCheckBox contentAreaFilledCheckBox;
private JCheckBox roundRectCheckBox;
private JProgressBar progressBar3; private JProgressBar progressBar3;
private JProgressBar progressBar4; private JProgressBar progressBar4;
private JSlider slider3; private JSlider slider3;

View File

@@ -296,6 +296,23 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 3" "value": "cell 5 3"
} ) } )
add( new FormComponent( "javax.swing.JComboBox" ) {
name: "buttonTypeComboBox"
"model": new javax.swing.DefaultComboBoxModel {
selectedItem: "-"
addElement( "-" )
addElement( "square" )
addElement( "roundRect" )
addElement( "tab" )
addElement( "help" )
}
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "buttonTypeChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 6 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) { add( new FormComponent( "javax.swing.JLabel" ) {
name: "radioButtonLabel" name: "radioButtonLabel"
"text": "JRadioButton:" "text": "JRadioButton:"
@@ -343,6 +360,16 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 4" "value": "cell 5 4"
} ) } )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "roundRectCheckBox"
"text": "roundRect"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "roundRectChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 6 4"
} )
add( new FormComponent( "javax.swing.JLabel" ) { add( new FormComponent( "javax.swing.JLabel" ) {
name: "comboBoxLabel" name: "comboBoxLabel"
"text": "JComboBox:" "text": "JComboBox:"