mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-10 22:17:13 -06:00
FlatComponentsTest: added combobox to switch LaF and added right-to-left checkbox
This commit is contained in:
@@ -27,22 +27,11 @@ public class FlatComponentsTest
|
||||
extends JPanel
|
||||
{
|
||||
public static void main( String[] args ) {
|
||||
try {
|
||||
if( args.length > 0 )
|
||||
UIManager.setLookAndFeel( args[0] );
|
||||
else
|
||||
UIManager.setLookAndFeel( new FlatTestLaf() );
|
||||
} catch( Exception ex ) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
JOptionPane.showMessageDialog( null,
|
||||
new FlatComponentsTest(),
|
||||
"FlatComponentsTest (Java " + System.getProperty( "java.version" ) + ")",
|
||||
JOptionPane.PLAIN_MESSAGE );
|
||||
FlatTestFrame frame = FlatTestFrame.create( args, "FlatComponentsTest" );
|
||||
frame.showFrame( new FlatComponentsTest() );
|
||||
}
|
||||
|
||||
public FlatComponentsTest() {
|
||||
FlatComponentsTest() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
@@ -54,39 +43,11 @@ public class FlatComponentsTest
|
||||
progressBar4.setIndeterminate( indeterminate );
|
||||
}
|
||||
|
||||
private void customColorsChanged() {
|
||||
boolean test = customColorsCheckBox.isSelected();
|
||||
|
||||
for( Component c : getComponents() ) {
|
||||
c.setForeground( testColor( test, c, "foreground", Color.blue ) );
|
||||
c.setBackground( testColor( test, c, "background", Color.red ) );
|
||||
|
||||
if( c instanceof JScrollPane ) {
|
||||
Component view = ((JScrollPane)c).getViewport().getView();
|
||||
if( view != null ) {
|
||||
view.setForeground( testColor( test, view, "foreground", Color.magenta ) );
|
||||
view.setBackground( testColor( test, view, "background", Color.orange ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Color testColor( boolean test, Component c, String propertyName, Color testColor ) {
|
||||
if( test )
|
||||
return testColor;
|
||||
else {
|
||||
String uiClassID = ((JComponent)c).getUIClassID();
|
||||
String key = uiClassID.substring( 0, uiClassID.length() - 2 ) + "." + propertyName;
|
||||
return UIManager.getColor( key );
|
||||
}
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
|
||||
JLabel labelLabel = new JLabel();
|
||||
JLabel label1 = new JLabel();
|
||||
JLabel label2 = new JLabel();
|
||||
customColorsCheckBox = new JCheckBox();
|
||||
JLabel buttonLabel = new JLabel();
|
||||
JButton button1 = new JButton();
|
||||
JButton button2 = new JButton();
|
||||
@@ -174,7 +135,7 @@ public class FlatComponentsTest
|
||||
|
||||
//======== this ========
|
||||
setLayout(new MigLayout(
|
||||
"insets 0,hidemode 3,gap 5 5",
|
||||
"insets 0,hidemode 3,gap 5 5,ltr",
|
||||
// columns
|
||||
"[]" +
|
||||
"[]" +
|
||||
@@ -215,12 +176,6 @@ public class FlatComponentsTest
|
||||
label2.setEnabled(false);
|
||||
add(label2, "cell 2 0");
|
||||
|
||||
//---- customColorsCheckBox ----
|
||||
customColorsCheckBox.setText("Custom colors");
|
||||
customColorsCheckBox.setMnemonic('C');
|
||||
customColorsCheckBox.addActionListener(e -> customColorsChanged());
|
||||
add(customColorsCheckBox, "cell 5 0");
|
||||
|
||||
//---- buttonLabel ----
|
||||
buttonLabel.setText("JButton:");
|
||||
add(buttonLabel, "cell 0 1");
|
||||
@@ -624,13 +579,13 @@ public class FlatComponentsTest
|
||||
//---- progressBar3 ----
|
||||
progressBar3.setOrientation(SwingConstants.VERTICAL);
|
||||
progressBar3.setValue(50);
|
||||
add(progressBar3, "cell 4 11");
|
||||
add(progressBar3, "cell 4 11 1 4");
|
||||
|
||||
//---- progressBar4 ----
|
||||
progressBar4.setOrientation(SwingConstants.VERTICAL);
|
||||
progressBar4.setValue(55);
|
||||
progressBar4.setStringPainted(true);
|
||||
add(progressBar4, "cell 4 11");
|
||||
add(progressBar4, "cell 4 11 1 4");
|
||||
|
||||
//---- scrollBarLabel ----
|
||||
scrollBarLabel.setText("JScrollBar:");
|
||||
@@ -671,7 +626,6 @@ public class FlatComponentsTest
|
||||
}
|
||||
|
||||
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
|
||||
private JCheckBox customColorsCheckBox;
|
||||
private JProgressBar progressBar3;
|
||||
private JProgressBar progressBar4;
|
||||
private JProgressBar progressBar1;
|
||||
|
||||
@@ -7,7 +7,7 @@ new FormModel {
|
||||
"JavaCodeGenerator.defaultVariableLocal": true
|
||||
}
|
||||
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
|
||||
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5"
|
||||
"$layoutConstraints": "insets 0,hidemode 3,gap 5 5,ltr"
|
||||
"$columnConstraints": "[][][][][][]"
|
||||
"$rowConstraints": "[][][][][][][][][][][][][][][][]"
|
||||
} ) {
|
||||
@@ -33,17 +33,6 @@ new FormModel {
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 2 0"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JCheckBox" ) {
|
||||
name: "customColorsCheckBox"
|
||||
"text": "Custom colors"
|
||||
"mnemonic": 67
|
||||
auxiliary() {
|
||||
"JavaCodeGenerator.variableLocal": false
|
||||
}
|
||||
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "customColorsChanged", false ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 5 0"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JLabel" ) {
|
||||
name: "buttonLabel"
|
||||
"text": "JButton:"
|
||||
@@ -551,7 +540,7 @@ new FormModel {
|
||||
"JavaCodeGenerator.variableLocal": false
|
||||
}
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 4 11"
|
||||
"value": "cell 4 11 1 4"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JProgressBar" ) {
|
||||
name: "progressBar4"
|
||||
@@ -562,7 +551,7 @@ new FormModel {
|
||||
"JavaCodeGenerator.variableLocal": false
|
||||
}
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 4 11"
|
||||
"value": "cell 4 11 1 4"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JLabel" ) {
|
||||
name: "scrollBarLabel"
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
/*
|
||||
* Copyright 2019 FormDev Software GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.formdev.flatlaf;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.prefs.Preferences;
|
||||
import javax.swing.*;
|
||||
import net.miginfocom.swing.*;
|
||||
|
||||
/**
|
||||
* @author Karl Tauber
|
||||
*/
|
||||
public class FlatTestFrame
|
||||
extends JFrame
|
||||
{
|
||||
private static final String PREFS_ROOT_PATH = "/flatlaf-test";
|
||||
private static final String KEY_LAF = "laf";
|
||||
|
||||
private JComponent content;
|
||||
|
||||
public static FlatTestFrame create( String[] args, String title ) {
|
||||
// set look and feel
|
||||
try {
|
||||
if( args.length > 0 )
|
||||
UIManager.setLookAndFeel( args[0] );
|
||||
else {
|
||||
String lafClassName = Preferences.userRoot().node( PREFS_ROOT_PATH )
|
||||
.get( KEY_LAF, FlatLightLaf.class.getName() );
|
||||
UIManager.setLookAndFeel( lafClassName );
|
||||
}
|
||||
} catch( Exception ex ) {
|
||||
ex.printStackTrace();
|
||||
|
||||
// fallback
|
||||
try {
|
||||
UIManager.setLookAndFeel( new FlatLightLaf() );
|
||||
} catch( Exception ex2 ) {
|
||||
ex2.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// create frame
|
||||
FlatTestFrame frame = new FlatTestFrame();
|
||||
frame.setTitle( title + " (Java " + System.getProperty( "java.version" ) + ")" );
|
||||
return frame;
|
||||
}
|
||||
|
||||
private FlatTestFrame() {
|
||||
initComponents();
|
||||
|
||||
// initialize look and feels combo box
|
||||
DefaultComboBoxModel<LafInfo> lafModel = new DefaultComboBoxModel<>();
|
||||
lafModel.addElement( new LafInfo( "Flat Light", FlatLightLaf.class.getName() ) );
|
||||
lafModel.addElement( new LafInfo( "Flat Dark", FlatDarkLaf.class.getName() ) );
|
||||
lafModel.addElement( new LafInfo( "Flat Test", FlatTestLaf.class.getName() ) );
|
||||
|
||||
UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
|
||||
for( UIManager.LookAndFeelInfo lookAndFeel : lookAndFeels ) {
|
||||
String name = lookAndFeel.getName();
|
||||
String className = lookAndFeel.getClassName();
|
||||
if( className.equals( "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel" ) ||
|
||||
className.equals( "com.sun.java.swing.plaf.motif.MotifLookAndFeel" ) )
|
||||
continue;
|
||||
|
||||
lafModel.addElement( new LafInfo( name, className ) );
|
||||
}
|
||||
|
||||
LookAndFeel activeLaf = UIManager.getLookAndFeel();
|
||||
String activeLafClassName = activeLaf.getClass().getName();
|
||||
int sel = lafModel.getIndexOf( new LafInfo( null, activeLafClassName ) );
|
||||
if( sel < 0 ) {
|
||||
lafModel.addElement( new LafInfo( activeLaf.getName(), activeLafClassName ) );
|
||||
sel = lafModel.getSize() - 1;
|
||||
}
|
||||
lafModel.setSelectedItem( lafModel.getElementAt( sel ) );
|
||||
|
||||
lookAndFeelComboBox.setModel( lafModel );
|
||||
|
||||
// register ESC key to close frame
|
||||
((JComponent)getContentPane()).registerKeyboardAction(
|
||||
e -> {
|
||||
dispose();
|
||||
},
|
||||
KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0, false ),
|
||||
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
|
||||
|
||||
// close frame
|
||||
closeButton.addActionListener(e -> dispose());
|
||||
}
|
||||
|
||||
protected void showFrame( JComponent content ) {
|
||||
this.content = content;
|
||||
|
||||
contentPanel.add( content );
|
||||
pack();
|
||||
setLocationRelativeTo( null );
|
||||
setVisible( true );
|
||||
}
|
||||
|
||||
private void lookAndFeelChanged() {
|
||||
LafInfo newLaf = (LafInfo) lookAndFeelComboBox.getSelectedItem();
|
||||
if( newLaf == null )
|
||||
return;
|
||||
|
||||
if( newLaf.className.equals( UIManager.getLookAndFeel().getClass().getName() ) )
|
||||
return;
|
||||
|
||||
Preferences.userRoot().node( PREFS_ROOT_PATH ).put( KEY_LAF, newLaf.className );
|
||||
|
||||
EventQueue.invokeLater( () -> {
|
||||
try {
|
||||
// change look and feel
|
||||
UIManager.setLookAndFeel( newLaf.className );
|
||||
|
||||
// update all components
|
||||
SwingUtilities.updateComponentTreeUI( this );
|
||||
|
||||
// increase size of frame if necessary
|
||||
int width = getWidth();
|
||||
int height = getHeight();
|
||||
Dimension prefSize = getPreferredSize();
|
||||
if( prefSize.width > width || prefSize.height > height )
|
||||
setSize( Math.max( prefSize.width, width ), Math.max( prefSize.height, height ) );
|
||||
|
||||
} catch( Exception ex ) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private void explicitColorsChanged() {
|
||||
boolean explicit = explicitColorsCheckBox.isSelected();
|
||||
|
||||
for( Component c : content.getComponents() ) {
|
||||
c.setForeground( explicitColor( explicit, c, "foreground", Color.blue ) );
|
||||
c.setBackground( explicitColor( explicit, c, "background", Color.red ) );
|
||||
|
||||
if( c instanceof JScrollPane ) {
|
||||
Component view = ((JScrollPane)c).getViewport().getView();
|
||||
if( view != null ) {
|
||||
view.setForeground( explicitColor( explicit, view, "foreground", Color.magenta ) );
|
||||
view.setBackground( explicitColor( explicit, view, "background", Color.orange ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Color explicitColor( boolean explicit, Component c, String propertyName, Color explicitColor ) {
|
||||
if( explicit )
|
||||
return explicitColor;
|
||||
else {
|
||||
String uiClassID = ((JComponent)c).getUIClassID();
|
||||
String key = uiClassID.substring( 0, uiClassID.length() - 2 ) + "." + propertyName;
|
||||
return UIManager.getColor( key );
|
||||
}
|
||||
}
|
||||
|
||||
private void rightToLeftChanged() {
|
||||
contentPanel.applyComponentOrientation( rightToLeftCheckBox.isSelected()
|
||||
? ComponentOrientation.RIGHT_TO_LEFT
|
||||
: ComponentOrientation.LEFT_TO_RIGHT );
|
||||
contentPanel.revalidate();
|
||||
contentPanel.repaint();
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
|
||||
dialogPane = new JPanel();
|
||||
contentPanel = new JPanel();
|
||||
buttonBar = new JPanel();
|
||||
lookAndFeelComboBox = new JComboBox<>();
|
||||
explicitColorsCheckBox = new JCheckBox();
|
||||
rightToLeftCheckBox = new JCheckBox();
|
||||
closeButton = new JButton();
|
||||
|
||||
//======== this ========
|
||||
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||
Container contentPane = getContentPane();
|
||||
contentPane.setLayout(new BorderLayout());
|
||||
|
||||
//======== dialogPane ========
|
||||
{
|
||||
dialogPane.setLayout(new BorderLayout());
|
||||
|
||||
//======== contentPanel ========
|
||||
{
|
||||
contentPanel.setLayout(new MigLayout(
|
||||
"insets dialog,hidemode 3",
|
||||
// columns
|
||||
"[grow,fill]",
|
||||
// rows
|
||||
"[grow,fill]"));
|
||||
}
|
||||
dialogPane.add(contentPanel, BorderLayout.CENTER);
|
||||
|
||||
//======== buttonBar ========
|
||||
{
|
||||
buttonBar.setLayout(new MigLayout(
|
||||
"insets dialog",
|
||||
// columns
|
||||
"[fill]" +
|
||||
"[fill]" +
|
||||
"[fill]" +
|
||||
"[grow,fill]" +
|
||||
"[button,fill]",
|
||||
// rows
|
||||
null));
|
||||
|
||||
//---- lookAndFeelComboBox ----
|
||||
lookAndFeelComboBox.addActionListener(e -> lookAndFeelChanged());
|
||||
buttonBar.add(lookAndFeelComboBox, "cell 0 0");
|
||||
|
||||
//---- explicitColorsCheckBox ----
|
||||
explicitColorsCheckBox.setText("explicit colors");
|
||||
explicitColorsCheckBox.setMnemonic('E');
|
||||
explicitColorsCheckBox.addActionListener(e -> explicitColorsChanged());
|
||||
buttonBar.add(explicitColorsCheckBox, "cell 1 0");
|
||||
|
||||
//---- rightToLeftCheckBox ----
|
||||
rightToLeftCheckBox.setText("right-to-left");
|
||||
rightToLeftCheckBox.setMnemonic('R');
|
||||
rightToLeftCheckBox.addActionListener(e -> rightToLeftChanged());
|
||||
buttonBar.add(rightToLeftCheckBox, "cell 2 0");
|
||||
|
||||
//---- closeButton ----
|
||||
closeButton.setText("Close");
|
||||
buttonBar.add(closeButton, "cell 4 0");
|
||||
}
|
||||
dialogPane.add(buttonBar, BorderLayout.SOUTH);
|
||||
}
|
||||
contentPane.add(dialogPane, BorderLayout.CENTER);
|
||||
// JFormDesigner - End of component initialization //GEN-END:initComponents
|
||||
}
|
||||
|
||||
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
|
||||
private JPanel dialogPane;
|
||||
private JPanel contentPanel;
|
||||
private JPanel buttonBar;
|
||||
private JComboBox<LafInfo> lookAndFeelComboBox;
|
||||
private JCheckBox explicitColorsCheckBox;
|
||||
private JCheckBox rightToLeftCheckBox;
|
||||
private JButton closeButton;
|
||||
// JFormDesigner - End of variables declaration //GEN-END:variables
|
||||
|
||||
//---- class LafInfo ------------------------------------------------------
|
||||
|
||||
static class LafInfo
|
||||
{
|
||||
final String name;
|
||||
final String className;
|
||||
|
||||
LafInfo( String name, String className ) {
|
||||
this.name = name;
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals( Object obj ) {
|
||||
return obj instanceof LafInfo && className.equals( ((LafInfo)obj).className );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
JFDML JFormDesigner: "7.0.0.0.194" Java: "11.0.2" encoding: "UTF-8"
|
||||
|
||||
new FormModel {
|
||||
contentType: "form/swing"
|
||||
root: new FormRoot {
|
||||
add( new FormWindow( "javax.swing.JFrame", new FormLayoutManager( class java.awt.BorderLayout ) ) {
|
||||
name: "this"
|
||||
"$locationPolicy": 2
|
||||
"$sizePolicy": 2
|
||||
"defaultCloseOperation": 2
|
||||
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class java.awt.BorderLayout ) ) {
|
||||
name: "dialogPane"
|
||||
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
|
||||
"$layoutConstraints": "insets dialog,hidemode 3"
|
||||
"$columnConstraints": "[grow,fill]"
|
||||
"$rowConstraints": "[grow,fill]"
|
||||
} ) {
|
||||
name: "contentPanel"
|
||||
}, new FormLayoutConstraints( class java.lang.String ) {
|
||||
"value": "Center"
|
||||
} )
|
||||
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
|
||||
"$layoutConstraints": "insets dialog"
|
||||
"$columnConstraints": "[fill][fill][fill][grow,fill][button,fill]"
|
||||
"$rowSpecs": "[fill]"
|
||||
} ) {
|
||||
name: "buttonBar"
|
||||
add( new FormComponent( "javax.swing.JComboBox" ) {
|
||||
name: "lookAndFeelComboBox"
|
||||
auxiliary() {
|
||||
"JavaCodeGenerator.typeParameters": "LafInfo"
|
||||
}
|
||||
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "lookAndFeelChanged", false ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 0 0"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JCheckBox" ) {
|
||||
name: "explicitColorsCheckBox"
|
||||
"text": "explicit colors"
|
||||
"mnemonic": 69
|
||||
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "explicitColorsChanged", false ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 1 0"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JCheckBox" ) {
|
||||
name: "rightToLeftCheckBox"
|
||||
"text": "right-to-left"
|
||||
"mnemonic": 82
|
||||
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "rightToLeftChanged", false ) )
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 2 0"
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JButton" ) {
|
||||
name: "closeButton"
|
||||
"text": "Close"
|
||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||
"value": "cell 4 0"
|
||||
} )
|
||||
}, new FormLayoutConstraints( class java.lang.String ) {
|
||||
"value": "South"
|
||||
} )
|
||||
}, new FormLayoutConstraints( class java.lang.String ) {
|
||||
"value": "Center"
|
||||
} )
|
||||
}, new FormLayoutConstraints( null ) {
|
||||
"location": new java.awt.Point( 0, 0 )
|
||||
"size": new java.awt.Dimension( 400, 300 )
|
||||
} )
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user