• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JMenuItems Disappear After repaint()

 
D. Brown
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, this takes care of it:

menuBar.updateUI();

Is that the best solution?
 
Darryl Burke
Bartender
Posts: 5136
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
D. Brown
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for one.jpg]
Graphic File
 
D. Brown
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should be, call to updateUI(), not call to update().
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic