Skip to main content

SystemTray doesn't work with JMenu using apple.laf.useScreenMenuBar on Mac

4 replies [Last post]
calbot
Offline
Joined: 2006-12-16

Hi All,

I'm trying to use SystemTray on mac with the screen menubar using the Apple System property apple.laf.useScreenMenuBar=true.

The problem is if you open a modal dialog with this configuration the menu items in your screen JMenuBar become disabled after you close the modal dialog.

I couldn't find a fix but a work around I found was to re-create the main JFrame which has the JMenuBar each time the modal JDialog is closed. That way the JMenuBar is re-created as well.

Perhaps I'm doing something wrong. Has anyone else come across this problem? Any solutions?

You will see that if you comment either of the following lines from my main method the menubar items don't get disabled
System.setProperty("apple.laf.useScreenMenuBar", "true");
createTrayIcon();

See a test application which demonstrates the bug below:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;

/**
* When you click [Menu->Make Dialog] to open a modal dialog then close the
* modal dialog the [Menu->Make Dialog] menu item will become disabled. This
* only happens when you use the apple.laf.useScreenMenuBar System property.
*/
public class TestJdicNonWorkingMenu extends JFrame
{
public TestJdicNonWorkingMenu()
{
super("Frame ");
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("Menu");
JMenuItem mi = new JMenuItem("Make Dialog");
menu.add(mi);
mb.add(menu);

mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JDialog d = new JDialog(TestJdicNonWorkingMenu.this, "Test Dialog", true);
d.setSize(400, 400);
d.setVisible(true);

}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setJMenuBar(mb);

}

public static void main(String[] args)
{
System.setProperty("apple.laf.useScreenMenuBar", "true");
final TestJdicNonWorkingMenu f = new TestJdicNonWorkingMenu();
f.setVisible(true);

createTrayIcon();
}

private static void createTrayIcon()
{
SystemTray tray = SystemTray.getDefaultSystemTray();
TrayIcon ti = new TrayIcon(new Icon()
{

public int getIconHeight()
{
return 16;
}

public int getIconWidth()
{
return 16;
}

public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(Color.BLACK);
g.fillRect(x, y, getIconWidth(), getIconHeight());
}
});
ti.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Testing123");
}
});

tray.addTrayIcon(ti);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
calbot
Offline
Joined: 2006-12-16

...fix JDIC or the Mac JRE for that matter. Which ever makes sense :)

calbot
Offline
Joined: 2006-12-16

I found the problem and a solution.
I changed the parent frame parameter in the call to the super constructor from [b](Frame) null[/b] to [b]JOptionPane.getRootFrame()[/b]

Then I called [b]JOptionPane.setRootFrame(this);[/b] in my JFrame constructor.

static class PopupParent extends JDialog
{
public PopupParent()
{
super([b]JOptionPane.getRootFrame(),[/b] "JDIC Tray Icon");
try
{
Method setAlwaysOnTop = this.getClass().getMethod("setAlwaysOnTop", new Class[]{boolean.class});
setAlwaysOnTop.invoke(this, new Object[]{Boolean.TRUE});
}
catch (NoSuchMethodException e)
{
}
catch (Exception e)
{
e.printStackTrace();
}
this.setUndecorated(true);
this.setBounds(0, 0, 0, 0);
}
}

There always seems to be problems when you don't give dialogs a proper parent. I've seen it when you switch back to Swing apps on windows (without using alt+tab) and you aren't brought to expected JDialog because the dialog didn't have its parent set.

My solution is probably not ideal but I hope it gives others ideas on how to update the JDIC code to fix it.

Thanks.

robross
Offline
Joined: 2003-06-13

I ran your code on my MacBook Pro and could not duplicate your problem. I can select the Make Dialog menu item, see the menu items are disabled, and when I close the dialog, they are enabled again.

I'm on Mac OS 10.4.11 on a 2.4 GHz Intel Core 2 Duo, running Java build 1.5.0_07-164.

So it works fine for me. Maybe it's a bug in the JVM on the platform you're running it on?

calbot
Offline
Joined: 2006-12-16

Yes, my environment is different.

Model Name: MacBook Pro
Model Identifier: MacBookPro4,1
Processor Name: Intel Core 2 Duo
Processor Speed: 2.5 GHz
Mac OS X 10.5.4 (9E17)
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05-237)
Java HotSpot(TM) Client VM (build 1.5.0_13-119, mixed mode, sharing)

I've had reports of a user of my software experiencing the problem too.
Can anyone else reproduce the problem?
Anyone have any ideas for solving/investigating this issue?

Thanks