support menu bars in JDialog

This commit is contained in:
Karl Tauber
2021-03-23 14:58:53 +01:00
parent fe1e364a1d
commit ce1a1487aa
5 changed files with 63 additions and 17 deletions

View File

@@ -28,6 +28,7 @@ import java.awt.event.WindowEvent;
import java.awt.event.WindowListener; import java.awt.event.WindowListener;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import javax.swing.AbstractButton; import javax.swing.AbstractButton;
import javax.swing.JDialog;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JMenu; import javax.swing.JMenu;
@@ -137,10 +138,17 @@ class MnemonicHandler
// get menu bar and first menu // get menu bar and first menu
Component c = e.getComponent(); Component c = e.getComponent();
JRootPane rootPane = SwingUtilities.getRootPane( c ); JRootPane rootPane = SwingUtilities.getRootPane( c );
Window window = (rootPane != null) ? SwingUtilities.getWindowAncestor( rootPane ) : null;
JMenuBar menuBar = (rootPane != null) ? rootPane.getJMenuBar() : null; JMenuBar menuBar = (rootPane != null) ? rootPane.getJMenuBar() : null;
if( menuBar == null && window instanceof JFrame ) if( menuBar == null ) {
menuBar = ((JFrame)window).getJMenuBar(); // 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; JMenu firstMenu = (menuBar != null) ? menuBar.getMenu( 0 ) : null;
// select first menu and show mnemonics // select first menu and show mnemonics

View File

@@ -431,7 +431,9 @@ public class FlatRootPaneUI
(parent instanceof JFrame && (parent instanceof JFrame &&
(((JFrame)parent).getJMenuBar() == null || (((JFrame)parent).getJMenuBar() == null ||
!((JFrame)parent).getJMenuBar().isVisible())) || !((JFrame)parent).getJMenuBar().isVisible())) ||
parent instanceof JDialog; (parent instanceof JDialog &&
(((JDialog)parent).getJMenuBar() == null ||
!((JDialog)parent).getJMenuBar().isVisible()));
} }
} }
} }

View File

@@ -75,14 +75,14 @@ public class FlatWindowDecorationsTest
super.addNotify(); super.addNotify();
Window window = SwingUtilities.windowForComponent( this ); Window window = SwingUtilities.windowForComponent( this );
menuBarCheckBox.setEnabled( window instanceof JFrame ); menuBarCheckBox.setSelected( window instanceof JFrame );
menuBarEmbeddedCheckBox.setEnabled( window instanceof JFrame );
menuBarVisibleCheckBox.setEnabled( window instanceof JFrame );
maximizedBoundsCheckBox.setEnabled( window instanceof Frame ); maximizedBoundsCheckBox.setEnabled( window instanceof Frame );
addMenuButton.setEnabled( menuBarCheckBox.isEnabled() ); addMenuButton.setEnabled( menuBarCheckBox.isEnabled() );
addGlueButton.setEnabled( menuBarCheckBox.isEnabled() );
removeMenuButton.setEnabled( menuBarCheckBox.isEnabled() ); removeMenuButton.setEnabled( menuBarCheckBox.isEnabled() );
changeMenuButton.setEnabled( menuBarCheckBox.isEnabled() ); changeMenuButton.setEnabled( menuBarCheckBox.isEnabled() );
changeTitleButton.setEnabled( menuBarCheckBox.isEnabled() );
boolean windowHasIcons = (window != null && !window.getIconImages().isEmpty()); boolean windowHasIcons = (window != null && !window.getIconImages().isEmpty());
iconNoneRadioButton.setEnabled( windowHasIcons ); iconNoneRadioButton.setEnabled( windowHasIcons );
@@ -117,11 +117,12 @@ public class FlatWindowDecorationsTest
private void menuBarChanged() { private void menuBarChanged() {
Window window = SwingUtilities.windowForComponent( this ); Window window = SwingUtilities.windowForComponent( this );
if( window instanceof JFrame ) { if( window instanceof JFrame )
((JFrame)window).setJMenuBar( menuBarCheckBox.isSelected() ? menuBar : null ); ((JFrame)window).setJMenuBar( menuBarCheckBox.isSelected() ? menuBar : null );
window.revalidate(); else if( window instanceof JDialog )
window.repaint(); ((JDialog)window).setJMenuBar( menuBarCheckBox.isSelected() ? menuBar : null );
} window.revalidate();
window.repaint();
} }
private void menuBarEmbeddedChanged() { private void menuBarEmbeddedChanged() {
@@ -250,7 +251,7 @@ public class FlatWindowDecorationsTest
private void openDialog() { private void openDialog() {
Window owner = SwingUtilities.windowForComponent( this ); 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.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
dialog.add( new FlatWindowDecorationsTest() ); dialog.add( new FlatWindowDecorationsTest() );
dialog.pack(); dialog.pack();
@@ -258,6 +259,17 @@ public class FlatWindowDecorationsTest
dialog.setVisible( true ); 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() { private void decorationStyleChanged() {
int style; int style;
if( styleFrameRadioButton.isSelected() ) if( styleFrameRadioButton.isSelected() )
@@ -315,10 +327,10 @@ public class FlatWindowDecorationsTest
unifiedBackgroundCheckBox = new JCheckBox(); unifiedBackgroundCheckBox = new JCheckBox();
JPanel panel3 = new JPanel(); JPanel panel3 = new JPanel();
addMenuButton = new JButton(); addMenuButton = new JButton();
JButton addGlueButton = new JButton(); addGlueButton = new JButton();
removeMenuButton = new JButton(); removeMenuButton = new JButton();
changeMenuButton = new JButton(); changeMenuButton = new JButton();
JButton changeTitleButton = new JButton(); changeTitleButton = new JButton();
menuBarEmbeddedCheckBox = new JCheckBox(); menuBarEmbeddedCheckBox = new JCheckBox();
colorizeMenuBarCheckBox = new JCheckBox(); colorizeMenuBarCheckBox = new JCheckBox();
menuBarVisibleCheckBox = new JCheckBox(); menuBarVisibleCheckBox = new JCheckBox();
@@ -344,6 +356,7 @@ public class FlatWindowDecorationsTest
iconTestAllRadioButton = new JRadioButton(); iconTestAllRadioButton = new JRadioButton();
iconTestRandomRadioButton = new JRadioButton(); iconTestRandomRadioButton = new JRadioButton();
JButton openDialogButton = new JButton(); JButton openDialogButton = new JButton();
JButton openFrameButton = new JButton();
menuBar = new JMenuBar(); menuBar = new JMenuBar();
JMenu fileMenu = new JMenu(); JMenu fileMenu = new JMenu();
JMenuItem newMenuItem = new JMenuItem(); JMenuItem newMenuItem = new JMenuItem();
@@ -587,7 +600,13 @@ public class FlatWindowDecorationsTest
//---- openDialogButton ---- //---- openDialogButton ----
openDialogButton.setText("Open Dialog"); openDialogButton.setText("Open Dialog");
openDialogButton.addActionListener(e -> openDialog()); 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 ======== //======== menuBar ========
{ {
@@ -781,8 +800,10 @@ public class FlatWindowDecorationsTest
private JCheckBox menuBarCheckBox; private JCheckBox menuBarCheckBox;
private JCheckBox unifiedBackgroundCheckBox; private JCheckBox unifiedBackgroundCheckBox;
private JButton addMenuButton; private JButton addMenuButton;
private JButton addGlueButton;
private JButton removeMenuButton; private JButton removeMenuButton;
private JButton changeMenuButton; private JButton changeMenuButton;
private JButton changeTitleButton;
private JCheckBox menuBarEmbeddedCheckBox; private JCheckBox menuBarEmbeddedCheckBox;
private JCheckBox colorizeMenuBarCheckBox; private JCheckBox colorizeMenuBarCheckBox;
private JCheckBox menuBarVisibleCheckBox; private JCheckBox menuBarVisibleCheckBox;

View File

@@ -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 { new FormModel {
contentType: "form/swing" contentType: "form/swing"
@@ -52,6 +52,9 @@ new FormModel {
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "addGlueButton" name: "addGlueButton"
"text": "Add glue" "text": "Add glue"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "addGlue", false ) ) addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "addGlue", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1" "value": "cell 0 1"
@@ -79,6 +82,9 @@ new FormModel {
add( new FormComponent( "javax.swing.JButton" ) { add( new FormComponent( "javax.swing.JButton" ) {
name: "changeTitleButton" name: "changeTitleButton"
"text": "Change title" "text": "Change title"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "changeTitle", false ) ) addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "changeTitle", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4" "value": "cell 0 4"
@@ -338,7 +344,15 @@ new FormModel {
"text": "Open Dialog" "text": "Open Dialog"
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "openDialog", false ) ) addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "openDialog", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, 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 ) { }, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 ) "location": new java.awt.Point( 0, 0 )

View File

@@ -344,6 +344,7 @@ TitlePane.background = #0f0
TitlePane.inactiveBackground = #080 TitlePane.inactiveBackground = #080
TitlePane.foreground = #00f TitlePane.foreground = #00f
TitlePane.inactiveForeground = #fff TitlePane.inactiveForeground = #fff
TitlePane.borderColor = #f00
#---- ToggleButton ---- #---- ToggleButton ----