Results 1 to 5 of 5
- 10-28-2011, 03:27 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Destroy a JLabel from within its paintComponent
Dear coders,
I have a tricky question about a JLabel.
In an application I have an object that sets up a JLayeredPane. On this pane some JLabel extended objects are drawn. Once the JLabel objects have been added on the pane, I can change their appearance and location from within their own paintComponent method implementations.
However, at some point I need to remove the JLabel objects. Is there a way to do this from within the paintComponent() too?
I know how to remove them from the JLayeredPane by getting the components using getComponents() and comparing them using instanceof, and finally using the remove() method of the pane. But it would make for a lot less messy program if each JLabel could take care of its own destruction.
Megathanks in advance,
eyeBoth
- 10-28-2011, 03:46 PM #2
Re: Destroy a JLabel from within its paintComponent
Probably a bad approach. Painting is for painting, not for logic. You might even be in danger of causing a stack overflow.
Probably, but again, it's probably a bad approach. Don't mix painting and logic.
Why don't you just keep around the instance of the JLabel and call the appropriate methods on that?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Re: Destroy a JLabel from within its paintComponent
You as the programmer do not have absolute control over when a component's paintComponent method is called since it can be called at your suggestion (via repaint() for instance) or at the discretion of the JVM for instance if the operating system tells it to repaint "dirty regions" on your GUI. Not only that, you can't even say for certain that a paintComponent method will be called if you suggest it via repaint() since the paint manager may decide not to do it if for instance it gets behind due to multiple repaints in a row. The third issue is that you want your painting methods to be as blindingly quick and as small as possible so as to not reduce the perceived responsiveness of your application.
What I'm getting at, for these reasons and for others as well, Kevin's suggestions above are good ones: do not use paint or paintComponent methods for your program logic. There are better ways to decide when and how to change the properties of components, so just don't do this inside of paintComponent.
- 10-29-2011, 11:37 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Destroy a JLabel from within its paintComponent
Hello Kevin & Fubarable, thank you for your quick replies.
Well, actually each painted object has another dedicated object (= the model) managing the logic of what is represented. The JLabel extended object only takes care of the interfacing. So when the states of the model object change, based on input from the visual object, or from interaction with other model objects, the visual object changes appearance and location. This works fine.
Originally Posted by KevinWorkman
But when the model is to be discarded, the visual object needs to be removed from the pane. And for this feat, I have to come up with increasingly difficult mechanisms in the main object (the one that contains the JLayeredPane) to clean up every link to this visual object. So in my opinion it would make sense to have the visual object be put in charge of its removal.
I understand. Could I do it outside the paintComponent() method but still in the visual object? Mind you that I don't have a reference to the JLayeredPane from within the visual nor the model objects.
Originally Posted by Fubarable
-
Re: Destroy a JLabel from within its paintComponent
I think you may be confusing the view with the model. I would remove the logical representation of the object from the model and have the model's listener (the control) remove the visual representation from the view. How to do this specifically will depend on your program's structure.
Similar Threads
-
Destroy jtextarea and jbutton.
By sublixt in forum New To JavaReplies: 3Last Post: 08-25-2011, 07:06 AM -
How to destroy a Process in windows?
By hahanizhu in forum Threads and SynchronizationReplies: 2Last Post: 04-26-2011, 10:19 AM -
Jbutton to destroy process
By peterhammond in forum AWT / SwingReplies: 1Last Post: 04-19-2011, 06:58 PM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Destroy object
By anjanesh in forum New To JavaReplies: 11Last Post: 01-02-2010, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks