mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-13 07:17:13 -06:00
Demo: improved "Font" menu:
- add current font family and size to menu - filter out unavailable fonts - select active font family and size - disable font menu items if non-FlatLaf LaF is active also show current font in bottom control bar (issue #105)
This commit is contained in:
@@ -37,7 +37,7 @@ import net.miginfocom.swing.*;
|
|||||||
class ControlBar
|
class ControlBar
|
||||||
extends JPanel
|
extends JPanel
|
||||||
{
|
{
|
||||||
private JFrame frame;
|
private DemoFrame frame;
|
||||||
private JTabbedPane tabbedPane;
|
private JTabbedPane tabbedPane;
|
||||||
|
|
||||||
ControlBar() {
|
ControlBar() {
|
||||||
@@ -78,6 +78,9 @@ class ControlBar
|
|||||||
// update info label because user scale factor may change
|
// update info label because user scale factor may change
|
||||||
updateInfoLabel();
|
updateInfoLabel();
|
||||||
|
|
||||||
|
// update "Font" menu
|
||||||
|
frame.updateFontMenuItems();
|
||||||
|
|
||||||
// this is necessary because embedded JOptionPane's "steal" the default button
|
// this is necessary because embedded JOptionPane's "steal" the default button
|
||||||
frame.getRootPane().setDefaultButton( closeButton );
|
frame.getRootPane().setDefaultButton( closeButton );
|
||||||
} );
|
} );
|
||||||
@@ -90,7 +93,15 @@ class ControlBar
|
|||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize( JFrame frame, JTabbedPane tabbedPane ) {
|
@Override
|
||||||
|
public void updateUI() {
|
||||||
|
super.updateUI();
|
||||||
|
|
||||||
|
if( infoLabel != null )
|
||||||
|
updateInfoLabel();
|
||||||
|
}
|
||||||
|
|
||||||
|
void initialize( DemoFrame frame, JTabbedPane tabbedPane ) {
|
||||||
this.frame = frame;
|
this.frame = frame;
|
||||||
this.tabbedPane = tabbedPane;
|
this.tabbedPane = tabbedPane;
|
||||||
|
|
||||||
@@ -141,10 +152,14 @@ class ControlBar
|
|||||||
private void updateInfoLabel() {
|
private void updateInfoLabel() {
|
||||||
double systemScaleFactor = UIScale.getSystemScaleFactor( getGraphicsConfiguration() );
|
double systemScaleFactor = UIScale.getSystemScaleFactor( getGraphicsConfiguration() );
|
||||||
float userScaleFactor = UIScale.getUserScaleFactor();
|
float userScaleFactor = UIScale.getUserScaleFactor();
|
||||||
|
Font font = UIManager.getFont( "Label.font" );
|
||||||
String newInfo = "(Java " + System.getProperty( "java.version" )
|
String newInfo = "(Java " + System.getProperty( "java.version" )
|
||||||
+ (systemScaleFactor != 1 ? ("; system scale factor " + systemScaleFactor) : "")
|
+ (systemScaleFactor != 1 ? ("; system scale factor " + systemScaleFactor) : "")
|
||||||
+ (userScaleFactor != 1 ? ("; user scale factor " + userScaleFactor) : "")
|
+ (userScaleFactor != 1 ? ("; user scale factor " + userScaleFactor) : "")
|
||||||
+ (systemScaleFactor == 1 && userScaleFactor == 1 ? "; no scaling" : "")
|
+ (systemScaleFactor == 1 && userScaleFactor == 1 ? "; no scaling" : "")
|
||||||
|
+ "; " + font.getFamily() + " " + font.getSize()
|
||||||
|
+ (font.isBold() ? " BOLD" : "")
|
||||||
|
+ (font.isItalic() ? " ITALIC" : "")
|
||||||
+ ")";
|
+ ")";
|
||||||
|
|
||||||
if( !newInfo.equals( infoLabel.getText() ) )
|
if( !newInfo.equals( infoLabel.getText() ) )
|
||||||
@@ -177,6 +192,10 @@ class ControlBar
|
|||||||
// change look and feel
|
// change look and feel
|
||||||
UIManager.setLookAndFeel( lafClassName );
|
UIManager.setLookAndFeel( lafClassName );
|
||||||
|
|
||||||
|
// clear custom default font when switching to non-FlatLaf LaF
|
||||||
|
if( !(UIManager.getLookAndFeel() instanceof FlatLaf) )
|
||||||
|
UIManager.put( "defaultFont", null );
|
||||||
|
|
||||||
// update all components
|
// update all components
|
||||||
FlatLaf.updateUI();
|
FlatLaf.updateUI();
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package com.formdev.flatlaf.demo;
|
|||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.text.DefaultEditorKit;
|
import javax.swing.text.DefaultEditorKit;
|
||||||
import com.formdev.flatlaf.FlatLaf;
|
import com.formdev.flatlaf.FlatLaf;
|
||||||
@@ -32,10 +34,18 @@ import net.miginfocom.swing.*;
|
|||||||
class DemoFrame
|
class DemoFrame
|
||||||
extends JFrame
|
extends JFrame
|
||||||
{
|
{
|
||||||
|
private final String[] availableFontFamilyNames;
|
||||||
|
private int initialFontMenuItemCount = -1;
|
||||||
|
|
||||||
DemoFrame() {
|
DemoFrame() {
|
||||||
int tabIndex = DemoPrefs.getState().getInt( FlatLafDemo.KEY_TAB, 0 );
|
int tabIndex = DemoPrefs.getState().getInt( FlatLafDemo.KEY_TAB, 0 );
|
||||||
|
|
||||||
|
availableFontFamilyNames = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
||||||
|
.getAvailableFontFamilyNames().clone();
|
||||||
|
Arrays.sort( availableFontFamilyNames );
|
||||||
|
|
||||||
initComponents();
|
initComponents();
|
||||||
|
updateFontMenuItems();
|
||||||
controlBar.initialize( this, tabbedPane );
|
controlBar.initialize( this, tabbedPane );
|
||||||
|
|
||||||
if( tabIndex >= 0 && tabIndex < tabbedPane.getTabCount() && tabIndex != tabbedPane.getSelectedIndex() )
|
if( tabIndex >= 0 && tabIndex < tabbedPane.getTabCount() && tabIndex != tabbedPane.getSelectedIndex() )
|
||||||
@@ -91,6 +101,7 @@ class DemoFrame
|
|||||||
|
|
||||||
private void restoreFont() {
|
private void restoreFont() {
|
||||||
UIManager.put( "defaultFont", null );
|
UIManager.put( "defaultFont", null );
|
||||||
|
updateFontMenuItems();
|
||||||
FlatLaf.updateUI();
|
FlatLaf.updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,17 +110,79 @@ class DemoFrame
|
|||||||
Font newFont = font.deriveFont( (float) (font.getSize() + 1) );
|
Font newFont = font.deriveFont( (float) (font.getSize() + 1) );
|
||||||
UIManager.put( "defaultFont", newFont );
|
UIManager.put( "defaultFont", newFont );
|
||||||
|
|
||||||
|
updateFontMenuItems();
|
||||||
FlatLaf.updateUI();
|
FlatLaf.updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decrFont() {
|
private void decrFont() {
|
||||||
Font font = UIManager.getFont( "defaultFont" );
|
Font font = UIManager.getFont( "defaultFont" );
|
||||||
Font newFont = font.deriveFont( (float) Math.max( font.getSize() - 1, 8 ) );
|
Font newFont = font.deriveFont( (float) Math.max( font.getSize() - 1, 10 ) );
|
||||||
UIManager.put( "defaultFont", newFont );
|
UIManager.put( "defaultFont", newFont );
|
||||||
|
|
||||||
|
updateFontMenuItems();
|
||||||
FlatLaf.updateUI();
|
FlatLaf.updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updateFontMenuItems() {
|
||||||
|
if( initialFontMenuItemCount < 0 )
|
||||||
|
initialFontMenuItemCount = fontMenu.getItemCount();
|
||||||
|
else {
|
||||||
|
// remove old font items
|
||||||
|
for( int i = fontMenu.getItemCount() - 1; i >= initialFontMenuItemCount; i-- )
|
||||||
|
fontMenu.remove( i );
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current font
|
||||||
|
Font currentFont = UIManager.getFont( "Label.font" );
|
||||||
|
String currentFamily = currentFont.getFamily();
|
||||||
|
String currentSize = Integer.toString( currentFont.getSize() );
|
||||||
|
|
||||||
|
// add font families
|
||||||
|
fontMenu.addSeparator();
|
||||||
|
ArrayList<String> families = new ArrayList<>( Arrays.asList(
|
||||||
|
"Arial", "Comic Sans MS", "Courier New", "DejaVu Sans", "Dialog", "Monospaced",
|
||||||
|
"Noto Sans", "Roboto", "SansSerif", "Segoe UI", "Serif", "Tahoma", "Verdana" ) );
|
||||||
|
if( !families.contains( currentFamily ) )
|
||||||
|
families.add( currentFamily );
|
||||||
|
families.sort( String.CASE_INSENSITIVE_ORDER );
|
||||||
|
|
||||||
|
ButtonGroup familiesGroup = new ButtonGroup();
|
||||||
|
for( String family : families ) {
|
||||||
|
if( Arrays.binarySearch( availableFontFamilyNames, family ) < 0 )
|
||||||
|
continue; // not available
|
||||||
|
|
||||||
|
JCheckBoxMenuItem item = new JCheckBoxMenuItem( family );
|
||||||
|
item.setSelected( family.equals( currentFamily ) );
|
||||||
|
item.addActionListener( this::fontFamilyChanged );
|
||||||
|
fontMenu.add( item );
|
||||||
|
|
||||||
|
familiesGroup.add( item );
|
||||||
|
}
|
||||||
|
|
||||||
|
// add font sizes
|
||||||
|
fontMenu.addSeparator();
|
||||||
|
ArrayList<String> sizes = new ArrayList<>( Arrays.asList(
|
||||||
|
"10", "12", "14", "16", "18", "20", "24", "28" ) );
|
||||||
|
if( !sizes.contains( currentSize ) )
|
||||||
|
sizes.add( currentSize );
|
||||||
|
sizes.sort( String.CASE_INSENSITIVE_ORDER );
|
||||||
|
|
||||||
|
ButtonGroup sizesGroup = new ButtonGroup();
|
||||||
|
for( String size : sizes ) {
|
||||||
|
JCheckBoxMenuItem item = new JCheckBoxMenuItem( size );
|
||||||
|
item.setSelected( size.equals( currentSize ) );
|
||||||
|
item.addActionListener( this::fontSizeChanged );
|
||||||
|
fontMenu.add( item );
|
||||||
|
|
||||||
|
sizesGroup.add( item );
|
||||||
|
}
|
||||||
|
|
||||||
|
// enabled/disable items
|
||||||
|
boolean enabled = UIManager.getLookAndFeel() instanceof FlatLaf;
|
||||||
|
for( Component item : fontMenu.getMenuComponents() )
|
||||||
|
item.setEnabled( enabled );
|
||||||
|
}
|
||||||
|
|
||||||
private void initComponents() {
|
private void initComponents() {
|
||||||
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
|
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
|
||||||
JMenuBar menuBar1 = new JMenuBar();
|
JMenuBar menuBar1 = new JMenuBar();
|
||||||
@@ -493,26 +566,6 @@ class DemoFrame
|
|||||||
cutMenuItem.addActionListener( new DefaultEditorKit.CutAction() );
|
cutMenuItem.addActionListener( new DefaultEditorKit.CutAction() );
|
||||||
copyMenuItem.addActionListener( new DefaultEditorKit.CopyAction() );
|
copyMenuItem.addActionListener( new DefaultEditorKit.CopyAction() );
|
||||||
pasteMenuItem.addActionListener( new DefaultEditorKit.PasteAction() );
|
pasteMenuItem.addActionListener( new DefaultEditorKit.PasteAction() );
|
||||||
|
|
||||||
// add font families
|
|
||||||
fontMenu.addSeparator();
|
|
||||||
String[] fontFamilies = { "Arial", "Comic Sans MS", "Courier New", "Dialog",
|
|
||||||
"Monospaced", "SansSerif", "Serif", "Tahoma", "Verdana" };
|
|
||||||
for( String fontFamily : fontFamilies ) {
|
|
||||||
JMenuItem fontItem = new JMenuItem( fontFamily );
|
|
||||||
fontItem.addActionListener( this::fontFamilyChanged );
|
|
||||||
fontMenu.add( fontItem );
|
|
||||||
}
|
|
||||||
|
|
||||||
// add font sizes
|
|
||||||
fontMenu.addSeparator();
|
|
||||||
int[] fontSizes = { 8, 10, 12, 14, 16, 18, 20, 24, 28 };
|
|
||||||
for( int fontSize : fontSizes ) {
|
|
||||||
String fontSizeStr = Integer.toString( fontSize );
|
|
||||||
JMenuItem fontItem = new JMenuItem( fontSizeStr );
|
|
||||||
fontItem.addActionListener( this::fontSizeChanged );
|
|
||||||
fontMenu.add( fontItem );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
|
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
|
||||||
|
|||||||
Reference in New Issue
Block a user