Results 1 to 15 of 15
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default What does setVisible() actually do?

    When I use
    Java Code:
    this.setVisible(true);
    this.setVisible(false);
    (exactly like that, on then off) it works fine.

    When I don't include setVisible() until later in my program, it gives an error "Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0"

    What is setVisible() actually doing besides making it visible?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: What does setVisible() actually do?

    When I don't include setVisible() until later in my program, it gives an error "Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0"
    Posting an SSCCE does wonders to provide context and leave guesswork out of the equation

    What is setVisible() actually doing besides making it visible?
    Why not look at the source code (or the API docs)? I can only guess as to the method you are referring to, my guess being it JFrame or an inherited method
    javax.swing: JFrame.java

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    I don't know what SSCCE is. This is not a problem for my program, I'm just curious as to what it does besides making it visible. All I ever see is that it does only that. That is not true, but I don't know what else it does.

  4. #4
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    Quote Originally Posted by Prime624 View Post
    I don't know what SSCCE is. This is not a problem for my program, I'm just curious as to what it does besides making it visible. All I ever see is that it does only that. That is not true, but I don't know what else it does.
    Check here for a better understanding. JComponent (Java Platform SE 7 ) Perhaps you think it does more because in many instances the object being setVisible is a container, or has other parameters that come into play. Actually, setVisible(true) only does that one thing... makes something visible.

    Maybe the greatest Java resource available ---> Java Platform SE 7
    Last edited by NotTooOld; 04-12-2013 at 08:00 AM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: What does setVisible() actually do?

    That method does only what is says: make a component (in)visible; if the component were part of a LayoutManager, maybe some other components are resized and/or moved; as a programmer you shouldn't be concerned what the system does behind the scenes as long as the component is made (in)visible.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    I'd prefer to just keep it invisible until it is ready, but I am forced to set it visible then invisible before I do stuff to it. There has to be something else it does.

  7. #7
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    Quote Originally Posted by Prime624 View Post
    I'd prefer to just keep it invisible until it is ready, but I am forced to set it visible then invisible before I do stuff to it. There has to be something else it does.
    If you don't want to believe me, or JosAH (a moderator), or the Java Platform documentation, I don't know how to help you. It really does only do one thing. It sets a single property of the object. Now it is a different thing after you explain a bit more. You are trying to do something with this object, and then, only after you've done whatever, you want to make it visible. No problem. You can instanciate the object with it's visible property set to false, then do your thing, then set it visible, but you must instanciate the object first. Or you could make a new class I suppose that extends the object we're talking about, and make the changes to your new object class. This way, the changes are already there when you instanciate it. But I really don't know which would be better for you since you are not giving a great deal of detail.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: What does setVisible() actually do?

    Setting your frame visible establishes your frame insets to be non-zero. Probably because the frame needs to be decorated before it is displayed (that is a guess). Normally I do this by using frame.pack(). Once the insets are initialized to their default values, the frames width and height become non-zero (assuming they were both zero before). This is not documented in the API as far as I know. It also makes the frame visible.

    Edit: Upon further review. JFrame is an indirect sublcass of java.awt.Component which calls the Components setVisible() method. According to the API:

    "This method changes layout-related information, and therefore, invalidates the component hierarchy."

    This may cause the observed changes mentioned above. But I really don't know for certain.

    Regards,
    Jim
    Last edited by jim829; 04-13-2013 at 06:01 AM. Reason: updated comment
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    Quote Originally Posted by jim829 View Post
    Setting your frame visible establishes your frame insets to be non-zero. Probably because the frame needs to be decorated before it is displayed (that is a guess). Normally I do this by using frame.pack(). Once the insets are initialized to their default values, the frames width and height become non-zero (assuming they were both zero before). This is not documented in the API as far as I know. It also makes the frame visible.

    Edit: Upon further review. JFrame is an indirect sublcass of java.awt.Component which calls the Components setVisible() method. According to the API:

    "This method changes layout-related information, and therefore, invalidates the component hierarchy."

    This may cause the observed changes mentioned above. But I really don't know for certain.

    Regards,
    Jim
    Thanks Jim. I was not sure exactly HOW it made a component visible/invisible. Good to know. It seems it has effects similar to remove() or invalidate() in that the LayoutManager is notified and adjusts the hierarchy.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: What does setVisible() actually do?

    And I am not saying that this is "supposed" to be caused by setVisible(). Just that it apparently is a side effect.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    Quote Originally Posted by jim829 View Post
    Setting your frame visible establishes your frame insets to be non-zero. Probably because the frame needs to be decorated before it is displayed (that is a guess). Normally I do this by using frame.pack(). Once the insets are initialized to their default values, the frames width and height become non-zero (assuming they were both zero before). This is not documented in the API as far as I know. It also makes the frame visible.

    Edit: Upon further review. JFrame is an indirect sublcass of java.awt.Component which calls the Components setVisible() method. According to the API:

    "This method changes layout-related information, and therefore, invalidates the component hierarchy."

    This may cause the observed changes mentioned above. But I really don't know for certain.

    Regards,
    Jim
    THANK YOU!!! Now just to find a way to get the same effect without setting visible. Probably won't get to looking for one until I finish though. Not that important.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: What does setVisible() actually do?

    Try frame.pack(). However, once you add your components you will need to do it again. Also, apparently your frame was not given an initial size so you might try using setPreferredSize(new Dimension(width, height)).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    Doesn't work. I originally set the size as the screen width and height, I set it using w.setExtendedState(w.MAXIMIZED_BOTH); (w is my frame). I'm not sure if the second counts as setting the size.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: What does setVisible() actually do?

    I have never used setExtendedState(). Did you do frame.pack() right afterwards? (I am guessing now). Also, another way to get the screen size is:

    Java Code:
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension size = tk.getScreenSize();
    Otherwise, I am out of ideas.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: What does setVisible() actually do?

    I did use the toolkit thing, but using the extended thing make it full screen. I probably made it a lot more complicated than it needs to be, but oh well. It works so I'm happy.

Similar Threads

  1. SetVisible causing problem
    By eric_bloodaxe in forum NetBeans
    Replies: 2
    Last Post: 03-11-2012, 07:06 PM
  2. setVisible does not work
    By michalss in forum AWT / Swing
    Replies: 5
    Last Post: 04-10-2011, 10:35 PM
  3. .setVisible(false); not working... grr...
    By Roclemir in forum New To Java
    Replies: 8
    Last Post: 05-29-2010, 12:22 AM
  4. can't get setVisible(false) to work
    By alexander.s in forum AWT / Swing
    Replies: 0
    Last Post: 04-27-2010, 08:33 PM
  5. setVisible(true) does not work
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2007, 07:32 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
  •