1/22/09

JMenuItem not visible - JMenuItem behind Canvas

I was trying to make a GUI in java where I needed to have a JMenuBar (with some JMenuItems) and a Canvas. The problem I run into was that my JMenuItems were shown behind Canvas. I didn't know why so I searched the web to find the solution. I was not very lucky and for a few hours the only important "think" I found was that Canvas is heavyweight component (awt) and JMenuItem is lightweight (swing) component. And you only mix them if you know exactly what you are doing... So I "solved" that by only using awt components. How? It isn't difficult.
Just remove all J :-P
e.g.
JMenuItem = MenuItem
JMenuBar = MenuBar
setJMenuBar = setMenuBar
e.t.c
This can be done because swing has JMenuItem and awt has MenuItem e.t.c.

The line I had to change more was:

itemExit.setShortcut(
new MenuShortcut(
new KeyEvent
(this, 1 , 1 ,
KeyEvent.CTRL_MASK , KeyEvent.VK_X ,
KeyEvent.CHAR_UNDEFINED ).getKeyCode()
));

which became like this :

itemExit.setAccelerator(
KeyStroke.getKeyStroke (KeyEvent.VK_X, KeyEvent.CTRL_MASK)
);

or it could be:

itemExit.setShortcut(
new MenuShortcut(KeyStroke.getKeyStroke("X").getKeyCode())
);

That's all for now. That worked for me.
If you have any other similar problem please leave it as comment to help others too.
Thank you for visiting.

No comments: