mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 22:10:54 +03:00
Demo: add info label shows the current system/user scale factor and the java version
This commit is contained in:
@@ -19,6 +19,7 @@ package com.formdev.flatlaf.util;
|
|||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.GraphicsConfiguration;
|
||||||
import java.awt.GraphicsEnvironment;
|
import java.awt.GraphicsEnvironment;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.beans.PropertyChangeEvent;
|
import java.beans.PropertyChangeEvent;
|
||||||
@@ -42,6 +43,7 @@ import javax.swing.plaf.UIResource;
|
|||||||
* So the JRE does the scaling itself.
|
* So the JRE does the scaling itself.
|
||||||
* E.g. when you draw a 10px line, a 15px line is drawn on screen.
|
* 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 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
|
* 2) user scaling mode
|
||||||
*
|
*
|
||||||
@@ -51,6 +53,7 @@ import javax.swing.plaf.UIResource;
|
|||||||
* The JRE does not scale anything.
|
* The JRE does not scale anything.
|
||||||
* So we have to invoke {@link #scale(float)} where necessary.
|
* So we have to invoke {@link #scale(float)} where necessary.
|
||||||
* There is only one user scale factor for all displays.
|
* 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
|
* @author Karl Tauber
|
||||||
*/
|
*/
|
||||||
@@ -89,6 +92,14 @@ public class UIScale
|
|||||||
return jreHiDPI;
|
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) ----------------------------------------------
|
//---- user scaling (Java 8) ----------------------------------------------
|
||||||
|
|
||||||
private static float scaleFactor = 1;
|
private static float scaleFactor = 1;
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import javax.swing.plaf.metal.MetalLookAndFeel;
|
|||||||
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
|
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
|
||||||
import com.formdev.flatlaf.*;
|
import com.formdev.flatlaf.*;
|
||||||
import com.formdev.flatlaf.util.SystemInfo;
|
import com.formdev.flatlaf.util.SystemInfo;
|
||||||
|
import com.formdev.flatlaf.util.UIScale;
|
||||||
import net.miginfocom.swing.*;
|
import net.miginfocom.swing.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,11 +103,26 @@ class ControlBar
|
|||||||
frame.addWindowListener( new WindowAdapter() {
|
frame.addWindowListener( new WindowAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void windowOpened( WindowEvent e ) {
|
public void windowOpened( WindowEvent e ) {
|
||||||
|
updateInfoLabel();
|
||||||
closeButton.requestFocusInWindow();
|
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 ) {
|
private void registerSwitchToLookAndFeel( int keyCode, String lafClassName ) {
|
||||||
((JComponent)frame.getContentPane()).registerKeyboardAction(
|
((JComponent)frame.getContentPane()).registerKeyboardAction(
|
||||||
e -> {
|
e -> {
|
||||||
@@ -138,6 +154,9 @@ class ControlBar
|
|||||||
// change look and feel
|
// change look and feel
|
||||||
UIManager.setLookAndFeel( newLaf.className );
|
UIManager.setLookAndFeel( newLaf.className );
|
||||||
|
|
||||||
|
// update info label because user scale factor may change
|
||||||
|
updateInfoLabel();
|
||||||
|
|
||||||
// update all components
|
// update all components
|
||||||
SwingUtilities.updateComponentTreeUI( frame );
|
SwingUtilities.updateComponentTreeUI( frame );
|
||||||
|
|
||||||
@@ -209,6 +228,7 @@ class ControlBar
|
|||||||
lookAndFeelComboBox = new JComboBox<>();
|
lookAndFeelComboBox = new JComboBox<>();
|
||||||
rightToLeftCheckBox = new JCheckBox();
|
rightToLeftCheckBox = new JCheckBox();
|
||||||
enabledCheckBox = new JCheckBox();
|
enabledCheckBox = new JCheckBox();
|
||||||
|
infoLabel = new JLabel();
|
||||||
closeButton = new JButton();
|
closeButton = new JButton();
|
||||||
|
|
||||||
//======== this ========
|
//======== this ========
|
||||||
@@ -242,6 +262,10 @@ class ControlBar
|
|||||||
enabledCheckBox.addActionListener(e -> enabledChanged());
|
enabledCheckBox.addActionListener(e -> enabledChanged());
|
||||||
add(enabledCheckBox, "cell 2 1");
|
add(enabledCheckBox, "cell 2 1");
|
||||||
|
|
||||||
|
//---- infoLabel ----
|
||||||
|
infoLabel.setText("text");
|
||||||
|
add(infoLabel, "cell 3 1,alignx center,growx 0");
|
||||||
|
|
||||||
//---- closeButton ----
|
//---- closeButton ----
|
||||||
closeButton.setText("Close");
|
closeButton.setText("Close");
|
||||||
closeButton.addActionListener(e -> closePerformed());
|
closeButton.addActionListener(e -> closePerformed());
|
||||||
@@ -254,6 +278,7 @@ class ControlBar
|
|||||||
private JComboBox<LafInfo> lookAndFeelComboBox;
|
private JComboBox<LafInfo> lookAndFeelComboBox;
|
||||||
private JCheckBox rightToLeftCheckBox;
|
private JCheckBox rightToLeftCheckBox;
|
||||||
private JCheckBox enabledCheckBox;
|
private JCheckBox enabledCheckBox;
|
||||||
|
private JLabel infoLabel;
|
||||||
private JButton closeButton;
|
private JButton closeButton;
|
||||||
// JFormDesigner - End of variables declaration //GEN-END:variables
|
// JFormDesigner - End of variables declaration //GEN-END:variables
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ new FormModel {
|
|||||||
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
|
||||||
"value": "cell 2 1"
|
"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" ) {
|
add( new FormComponent( "javax.swing.JButton" ) {
|
||||||
name: "closeButton"
|
name: "closeButton"
|
||||||
"text": "Close"
|
"text": "Close"
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ public class FlatLafDemo
|
|||||||
|
|
||||||
// create frame
|
// create frame
|
||||||
DemoFrame frame = new DemoFrame();
|
DemoFrame frame = new DemoFrame();
|
||||||
frame.setTitle( frame.getTitle() + " (Java " + System.getProperty( "java.version" ) + ")" );
|
|
||||||
|
|
||||||
// show frame
|
// show frame
|
||||||
frame.pack();
|
frame.pack();
|
||||||
|
|||||||
Reference in New Issue
Block a user