Demo: add info label shows the current system/user scale factor and the java version

This commit is contained in:
Karl Tauber
2019-09-07 23:48:24 +02:00
parent 70d3b7c443
commit c69e9e12c6
4 changed files with 42 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import com.formdev.flatlaf.*;
import com.formdev.flatlaf.util.SystemInfo;
import com.formdev.flatlaf.util.UIScale;
import net.miginfocom.swing.*;
/**
@@ -102,11 +103,26 @@ class ControlBar
frame.addWindowListener( new WindowAdapter() {
@Override
public void windowOpened( WindowEvent e ) {
updateInfoLabel();
closeButton.requestFocusInWindow();
}
@Override
public void windowActivated( WindowEvent e ) {
updateInfoLabel();
}
} );
}
private void updateInfoLabel() {
double systemScaleFactor = UIScale.getSystemScaleFactor( getGraphicsConfiguration() );
float userScaleFactor = UIScale.getUserScaleFactor();
infoLabel.setText( "(Java " + System.getProperty( "java.version" )
+ (systemScaleFactor != 1 ? ("; system scale factor " + systemScaleFactor) : "")
+ (userScaleFactor != 1 ? ("; user scale factor " + userScaleFactor) : "")
+ (systemScaleFactor == 1 && userScaleFactor == 1 ? "; no scaling" : "")
+ ")" );
}
private void registerSwitchToLookAndFeel( int keyCode, String lafClassName ) {
((JComponent)frame.getContentPane()).registerKeyboardAction(
e -> {
@@ -138,6 +154,9 @@ class ControlBar
// change look and feel
UIManager.setLookAndFeel( newLaf.className );
// update info label because user scale factor may change
updateInfoLabel();
// update all components
SwingUtilities.updateComponentTreeUI( frame );
@@ -209,6 +228,7 @@ class ControlBar
lookAndFeelComboBox = new JComboBox<>();
rightToLeftCheckBox = new JCheckBox();
enabledCheckBox = new JCheckBox();
infoLabel = new JLabel();
closeButton = new JButton();
//======== this ========
@@ -242,6 +262,10 @@ class ControlBar
enabledCheckBox.addActionListener(e -> enabledChanged());
add(enabledCheckBox, "cell 2 1");
//---- infoLabel ----
infoLabel.setText("text");
add(infoLabel, "cell 3 1,alignx center,growx 0");
//---- closeButton ----
closeButton.setText("Close");
closeButton.addActionListener(e -> closePerformed());
@@ -254,6 +278,7 @@ class ControlBar
private JComboBox<LafInfo> lookAndFeelComboBox;
private JCheckBox rightToLeftCheckBox;
private JCheckBox enabledCheckBox;
private JLabel infoLabel;
private JButton closeButton;
// JFormDesigner - End of variables declaration //GEN-END:variables

View File

@@ -41,6 +41,12 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "infoLabel"
"text": "text"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 3 1,alignx center,growx 0"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "closeButton"
"text": "Close"

View File

@@ -55,7 +55,6 @@ public class FlatLafDemo
// create frame
DemoFrame frame = new DemoFrame();
frame.setTitle( frame.getTitle() + " (Java " + System.getProperty( "java.version" ) + ")" );
// show frame
frame.pack();