Results 1 to 5 of 5
  1. #1
    eyeBoth is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: Destroy a JLabel from within its paintComponent

    Quote Originally Posted by eyeBoth View Post
    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.
    Probably a bad approach. Painting is for painting, not for logic. You might even be in danger of causing a stack overflow.

    Quote Originally Posted by eyeBoth View Post
    However, at some point I need to remove the JLabel objects. Is there a way to do this from within the paintComponent() too?
    Probably, but again, it's probably a bad approach. Don't mix painting and logic.

    Quote Originally Posted by eyeBoth View Post
    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.
    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!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  4. #4
    eyeBoth is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Destroy a JLabel from within its paintComponent

    Hello Kevin & Fubarable, thank you for your quick replies.

    Quote Originally Posted by KevinWorkman
    Probably a bad approach. Painting is for painting, not for logic.
    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.

    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.

    Quote Originally Posted by Fubarable
    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.
    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Destroy jtextarea and jbutton.
    By sublixt in forum New To Java
    Replies: 3
    Last Post: 08-25-2011, 07:06 AM
  2. How to destroy a Process in windows?
    By hahanizhu in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-26-2011, 10:19 AM
  3. Jbutton to destroy process
    By peterhammond in forum AWT / Swing
    Replies: 1
    Last Post: 04-19-2011, 06:58 PM
  4. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  5. Destroy object
    By anjanesh in forum New To Java
    Replies: 11
    Last Post: 01-02-2010, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •