Results 1 to 7 of 7
  1. #1
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Clearing up a GUI

    I've created a code that generates a GUI and adds some pictures to it when hitting a button. These pictures are included as JLabels.

    Now I want all the pictures to be removed from the GUI when hitting another. But how can I achieve this?

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

    Default Re: Clearing up a GUI

    Probably easiest is to remove the image-holding ImageIcons from the JLabels. This can be done by calling setIcon(null) on the JLabels.

  3. #3
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: Clearing up a GUI

    How do you mean that? If i just replaced the directory with null, it wouldn't be done by clicking on a button.

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

    Default Re: Clearing up a GUI

    Quote Originally Posted by Alpa View Post
    How do you mean that? If i just replaced the directory with null, it wouldn't be done by clicking on a button.
    You are using terms that I don't understand, and I fear that you assume that I know more about the structure of your program than I do (which is very little indeed).

    What I'm suggesting is that if you want to clear an image out of a GUI that is held by a JLabel, you simply call setIcon(null) on that JLabel, that's it.

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Clearing up a GUI

    Another way is to place JPanels in the GUI witch you probably already have done... and then use the .setVisible(true/false) to show and hide the images. The advantage of this is that you have control over the placement? But sense Fubarable is better then me in JAVA i would take his advice.

  6. #6
    Alpa is offline Member
    Join Date
    Dec 2011
    Posts
    17
    Rep Power
    0

    Default Re: Clearing up a GUI

    Quote Originally Posted by santa View Post
    Another way is to place JPanels in the GUI witch you probably already have done... and then use the .setVisible(true/false) to show and hide the images.
    But if I did that, would I be able to place other images on positions where the former images were placed (before they were hid by setting .setVisible(false))?

    Quote Originally Posted by Fubarable View Post
    You are using terms that I don't understand, and I fear that you assume that I know more about the structure of your program than I do (which is very little indeed).

    What I'm suggesting is that if you want to clear an image out of a GUI that is held by a JLabel, you simply call setIcon(null) on that JLabel, that's it.
    So you mean by writing labelx.setIcon(null); into the actionPerformed section of my button's code? And then I could write some code to find out which picture was shown on my GUI and then remove it using labelx.setIcon(null);?

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Clearing up a GUI

    Quote Originally Posted by Alpa View Post
    But if I did that, would I be able to place other images on positions where the former images were placed (before they were hid by setting .setVisible(false))?
    The things is that me and fubarable are talking about are the same, first you place the JPanel/s and set your layout then you add the imageicons to the panels, and when you want to clear an icon you can call JLable.setIcon(null) on it.

    If you are that worried about positions and stuff you can also use the graphics object to draw them on the JFrame/JPanel then they would appear on the exact pixels you'd like But if the pictures are going to appear in static positions then I'd use Panels and imageicons

Similar Threads

  1. Clearing Documents in TextPanes
    By wired-in=p in forum AWT / Swing
    Replies: 1
    Last Post: 01-31-2012, 08:59 AM
  2. Replies: 4
    Last Post: 01-06-2012, 04:23 PM
  3. screen not clearing during resize
    By cl2606 in forum Java Applets
    Replies: 5
    Last Post: 11-23-2011, 11:55 AM
  4. emptying/clearing an array
    By i8java in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 01:39 PM
  5. Help Clearing BufferedImage
    By ketann in forum Java 2D
    Replies: 19
    Last Post: 03-29-2010, 07:45 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
  •