aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JMenuItems Disappear After repaint() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenuItems Disappear After repaint()" Watch "JMenuItems Disappear After repaint()" New topic
Author

JMenuItems Disappear After repaint()

D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
I have written a dice roller program with "File" and "Help" menus. The File menu has "New Game" and "Exit" menu items. When the program first starts, I can click on "File" and see the "New Game" and "Exit" menu items, but after the first dice roll, I can't see the "Exit" menu item any more. I'm guessing that the repaint() call has done something to cover it up. The accelerator keys still work.

Does this problem sound familiar? I can't find any obvious refernce to it when I Google or search this site. I can post code if necessary.

I have tried this with same result with:

Eclipse Juno, Linux Iced Tea 1.6
Eclipse Helios, Linux Iced Tea 1.6
Eclipse Juno Windows 32 (Vista), Java 1.7

Thanks for any advice.

D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
Ok, this takes care of it:

menuBar.updateUI();

Is that the best solution?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

No. the updateUI() method should only be needed after changing the Look-and-Feel.

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

edit
1. Does your code respect Swing's single threaded rule?
2. does your GUI mix AWT and Swing components?


luck, db
There are no new questions, but there may be new answers.
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
I did my best to trim the code down and do the SSCCE thing. Below are the DiceRoller (main) class and the Roll Dice class. I have also attached one graphic image of a die. I guess you can copy it 5 times and name the new files "two.jpg" etc.

There are still two calls to update(), but they're commented out.

While trimming the code down and making sure it would still compile, I ran into something interesting. When I removed the scroll area from the center region, the problem went away. When I put the scroll area back in, the problem re-appeared. Why would that happen??

The teacher for the class I'm taking compiled the program on his computer, and he did not see the problem. I think he's using a Mac.

When I first start the program, I can get to File | Exit ok. After the first dice roll, I can get to File, but Exit is gone.

All insight is appreciated.

I tried to attach the source as files. But I got a scolding from the forum program, first saying that I couldn't attach files with extension .java, and then that I couldn't attach files with an extension .txt. I couldn't find anything about this in a FAQ, but I concluded that source should be posted in the message. So it's below.

Dave

==============DiceRoller.java==============







[one.jpg]

D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
That should be, call to updateUI(), not call to update().
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
as Darryl hinted/suggested, the problem is this line

class RollDice extends Canvas {

read how to do your drawing/painting here
http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155


1. there no reason to use AWT Canvas (excluding for Multimedia, OpenGL/CL, or there are a few good 3rd party libraries based on AWT)

2. AWT Canvas is heavyweight Component, then is basically always on top of lightweight JMenu & JMenuItem, have to change lightweight status for Swing JComponents (I'd not suggest)

3. put Icon /ImageIcon to the JLabel , for dices could be JButton the best of ways (layed by GridLayout)

4. in the case that you want to use painting then to change Canvas to the JPanel or JComponent, have to override PreferredSize for resizable contents
D. Brown
Greenhorn

Joined: Sep 20, 2012
Posts: 12
That was interesting reading. I'm still digesting it. I think I get the heavyweight/lightweight business now, and how it's causing the problem.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JMenuItems Disappear After repaint()