Extras: added class FlatDesktop for easy integration into macOS screen menu (About, Preferences and Quit) when using Java 8

Demo:
- use class `FlatDesktop`
- hide "File > Exit" and "Help > About" on macOS
This commit is contained in:
Karl Tauber
2021-10-24 09:44:34 +02:00
parent f8b9f4c1fa
commit cc238d3e34
4 changed files with 265 additions and 9 deletions

View File

@@ -37,6 +37,7 @@ import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.demo.HintManager.Hint;
import com.formdev.flatlaf.demo.extras.*;
import com.formdev.flatlaf.demo.intellijthemes.*;
import com.formdev.flatlaf.extras.FlatDesktop;
import com.formdev.flatlaf.extras.FlatAnimatedLafChange;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.formdev.flatlaf.extras.FlatUIDefaultsInspector;
@@ -81,6 +82,22 @@ class DemoFrame
if( tabIndex >= 0 && tabIndex < tabbedPane.getTabCount() && tabIndex != tabbedPane.getSelectedIndex() )
tabbedPane.setSelectedIndex( tabIndex );
// hide some menu items on macOS
if( SystemInfo.isMacOS ) {
exitMenuItem.setVisible( false );
aboutMenuItem.setVisible( false );
// do not use HTML text on macOS
htmlMenuItem.setText( "some text" );
}
// integrate into macOS screen menu
FlatDesktop.setAboutHandler( this::aboutActionPerformed );
FlatDesktop.setPreferencesHandler( this::showPreferences );
FlatDesktop.setQuitHandler( response -> {
response.performQuit();
} );
SwingUtilities.invokeLater( () -> {
showHints();
} );
@@ -174,6 +191,13 @@ class DemoFrame
"About", JOptionPane.PLAIN_MESSAGE );
}
private void showPreferences() {
JOptionPane.showMessageDialog( this,
"Sorry, but FlatLaf Demo does not have preferences. :(\n"
+ "This dialog is here to demonstrate usage of class 'FlatDesktop' on macOS.",
"Preferences", JOptionPane.PLAIN_MESSAGE );
}
private void selectedTabChanged() {
DemoPrefs.getState().putInt( FlatLafDemo.KEY_TAB, tabbedPane.getSelectedIndex() );
}
@@ -346,7 +370,7 @@ class DemoFrame
private JLabel accentColorLabel;
private void initAccentColors() {
accentColorLabel = new JLabel( "Accent color:" );
accentColorLabel = new JLabel( "Accent color: " );
toolBar.add( Box.createHorizontalGlue() );
toolBar.add( accentColorLabel );
@@ -412,7 +436,7 @@ class DemoFrame
JMenuItem openMenuItem = new JMenuItem();
JMenuItem saveAsMenuItem = new JMenuItem();
JMenuItem closeMenuItem = new JMenuItem();
JMenuItem exitMenuItem = new JMenuItem();
exitMenuItem = new JMenuItem();
JMenu editMenu = new JMenu();
JMenuItem undoMenuItem = new JMenuItem();
JMenuItem redoMenuItem = new JMenuItem();
@@ -431,7 +455,7 @@ class DemoFrame
JMenuItem structureViewMenuItem = new JMenuItem();
JMenuItem propertiesViewMenuItem = new JMenuItem();
JMenuItem menuItem2 = new JMenuItem();
JMenuItem menuItem1 = new JMenuItem();
htmlMenuItem = new JMenuItem();
JRadioButtonMenuItem radioButtonMenuItem1 = new JRadioButtonMenuItem();
JRadioButtonMenuItem radioButtonMenuItem2 = new JRadioButtonMenuItem();
JRadioButtonMenuItem radioButtonMenuItem3 = new JRadioButtonMenuItem();
@@ -449,7 +473,7 @@ class DemoFrame
JMenuItem showHintsMenuItem = new JMenuItem();
JMenuItem showUIDefaultsInspectorMenuItem = new JMenuItem();
JMenu helpMenu = new JMenu();
JMenuItem aboutMenuItem = new JMenuItem();
aboutMenuItem = new JMenuItem();
toolBar = new JToolBar();
JButton backButton = new JButton();
JButton forwardButton = new JButton();
@@ -638,9 +662,9 @@ class DemoFrame
menuItem2.setEnabled(false);
viewMenu.add(menuItem2);
//---- menuItem1 ----
menuItem1.setText("<html>some <b color=\"red\">HTML</b> <i color=\"blue\">text</i></html>");
viewMenu.add(menuItem1);
//---- htmlMenuItem ----
htmlMenuItem.setText("<html>some <b color=\"red\">HTML</b> <i color=\"blue\">text</i></html>");
viewMenu.add(htmlMenuItem);
viewMenu.addSeparator();
//---- radioButtonMenuItem1 ----
@@ -886,6 +910,8 @@ class DemoFrame
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JMenuItem exitMenuItem;
private JMenuItem htmlMenuItem;
private JMenu fontMenu;
private JMenu optionsMenu;
private JCheckBoxMenuItem windowDecorationsCheckBoxMenuItem;
@@ -894,6 +920,7 @@ class DemoFrame
private JCheckBoxMenuItem underlineMenuSelectionMenuItem;
private JCheckBoxMenuItem alwaysShowMnemonicsMenuItem;
private JCheckBoxMenuItem animatedLafChangeMenuItem;
private JMenuItem aboutMenuItem;
private JToolBar toolBar;
private JTabbedPane tabbedPane;
private ControlBar controlBar;

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.4.0.360" Java: "11.0.11" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.4.0.360" Java: "17" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -170,6 +170,9 @@ new FormModel {
"text": "Exit"
"accelerator": static javax.swing.KeyStroke getKeyStroke( 81, 4226, false )
"mnemonic": 88
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "exitActionPerformed", false ) )
} )
} )
@@ -285,8 +288,11 @@ new FormModel {
"enabled": false
} )
add( new FormComponent( "javax.swing.JMenuItem" ) {
name: "menuItem1"
name: "htmlMenuItem"
"text": "<html>some <b color=\"red\">HTML</b> <i color=\"blue\">text</i></html>"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
} )
add( new FormComponent( "javax.swing.JPopupMenu$Separator" ) {
name: "separator8"
@@ -415,6 +421,9 @@ new FormModel {
name: "aboutMenuItem"
"text": "About"
"mnemonic": 65
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "aboutActionPerformed", false ) )
} )
} )