From ce1a1487aa29059b177c9845e397daa3d9272850 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 23 Mar 2021 14:58:53 +0100 Subject: [PATCH] support menu bars in JDialog --- .../com/formdev/flatlaf/MnemonicHandler.java | 14 ++++-- .../formdev/flatlaf/ui/FlatRootPaneUI.java | 4 +- .../testing/FlatWindowDecorationsTest.java | 43 ++++++++++++++----- .../testing/FlatWindowDecorationsTest.jfd | 18 +++++++- .../flatlaf/testing/FlatTestLaf.properties | 1 + 5 files changed, 63 insertions(+), 17 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java index 5606ae0c..f67ee649 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/MnemonicHandler.java @@ -28,6 +28,7 @@ import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.lang.ref.WeakReference; import javax.swing.AbstractButton; +import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; @@ -137,10 +138,17 @@ class MnemonicHandler // get menu bar and first menu Component c = e.getComponent(); JRootPane rootPane = SwingUtilities.getRootPane( c ); - Window window = (rootPane != null) ? SwingUtilities.getWindowAncestor( rootPane ) : null; JMenuBar menuBar = (rootPane != null) ? rootPane.getJMenuBar() : null; - if( menuBar == null && window instanceof JFrame ) - menuBar = ((JFrame)window).getJMenuBar(); + if( menuBar == null ) { + // get menu bar from frame/dialog because there + // may be multiple nested root panes in a frame/dialog + // (e.g. each internal frame has its own root pane) + Window window = SwingUtilities.getWindowAncestor( c ); + if( window instanceof JFrame ) + menuBar = ((JFrame)window).getJMenuBar(); + else if( window instanceof JDialog ) + menuBar = ((JDialog)window).getJMenuBar(); + } JMenu firstMenu = (menuBar != null) ? menuBar.getMenu( 0 ) : null; // select first menu and show mnemonics diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java index 417cd1e5..7dc26eff 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatRootPaneUI.java @@ -431,7 +431,9 @@ public class FlatRootPaneUI (parent instanceof JFrame && (((JFrame)parent).getJMenuBar() == null || !((JFrame)parent).getJMenuBar().isVisible())) || - parent instanceof JDialog; + (parent instanceof JDialog && + (((JDialog)parent).getJMenuBar() == null || + !((JDialog)parent).getJMenuBar().isVisible())); } } } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.java index 7bdc8093..9b791068 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.java @@ -75,14 +75,14 @@ public class FlatWindowDecorationsTest super.addNotify(); Window window = SwingUtilities.windowForComponent( this ); - menuBarCheckBox.setEnabled( window instanceof JFrame ); - menuBarEmbeddedCheckBox.setEnabled( window instanceof JFrame ); - menuBarVisibleCheckBox.setEnabled( window instanceof JFrame ); + menuBarCheckBox.setSelected( window instanceof JFrame ); maximizedBoundsCheckBox.setEnabled( window instanceof Frame ); addMenuButton.setEnabled( menuBarCheckBox.isEnabled() ); + addGlueButton.setEnabled( menuBarCheckBox.isEnabled() ); removeMenuButton.setEnabled( menuBarCheckBox.isEnabled() ); changeMenuButton.setEnabled( menuBarCheckBox.isEnabled() ); + changeTitleButton.setEnabled( menuBarCheckBox.isEnabled() ); boolean windowHasIcons = (window != null && !window.getIconImages().isEmpty()); iconNoneRadioButton.setEnabled( windowHasIcons ); @@ -117,11 +117,12 @@ public class FlatWindowDecorationsTest private void menuBarChanged() { Window window = SwingUtilities.windowForComponent( this ); - if( window instanceof JFrame ) { + if( window instanceof JFrame ) ((JFrame)window).setJMenuBar( menuBarCheckBox.isSelected() ? menuBar : null ); - window.revalidate(); - window.repaint(); - } + else if( window instanceof JDialog ) + ((JDialog)window).setJMenuBar( menuBarCheckBox.isSelected() ? menuBar : null ); + window.revalidate(); + window.repaint(); } private void menuBarEmbeddedChanged() { @@ -250,7 +251,7 @@ public class FlatWindowDecorationsTest private void openDialog() { Window owner = SwingUtilities.windowForComponent( this ); - JDialog dialog = new JDialog( owner, "Dialog", ModalityType.APPLICATION_MODAL ); + JDialog dialog = new JDialog( owner, "Dialog", ModalityType.DOCUMENT_MODAL ); dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE ); dialog.add( new FlatWindowDecorationsTest() ); dialog.pack(); @@ -258,6 +259,17 @@ public class FlatWindowDecorationsTest dialog.setVisible( true ); } + private void openFrame() { + FlatWindowDecorationsTest comp = new FlatWindowDecorationsTest(); + JFrame frame = new JFrame( "Frame" ); + frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); + frame.add( comp ); + frame.setJMenuBar( comp.menuBar ); + frame.pack(); + frame.setLocationRelativeTo( this ); + frame.setVisible( true ); + } + private void decorationStyleChanged() { int style; if( styleFrameRadioButton.isSelected() ) @@ -315,10 +327,10 @@ public class FlatWindowDecorationsTest unifiedBackgroundCheckBox = new JCheckBox(); JPanel panel3 = new JPanel(); addMenuButton = new JButton(); - JButton addGlueButton = new JButton(); + addGlueButton = new JButton(); removeMenuButton = new JButton(); changeMenuButton = new JButton(); - JButton changeTitleButton = new JButton(); + changeTitleButton = new JButton(); menuBarEmbeddedCheckBox = new JCheckBox(); colorizeMenuBarCheckBox = new JCheckBox(); menuBarVisibleCheckBox = new JCheckBox(); @@ -344,6 +356,7 @@ public class FlatWindowDecorationsTest iconTestAllRadioButton = new JRadioButton(); iconTestRandomRadioButton = new JRadioButton(); JButton openDialogButton = new JButton(); + JButton openFrameButton = new JButton(); menuBar = new JMenuBar(); JMenu fileMenu = new JMenu(); JMenuItem newMenuItem = new JMenuItem(); @@ -587,7 +600,13 @@ public class FlatWindowDecorationsTest //---- openDialogButton ---- openDialogButton.setText("Open Dialog"); openDialogButton.addActionListener(e -> openDialog()); - add(openDialogButton, "cell 0 7"); + add(openDialogButton, "cell 0 7 2 1"); + + //---- openFrameButton ---- + openFrameButton.setText("Open Frame"); + openFrameButton.setMnemonic('A'); + openFrameButton.addActionListener(e -> openFrame()); + add(openFrameButton, "cell 0 7 2 1"); //======== menuBar ======== { @@ -781,8 +800,10 @@ public class FlatWindowDecorationsTest private JCheckBox menuBarCheckBox; private JCheckBox unifiedBackgroundCheckBox; private JButton addMenuButton; + private JButton addGlueButton; private JButton removeMenuButton; private JButton changeMenuButton; + private JButton changeTitleButton; private JCheckBox menuBarEmbeddedCheckBox; private JCheckBox colorizeMenuBarCheckBox; private JCheckBox menuBarVisibleCheckBox; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.jfd index 64f877b7..fc243485 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatWindowDecorationsTest.jfd @@ -1,4 +1,4 @@ -JFDML JFormDesigner: "7.0.3.1.342" Java: "15" encoding: "UTF-8" +JFDML JFormDesigner: "7.0.3.1.342" Java: "16" encoding: "UTF-8" new FormModel { contentType: "form/swing" @@ -52,6 +52,9 @@ new FormModel { add( new FormComponent( "javax.swing.JButton" ) { name: "addGlueButton" "text": "Add glue" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "addGlue", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 1" @@ -79,6 +82,9 @@ new FormModel { add( new FormComponent( "javax.swing.JButton" ) { name: "changeTitleButton" "text": "Change title" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "changeTitle", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 4" @@ -338,7 +344,15 @@ new FormModel { "text": "Open Dialog" addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "openDialog", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 7" + "value": "cell 0 7 2 1" + } ) + add( new FormComponent( "javax.swing.JButton" ) { + name: "openFrameButton" + "text": "Open Frame" + "mnemonic": 65 + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "openFrame", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 7 2 1" } ) }, new FormLayoutConstraints( null ) { "location": new java.awt.Point( 0, 0 ) diff --git a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties index 2083e575..c6b78bfd 100644 --- a/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties +++ b/flatlaf-testing/src/main/resources/com/formdev/flatlaf/testing/FlatTestLaf.properties @@ -344,6 +344,7 @@ TitlePane.background = #0f0 TitlePane.inactiveBackground = #080 TitlePane.foreground = #00f TitlePane.inactiveForeground = #fff +TitlePane.borderColor = #f00 #---- ToggleButton ----