macOS: Fixed NPE if using JMenuBar in JInternalFrame and macOS screen menu bar is enabled (issue #90)

This commit is contained in:
Karl Tauber
2020-04-30 13:38:23 +02:00
parent 5553fd6538
commit 5c0de9aa1c
5 changed files with 35 additions and 3 deletions

View File

@@ -19,6 +19,8 @@ FlatLaf Change Log
- Show mnemonics always when a menu bar is active or a popup menu is visible. - Show mnemonics always when a menu bar is active or a popup menu is visible.
- Hide mnemonics if window is deactivated (e.g. <kbd>Alt+Tab</kbd> to another - Hide mnemonics if window is deactivated (e.g. <kbd>Alt+Tab</kbd> to another
window). (issue #43) window). (issue #43)
- macOS: Fixed NPE if using `JMenuBar` in `JInternalFrame` and macOS screen menu
bar is enabled (with `-Dapple.laf.useScreenMenuBar=true`). (issue #90)
## 0.33 ## 0.33

View File

@@ -38,6 +38,7 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JComponent; import javax.swing.JComponent;
@@ -306,9 +307,13 @@ public abstract class FlatLaf
UIDefaultsLoader.loadDefaultsFromProperties( getClass(), addons, getAdditionalDefaults(), isDark(), defaults ); UIDefaultsLoader.loadDefaultsFromProperties( getClass(), addons, getAdditionalDefaults(), isDark(), defaults );
// use Aqua MenuBarUI if Mac screen menubar is enabled // use Aqua MenuBarUI if Mac screen menubar is enabled
if( SystemInfo.IS_MAC && Boolean.getBoolean( "apple.laf.useScreenMenuBar" ) ) if( SystemInfo.IS_MAC && Boolean.getBoolean( "apple.laf.useScreenMenuBar" ) ) {
defaults.put( "MenuBarUI", "com.apple.laf.AquaMenuBarUI" ); defaults.put( "MenuBarUI", "com.apple.laf.AquaMenuBarUI" );
// add defaults necessary for AquaMenuBarUI
defaults.put( "MenuBar.backgroundPainter", BorderFactory.createEmptyBorder() );
}
// initialize text antialiasing // initialize text antialiasing
putAATextInfo( defaults ); putAATextInfo( defaults );

View File

@@ -49,6 +49,11 @@ public class FlatMenuBarUI
return new FlatMenuBarUI(); return new FlatMenuBarUI();
} }
/*
* WARNING: This class is not used on macOS.
* Do not add any functionality here.
*/
@Override @Override
protected void installKeyboardActions() { protected void installKeyboardActions() {
super.installKeyboardActions(); super.installKeyboardActions();

View File

@@ -53,6 +53,14 @@ public class FlatInternalFrameTest
if( iconCheckBox.isSelected() ) if( iconCheckBox.isSelected() )
internalFrame.setFrameIcon( new FlatFileViewFloppyDriveIcon() ); internalFrame.setFrameIcon( new FlatFileViewFloppyDriveIcon() );
if( menuBarCheckBox.isSelected() ) {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu( "I'm a Menu Bar" );
menu.add( new JMenuItem( "Menu Item" ) );
menuBar.add( menu );
internalFrame.setJMenuBar( menuBar );
}
JPanel panel = new JPanel() { JPanel panel = new JPanel() {
private final Color color = new Color( (int) (Math.random() * 0xffffff) | 0x20000000, true ); private final Color color = new Color( (int) (Math.random() * 0xffffff) | 0x20000000, true );
@@ -93,6 +101,7 @@ public class FlatInternalFrameTest
iconifiableCheckBox = new JCheckBox(); iconifiableCheckBox = new JCheckBox();
maximizableCheckBox = new JCheckBox(); maximizableCheckBox = new JCheckBox();
iconCheckBox = new JCheckBox(); iconCheckBox = new JCheckBox();
menuBarCheckBox = new JCheckBox();
titleLabel = new JLabel(); titleLabel = new JLabel();
titleField = new JTextField(); titleField = new JTextField();
createFrameButton = new JButton(); createFrameButton = new JButton();
@@ -152,6 +161,10 @@ public class FlatInternalFrameTest
iconCheckBox.setText("Frame icon"); iconCheckBox.setText("Frame icon");
paletteContentPane.add(iconCheckBox, "cell 0 2"); paletteContentPane.add(iconCheckBox, "cell 0 2");
//---- menuBarCheckBox ----
menuBarCheckBox.setText("Menu Bar");
paletteContentPane.add(menuBarCheckBox, "cell 1 2");
//---- titleLabel ---- //---- titleLabel ----
titleLabel.setText("Frame title:"); titleLabel.setText("Frame title:");
paletteContentPane.add(titleLabel, "cell 0 3"); paletteContentPane.add(titleLabel, "cell 0 3");
@@ -163,7 +176,7 @@ public class FlatInternalFrameTest
paletteContentPane.add(createFrameButton, "cell 1 4,alignx right,growx 0"); paletteContentPane.add(createFrameButton, "cell 1 4,alignx right,growx 0");
} }
desktopPane.add(palette, JLayeredPane.PALETTE_LAYER); desktopPane.add(palette, JLayeredPane.PALETTE_LAYER);
palette.setBounds(15, 25, 220, 185); palette.setBounds(15, 25, 275, 185);
} }
add(desktopPane, "cell 0 0,width 600,height 600"); add(desktopPane, "cell 0 0,width 600,height 600");
// JFormDesigner - End of component initialization //GEN-END:initComponents // JFormDesigner - End of component initialization //GEN-END:initComponents
@@ -180,6 +193,7 @@ public class FlatInternalFrameTest
private JCheckBox iconifiableCheckBox; private JCheckBox iconifiableCheckBox;
private JCheckBox maximizableCheckBox; private JCheckBox maximizableCheckBox;
private JCheckBox iconCheckBox; private JCheckBox iconCheckBox;
private JCheckBox menuBarCheckBox;
private JLabel titleLabel; private JLabel titleLabel;
private JTextField titleField; private JTextField titleField;
private JButton createFrameButton; private JButton createFrameButton;

View File

@@ -56,6 +56,12 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2" "value": "cell 0 2"
} ) } )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "menuBarCheckBox"
"text": "Menu Bar"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
} )
add( new FormComponent( "javax.swing.JLabel" ) { add( new FormComponent( "javax.swing.JLabel" ) {
name: "titleLabel" name: "titleLabel"
"text": "Frame title:" "text": "Frame title:"
@@ -77,7 +83,7 @@ new FormModel {
}, new FormLayoutConstraints( null ) { }, new FormLayoutConstraints( null ) {
"x": 15 "x": 15
"y": 25 "y": 25
"width": 220 "width": 275
"height": 185 "height": 185
"layer": 100 "layer": 100
} ) } )