File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Issues arising from Glass Pane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issues arising from Glass Pane" Watch "Issues arising from Glass Pane" New topic
Author

Issues arising from Glass Pane

Russell Tristen
Greenhorn

Joined: Nov 29, 2009
Posts: 3
Hi,

Whilst I've been searching and reading these forums for a couple of months now, I've had to register as I've got an issue that Googling just can't fix!

I have a main panel on the frame, and a transparent glass panel over this. Here are the main steps in my program:

1) Glasspane visibility set to false. I then draw circles upon the main panel
2) Glasspane visibility set to true and update text on this (which should appear 'above' the circles)
3) Glasspane visibility set to false and I recolour these circles

Problem is, as soon as I update the text on the glasspanel, the circles beneath disappear, not appearing again until I redraw them. I know that I should be using threading, so I can't understand why this doesn't work:



Anybody got any tips? Thank you!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I'm not sure tha that you've posted enough code to help us figure out what's not working. I wonder if you're not doing your drawing in a JPanel (or other JComponent)'s paintComponent method?
James Filson
Greenhorn

Joined: Nov 24, 2009
Posts: 11
.
Russell Tristen
Greenhorn

Joined: Nov 29, 2009
Posts: 3
Thank you Pete. My code is very large so reducing it down to a workable example will take a while, I'll get started. In the mean time, it may be useful to know that:

I draw the circles on the main panel by:



and I am 'drawing' on the glass panel by updating the text in a JLabel present on it:



I've had a look in a few of my reference books and is it possible that I need to override the glass pane's paint method? If I set it's visibility to true before I start drawing the circles, it appears beneath these circles. Why isn't the glass pane always staying on top?

Sorry about all the questions - I hope you can help me further! Thanks again.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
I draw the circles on the main panel by: ....


And that is exactly what Pete suggested you should NOT be doing.

Custom painting is done by overriding the paintComponent() method. Read the section from the Swing tutorial on Custom Painting for more information, or search the forum/web for examples.
Mikko Kohtamäki
Ranch Hand

Joined: Dec 13, 2008
Posts: 112
And invokeLater can be (or should be) called only from EDT thread.

Use invokeAndWait (when calling from another thread) to do work in EDT, like the documentation says under the link...

And your above code can be written like
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Mikko Kohtamäki wrote:And invokeLater can be (or should be) called only from EDT thread.

Nonsense. invokeLater can be called from any thread, including the EDT. It is used if you want something to be done on the EDT but it can finish at some later time. invokeAndWait cannot be called from the EDT (throws an exception), and is used if you need the GUI update to be done before continuing with the remainder of the thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Russell Tristen
Greenhorn

Joined: Nov 29, 2009
Posts: 3
Thank you all for your replies. I have done as suggested and all is now fixed
 
 
subject: Issues arising from Glass Pane