Skip to content

Commit

Permalink
FlatComponentsTest: added "transparent background" checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
DevCharly committed Dec 9, 2024
1 parent 6a774d8 commit 145631f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ColorUIResource;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.extras.components.*;
import com.formdev.flatlaf.extras.components.FlatButton.ButtonType;
Expand Down Expand Up @@ -144,6 +145,36 @@ private void hideArrowButton() {
}
}

private void transparentBackground() {
EventQueue.invokeLater( () -> {
boolean transparent = transparentBackgroundCheckBox.isSelected();
Color transparentColor = new Color( 0, true );
ColorUIResource restoreColor = new ColorUIResource( Color.white );

FlatTestFrame.updateComponentsRecur( this, (c, type) -> {
if( c instanceof JComboBox ) {
((JComboBox<?>)c).putClientProperty( FlatClientProperties.STYLE,
transparent ? "background: #0000; buttonBackground: #0000; buttonEditableBackground: #0000" : null );
} else if( c instanceof JSpinner ) {
((JSpinner)c).putClientProperty( FlatClientProperties.STYLE,
transparent ? "background: #0000; buttonBackground: #0000" : null );
} else if( c instanceof JComponent )
c.setBackground( transparent ? transparentColor : restoreColor );
else
return;

// if background color is transparent it is also required to make the component non-opaque
// DO NOT USE LookAndFeel.installProperty() in real-world applications
// instead use c.setOpaque( false )
if( transparent )
LookAndFeel.installProperty( (JComponent) c, "opaque", false );
} );

if( !transparent )
SwingUtilities.updateComponentTreeUI( this );
} );
}

private void roundRectChanged() {
Boolean roundRect = roundRectCheckBox.isSelected() ? true : null;

Expand Down Expand Up @@ -398,6 +429,7 @@ private void initComponents() {
magentaCyanOutlineRadioButton = new JRadioButton();
focusPaintedCheckBox = new JCheckBox();
hideArrowButtonCheckBox = new JCheckBox();
transparentBackgroundCheckBox = new JCheckBox();
JLabel scrollBarLabel = new JLabel();
JScrollBar scrollBar1 = new JScrollBar();
JScrollBar scrollBar4 = new JScrollBar();
Expand Down Expand Up @@ -1256,6 +1288,7 @@ private void initComponents() {
"[]0" +
"[]0" +
"[]0" +
"[]0" +
"[]"));

//---- buttonTypeComboBox ----
Expand Down Expand Up @@ -1309,7 +1342,7 @@ private void initComponents() {
magentaCyanOutlineRadioButton.addActionListener(e -> outlineChanged());
panel4.add(magentaCyanOutlineRadioButton);
}
panel5.add(panel4, "cell 0 2 1 3");
panel5.add(panel4, "cell 0 2 1 4");

//---- focusPaintedCheckBox ----
focusPaintedCheckBox.setText("focusPainted");
Expand All @@ -1321,6 +1354,11 @@ private void initComponents() {
hideArrowButtonCheckBox.setText("hide arrow button");
hideArrowButtonCheckBox.addActionListener(e -> hideArrowButton());
panel5.add(hideArrowButtonCheckBox, "cell 1 3");

//---- transparentBackgroundCheckBox ----
transparentBackgroundCheckBox.setText("transparent background");
transparentBackgroundCheckBox.addActionListener(e -> transparentBackground());
panel5.add(transparentBackgroundCheckBox, "cell 1 4");
}
add(panel5, "cell 5 13 2 10,grow");

Expand Down Expand Up @@ -1728,6 +1766,7 @@ private void initComponents() {
private JRadioButton magentaCyanOutlineRadioButton;
private JCheckBox focusPaintedCheckBox;
private JCheckBox hideArrowButtonCheckBox;
private JCheckBox transparentBackgroundCheckBox;
private JSlider slider1;
private JSlider slider6;
private JCheckBox sliderPaintTrackCheckBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ new FormModel {
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$columnConstraints": "[][]"
"$rowConstraints": "[]0[]0[]0[]0[]"
"$rowConstraints": "[]0[]0[]0[]0[]0[]"
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
} ) {
name: "panel5"
Expand Down Expand Up @@ -1092,7 +1092,7 @@ new FormModel {
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "outlineChanged", false ) )
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2 1 3"
"value": "cell 0 2 1 4"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "focusPaintedCheckBox"
Expand All @@ -1115,6 +1115,16 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "transparentBackgroundCheckBox"
"text": "transparent background"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "transparentBackground", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 5 13 2 10,grow"
} )
Expand Down

0 comments on commit 145631f

Please sign in to comment.