• Post Reply Bookmark Topic Watch Topic
  • New Topic

Proper way to override painting on a JFrame

 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to override the paintComponent method of a JFrame, but I am getting a java.lang.stackOverflow error. I'm doing something wrong. My approach is to override the paintComponent method and override the show() as well. I call the base class show() method, before doing my stuff.

I have no problems with altering a JPanel, so I'm wondering if it has something to do with being a top level container.

Any ideas?

Thanks,
Aaron R>
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always compile with the deprecation option and I was under the impression the the show() method is depricated and that we should use setVisible(boolean) instead. Now I have no clue if that has anything to do with your problem or not.

If you give me some sample code I can see if I get the same error on my PC.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the JFrame api, scroll down to the section Methods inherited from class java.awt.Container you find the paint method. Following the link we see it is called to paint the child components of the container. There is no paintComponent method available in the JFrame api.

You can override the paint method for a JFrame but this requires care to avoid painting over components, menubar and insets.

A better approach is to override paintComponent in a JPanel and add the JPanel to the JFrame.

Look in the section Methods inherited from java.awt.Window to find the show method. This works okay for a JFrame. In the section Methods inherited from java.awt.Component is another show method that has been deprecated.
[ July 15, 2004: Message edited by: Craig Wood ]
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ha! I bet thats my issue. I've got a thread running the paintComponent method, which I thought I was overriding. Obviously not. I want to expressly override paint in my case, as there aren't any other components on my JFrame. I'm making a spash screen with a progress bar at the bottom.

I'll go poke about again and see what I can fix/break!

Regards,
Aaron R>
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm stumped. I think I'm missing something obvious or something I'm just ignorant about.

Here is the code I've got so far -



I switched to using a JWindow, instead of a JFrame, since I don't actually need anything from a JFrame anyway. I'm just trying to get a container that I can paint.

Any ideas as to whats going on here?

Regards,
Aaron R>
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm a bit far sighted and missed a few obvious things which were wrong. One of which is that my progress bar was set to draw vertically instead of the intended horizontal. :/ Oopps!

I'll post the fixed up code tommorow.

Regards!
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I'm a tad stumped again. I've got things working, except that I can't seem to get the JWindow off the screen now. Here is the latest -



I have a JFrame show up on the screen and only after I close it, does the JWindow close as well. I'm confused as to why that is.

Any ideas?
[ July 19, 2004: Message edited by: Aaron Roberts ]
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, never mind. I'm just dumb. I didn't realize I had overridden hide(). This obviously won't do anything if I don't call the super.hide(). :roll: C'est la vie!

Now to get the text working and get it finished!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!