Created
May 12, 2024 07:13
-
-
Save aparajita/fd803d16aa33094f58f307acea9babec to your computer and use it in GitHub Desktop.
setDefaultMenuBar bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.KeyEvent; | |
public class Main extends JFrame { | |
public Main() { | |
var fixed = false; | |
if (fixed) { | |
System.setProperty("apple.laf.useScreenMenuBar", "true"); | |
} | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setSize(300, 300); | |
JMenuBar menuBar = new JMenuBar(); | |
if (fixed) { | |
setJMenuBar(menuBar); | |
} else { | |
Desktop.getDesktop().setDefaultMenuBar(menuBar); | |
} | |
JMenu menu = new JMenu("Test"); | |
menuBar.add(menu); | |
var action = new DeleteAction(); | |
menu.add(action); | |
setVisible(true); | |
} | |
public static void main(String[] args) { | |
new Main(); | |
} | |
static class DeleteAction extends AbstractAction { | |
public DeleteAction() { | |
putValue(Action.NAME, "Delete"); | |
var keystroke = KeyStroke.getKeyStroke( | |
KeyEvent.VK_BACK_SPACE, | |
Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx() | |
); | |
putValue(Action.ACCELERATOR_KEY, keystroke); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
System.out.println("Delete pressed"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment