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

@@ -19,6 +19,7 @@ package com.formdev.flatlaf.util;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.beans.PropertyChangeEvent;
@@ -42,6 +43,7 @@ import javax.swing.plaf.UIResource;
* So the JRE does the scaling itself.
* E.g. when you draw a 10px line, a 15px line is drawn on screen.
* The scale factor may be different for each connected display.
* The scale factor may change for a window when moving the window from one display to another one.
*
* 2) user scaling mode
*
@@ -51,6 +53,7 @@ import javax.swing.plaf.UIResource;
* The JRE does not scale anything.
* So we have to invoke {@link #scale(float)} where necessary.
* There is only one user scale factor for all displays.
* The user scale factor may change if the active LaF or "Label.font" has changed.
*
* @author Karl Tauber
*/
@@ -89,6 +92,14 @@ public class UIScale
return jreHiDPI;
}
public static double getSystemScaleFactor( Graphics2D g ) {
return isJreHiDPIEnabled() ? g.getDeviceConfiguration().getDefaultTransform().getScaleX() : 1;
}
public static double getSystemScaleFactor( GraphicsConfiguration gc ) {
return isJreHiDPIEnabled() ? gc.getDefaultTransform().getScaleX() : 1;
}
//---- user scaling (Java 8) ----------------------------------------------
private static float scaleFactor = 1;

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();