I am having some problems regarding to the JMenuBar and I cant seem to figure it out.
I will start with an abriviation of the problem: The program consists of a JFrame, a JDialog and a JMenuBar. Initially, you will get to see a JFrame with the JMenuBar in the top. But at some point, the JDialog will pop up where a user can fill in some text fields. The problem that I'm having is that as soon as the focus goes to the JDialog, the JMenuBar disappears. What I want is that the JMenuBar stays in the top of the screen all the time, except if the whole program is NOT in focus. Here are 2 screenshots, in the first screen shot, the JFrame is selected and in the other one the JDialog is selected.
So what i actually want is instead of only seeing the JMenuBar when the focus is on the JFrame, i want to see the JMenuBar all the time. Since a JDialogs can not have the JMenuBar in the top, like a JFrame has, i decided not to have multiple JMenuBars, but just the one that should be visible all the time.
At last i will give a part of the code that is as small as possible (and still working) and also contains the problem:
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
/**
* #author Guus Leijsten
* #created Oct 27, 2012
*/
public class MenuBarProblem extends JFrame {
public MenuBarProblem() {
super("Frame");
this.setMinimumSize(new Dimension(270, 200));
this.setPreferredSize(new Dimension(800, 530));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JRootPane root = this.getRootPane();
//Menu
JMenu fileMenu = new JMenu("File");
JMenuItem file_exit = new JMenuItem("Exit");
file_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
file_exit.setToolTipText("Exit application");
file_exit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(file_exit);
JMenuBar menu = new JMenuBar();
menu.add(fileMenu);
root.setJMenuBar(menu);
this.setVisible(true);
JDialog d = new JDialog(this, "Dialog");
d.setSize(200, 100);
d.setLocation(0, (int)root.getContentPane().getLocationOnScreen().getY());
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.setVisible(true);
}
public static void main(String[] args) {
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("mac") >= 0) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
new MenuBarProblem();
}
}
If I can be honoust, i think that the problem lies in the part of JRootPane. But we'll see ;)
Did anyone else encountered this problem and managed to solve it alrady, or is there anybody that wants to give it a shot?
Thanks in advance!
Added content:
In the following example I will show a version that gives some functionality to the play.
This is the program i'm making:
The second image shows the state in which the right menu is undocked.
Obviously the JMenuBar should still be visible and operational because without it, a lot of functionalities of the program will be disabled.
At this point i'm starting to think that it is impossible for the JMenuBar to stay visible when the dialog (undocked menu) is undocked, and focussed on.
I know that the JMenuBar on a JDialog can not be in the mac osx style (top of screen), so are there any other techniques i can use for undocking, which does give me a mac osx style JMenuBar?
One key to solving this problem, pun intended, is to let a key binding share a common menu action, as shown below. Note how a menu item, your dialog's content and an (otherwise superfluous) button can all use the same Action instance. A few additional notes:
Kudos for using getMenuShortcutKeyMask().
Swing GUI objects should be constructed and manipulated only on the event dispatch thread (EDT).
System properties should be set before starting the EDT.
Make the dialog's setLocation() relative to the frame after its geometry is known.
A common Mac idiom uses the following predicate:
if (System.getProperty("os.name").startsWith("Mac OS X") {…}
See also this example.
For local use in the dialog itself, also consider JToolBar.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
/**
* #see https://stackoverflow.com/a/13100894/230513
*/
public class MenuBarProblem extends JFrame {
private static final int MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
private static final String exitName = "Exit";
private static final KeyStroke exitKey =
KeyStroke.getKeyStroke(KeyEvent.VK_W, MASK);
private final ExitAction exitAction = new ExitAction(exitName);
public MenuBarProblem() {
super("Frame");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenu fileMenu = new JMenu("File");
JMenuItem fileExit = new JMenuItem(exitAction);
fileMenu.add(fileExit);
JMenuBar menu = new JMenuBar();
menu.add(fileMenu);
JDialog d = new JDialog(this, "Dialog");
JPanel p = new JPanel();
p.getInputMap().put(exitKey, exitName);
p.getActionMap().put(exitName, exitAction);
p.add(new JButton(exitAction));
d.add(p);
d.pack();
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.setJMenuBar(menu);
this.pack();
this.setSize(new Dimension(320, 240));
this.setLocationByPlatform(true);
this.setVisible(true);
d.setLocation(this.getRootPane().getContentPane().getLocationOnScreen());
d.setVisible(true);
}
private static class ExitAction extends AbstractAction {
public ExitAction(String name) {
super(name);
this.putValue(Action.MNEMONIC_KEY, exitKey.getKeyCode());
this.putValue(Action.ACCELERATOR_KEY, exitKey);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new MenuBarProblem();
}
});
}
}
Solved!
Using a JFrame with the use of setAlwaysOnTop(true) gives me the desired effect of having a JMenuBar when the focus changes.
Related
I have a simple java swing application. It's mostly used on Macos, so I'm trying to add a default menu bar to it through Desktop.getDesktop().setDefaultMenuBar(...). I defined a menu bar with a "File" menu and a "New" menu item with an action listener. Using the mouse to click on File->New calls the listener's actionPerformed() event as expected.
I tried to attach the standard accelerator to the menu item (Command-N on a mac). Clicking on the "File" menu now displays "New" with the expected accelerator next to it. However, when I actually type Command-N, the action listener isn't called. The only visible effect of typing Command-N is that the "File" menu item briefly flickers.
Edit: This seems to be related to the fact the menu is being set through Desktop.setDefaultMenuBar(). If I attach the menu to the JFrame, then accelerators work correctly. However, I'm using Desktop.setDefaultMenuBar() to define a menu that appears even when no other windows are open.
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class Scratch {
static class MainMenu extends JMenuBar implements ActionListener {
public MainMenu() {
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
JMenuItem item = new JMenuItem("New");
item.setMnemonic(KeyEvent.VK_N);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, keyMask));
item.addActionListener(this);
fileMenu.add(item);
this.add(fileMenu);
}
#Override
public void actionPerformed(ActionEvent e) {
System.err.println("actionPerformed " + e);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Hello Stackoverflow!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello Stackoverflow!");
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Desktop.getDesktop().setDefaultMenuBar(new MainMenu());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
The call to Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx() is based on this blog entry.
Can anyone see what I'm doing wrong?
I'm testing with Amazon coretto 11 if it matters.
Here is the minimal working application :
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class JMenuItemReuse {
public static void main(String[] args) {
SwingUtilities.invokeLater(TestFrame::new);
}
}
class TestFrame extends JFrame {
public TestFrame() {
super();
JPanel panel = new JPanel();
JPopupMenu menu1 = new JPopupMenu();
JPopupMenu menu2 = new JPopupMenu();
JMenuItem item1 = new JMenuItem("reused");
JMenuItem item2 = new JMenuItem("not reused");
menu1.add(item1);
menu2.add(item1); // works if this line is commented
menu2.add(item2);
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (SwingUtilities.isRightMouseButton(e)) {
menu1.show(panel, e.getX(), e.getY());
} else {
menu2.show(panel, e.getX(), e.getY());
}
}
});
panel.add(new JLabel("popup-test"));
add(panel);
setPreferredSize(new Dimension(400, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
The problem is that the popup menus do not work as expected when at least one menu item is reused. It is not a big issue, can be easily avoided by duplicating the reused menu item, but I am still wondering why does it happen. Any ideas?
A JMenuItem belongs to one, and only one, JPopupMenu (or any other menu). You cannot add a Swing component to more than one container; if you do, then the component will automatically be removed from the previous container.
Actaully if you want, you can create Actions. Actions can be shared and added to multiple components (JMenuItems, JButtons etc). You can even enable/disable the Action which will enable/disable all the components at the same time.
I have a problem to reopen one JInternalFrame in my code. I select in MenuBar option "Cadastro"->"Cadastro de Veículos" and this action open one screen to insert a vehicle. However, if I close this screen and try reopen it again I can't anymore.
Below, two code that I'm using.
First, JMain (that is my JFrame):
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
import javax.swing.event.InternalFrameListener;
public class JMain extends JFrame implements ActionListener{
/**
* Create the panel.
*/
private JMenuBar menuBar = new JMenuBar();
private JMenu firstMenu = new JMenu("Cadastro");
private JMenu secndMenu = new JMenu("Indicadores");
private JMenu thirdMenu = new JMenu("Agendamentos");
private JMenu fourthMenu = new JMenu("Relatórios");
private JMenuItem cadVeiculos = new JMenuItem("Cadastro de Veículos");
private JMenuItem cadMotoristas = new JMenuItem("Cadastro de Motoristas");
private JMenuItem cadCargas = new JMenuItem("Cadastro de Cargas");
private JMenuItem newExit = new JMenuItem("Sair");
public JDesktopPane jdPane = new JDesktopPane();
JCadVeiculos telaCadVeiculos;
public static void main (String args []){
JMain jmain = new JMain();
}
public JMain() {
jdPane.setBackground(Color.LIGHT_GRAY);
setTitle("Gtrix - Version 0.1");
setSize(600, 500);
getContentPane().add(jdPane);
setJMenuBar(menuBar);
menuBar.add(firstMenu);
menuBar.add(secndMenu);
menuBar.add(thirdMenu);
menuBar.add(fourthMenu);
firstMenu.add(cadVeiculos);
firstMenu.add(cadMotoristas);
firstMenu.add(cadCargas);
firstMenu.addSeparator();
firstMenu.add(newExit);
cadVeiculos.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == cadVeiculos){
if(telaCadVeiculos == null){
telaCadVeiculos = new JCadVeiculos(this);
}
//telaCadVeiculos.show();
//telaCadVeiculos.setDefaultCloseOperation(JCadVeiculos.DO_NOTHING_ON_CLOSE);
jdPane.moveToFront(telaCadVeiculos);
}
}
}
and JCadVeiculos (my JInternalFrame):
package ui;
import java.awt.EventQueue;
import java.awt.Menu;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
public class JCadVeiculos extends JInternalFrame {
private JMain telaPrincipal;
/**
* Create the frame.
*/
public JCadVeiculos(JMain telaPrincipal) {
super("", true, true, false);
setSize(600,500);
setTitle("Cadastro de Veículos");
setVisible(true);
this.telaPrincipal = telaPrincipal;
telaPrincipal.jdPane.add(this);
}
}
Please, help me! :)
You will need to make the JInternalFrame visible again, as it was "hidden" when it was closed, for example...
public void actionPerformed(ActionEvent evt) {
if(evt.getSource() == cadVeiculos){
if(telaCadVeiculos == null){
telaCadVeiculos = new JCadVeiculos(this);
}
if (!telaCadVeiculos.getParent().equals(jdPane)) {
jdPane.add(telaCadVeiculos);
}
telaCadVeiculos.setVisible(true);
//telaCadVeiculos.show();
//telaCadVeiculos.setDefaultCloseOperation(JCadVeiculos.DO_NOTHING_ON_CLOSE);
jdPane.moveToFront(telaCadVeiculos);
}
}
On a personal note, I find...
telaPrincipal.jdPane.add(this);
in you JCadVeiculos class worrying. Your JCadVeiculos shouldn't really be making decisions about how it might be getting used and I'd personally refrain from making components publicly visible in this way, as it leads to possible misuse on the part of other developers.
The responsibility for the management of the JDesktopPane lies with the JMain class, it should be deciding what gets added to the JDesktopPane and when
Just saying.
You might also like to take a look at Initial Threads and make sure you're initialising your UI from within the context of the Event Dispatching Thread
I am pretty new to Swing, any help appreciated.
I have the following situation:
A "Main" Class where I define my main JPanel and default Label text.
A "GUILabel" Class (extends JLabel) where I define the look of the Text Labels.
A "popupmenu" Class (extends JPopupMenu) where I define the content of the popupmenu.
Target:
When I right-click on a panel the popupMenu should appear (this already works).
When I choose a menu item of this popupMenu, the text of the label I clicked on should change.
I guess its currently not working (I am sorry this code isn't complete - this is my 5th attempt), because I create the popup menu Once in the Main Class. Then I am adding this popup menu to each Label. So I guess thats why getInvoker() returns null in the popup menu class.
But do I really have to create a new popupmenu for each JLabel? Cant this single menu just handle all Components assigned to?
Main Frame:
package popupshit;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.border.BevelBorder;
public class Model implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 3));
frame.setVisible(true);
GUIPopupMenu popup = new GUIPopupMenu(9);
JLabel label1 = new GUILabel();
label1.setComponentPopupMenu(popup);
JLabel label2 = new GUILabel();
label2.setComponentPopupMenu(popup);
JLabel label3 = new GUILabel();
label3.setComponentPopupMenu(popup);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent e) {
((JLabel) e.getSource()).setText("" + e.getActionCommand());
}
}
PopupMenu:
package popupshit;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class GUIPopupMenu extends JPopupMenu {
public GUIPopupMenu(int numbers) {
for (int i = 1; i < numbers; i++) {
JMenuItem popMenuItem = new JMenuItem("" + i);
popMenuItem.addActionListener ((ActionListener) this.getInvoker());
System.out.println(this.getParent());
this.add(new JMenuItem("" + i));
}
this.addSeparator();
this.add(new JMenuItem("remove"));
}
}
GUILabel:
package popupshit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.BevelBorder;
public class GUILabel extends JLabel implements ActionListener {
public GUILabel() {
this.setBorder(new BevelBorder(BevelBorder.LOWERED));
this.setVisible(true);
this.setPreferredSize(new Dimension(50, 50));
this.setText("0");
}
#Override
public void actionPerformed(ActionEvent e) {
this.setText((String) e.getActionCommand());
JOptionPane.showMessageDialog(null, "worked!" );
}
}
invoker is null because until the popup menu is a actually invoked, it has no invoker.
Instead, add a simple ActionListener to the menu item which, when invoked, uses the invoker property to determine which should occur.
My personal preference would be to create a new instance of the popup menu for each component separately, passing a reference of the component in question or some other controller as required...but that's me...
Add an ActionListener to the menu item something like:
#Override
public void actionPerformed(ActionEvent e)
{
Component c = (Component)e.getSource();
JPopupMenu popup = (JPopupMenu)c.getParent();
JLabel label = (JLabel)popup.getInvoker();
...
}
Don't extend JPopupMenu just to add a few menu items to the popup.
For a program, I was using a KeyListener to add something to an ArrayList when pressing the button '1'. Objects in this list are being visualised constantly. With the KeyListener, this worked fluently, even when keeping the button pressed.
Later, I added a JMenuBar to the GUI. Adding something to the ArrayList now has an own JMenuItem with its accelerator set to the KeyStroke '1' and an ActionListener which performs the same stuff than the KeyListener before. However, the performance now is very bad. Keeping '1' pressed is going to lag extremely, it's very slow compared to the KeyListener.
Why is it so slow? Am I doing something wrong? Is there a better way?
...
AL al = new AL();
menu.add(createMenuItem("Add", KeyEvent.VK_1, al));
}
private JMenuItem createMenuItem(String text, int key, ActionListener al){
JMenuItem menuItem = new JMenuItem(text);
menuItem.setAccelerator(KeyStroke.getKeyStroke(key, 0));
menuItem.addActionListener(al);
return menuItem;
}
private class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
int keycode = ((JMenuItem)e.getSource()).getAccelerator().getKeyCode();
bla(keycode);
}
}
It looks like the slowdown is how the menu accelerators are handled. It might be L&F or even OS since when I profile it, there is no hotspot in the Java code (WindowsXP) dependent. A workaround could be to add the key binding to the root pane instead of using an menu accelerator.
Press '1' to trigger KeyListener on button (fast)
Press '2' to trigger menu accelerator (slow)
Press '3' to trigger KeyBinding on button (fast)
Press '4' to trigger KeyBinding on root pane (fast)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
public class TestKeySpeed {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
final JTextArea area = new JTextArea(20, 40);
area.setEditable(false);
JButton button = new JButton("Just something that has focus");
button.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_1) {
area.append("1");
}
}
});
AbstractAction action = new AbstractAction("Add") {
{
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('2'));
}
#Override
public void actionPerformed(ActionEvent e) {
area.append("2");
}
};
button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke('3'), "add3");
button.getActionMap().put("add3", action);
JMenu menu = new JMenu("File");
menu.add(action);
JMenuBar bar = new JMenuBar();
bar.add(menu);
JFrame frame = new JFrame("Test");
frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke('4'), "add4");
frame.getRootPane().getActionMap().put("add4", action);
frame.setJMenuBar(bar);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(area));
frame.getContentPane().add(button, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
button.requestFocusInWindow();
}
});
}
}
Something else is slowing your application. This example remains responsive with over a dozen Key Bindings. One useful approach is to let menu items and other components share the same actions, as shown here and here.
Addendum: Instead of implementing ActionListener, implement Action by extending AbstractAction, which will make it easier to manage the accelerator key.