Results 1 to 10 of 10
  1. #1
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Components are visible when they are supposed to be invisible

    I have tried using the following code to make four text fields in a panel invisible:

    Java Code:
     for (Component c : PnlBeginGame.getComponents()) {
                if (c instanceof JTextField) {
                    setVisible (false); 
                } 
            }
    It doesn't work but no errors are displayed. Does anyone know how to solve this problem? Thank you.

    I can individually make the text fields invisible, but that would look less sophisticated.
    Last edited by Anne; 08-19-2015 at 03:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Components are visible when they are supposed to be invisible

    For what object is the setVisible() method called in the posted code? In other words, what class is that code in?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Components are visible when they are supposed to be invisible

    Under public Board_game() {:

    Java Code:
     public class Board_game extends javax.swing.JFrame {
      
        private final CardLayout cardlayout; //specify layout to use
        private final Container contentPane; //container for different "views"/screens of out program
        /**
         * Creates new form Board_game
         */
        public Board_game() {
            initComponents();
       
            for (Component c : PnlBeginGame.getComponents()) {
                if (c instanceof JTextField) {
                    setVisible (false); 
                } 
            }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Components are visible when they are supposed to be invisible

    What is the code in the for loop that has the call to setVisible() supposed to do?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Components are visible when they are supposed to be invisible

    The code gets all the components first, checks whether any are text fields, and if they are, it should make them invisible, but it's not making the text fields invisible. The setVisible(false) code is supposed to make the text fields invisible.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Components are visible when they are supposed to be invisible

    Here's what I see the code doing:
    It gets a list of the components in PnlBeginGame
    in a loop if any of the components in that container are JTextField objects, it calls the Board_game's setVisible() method.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: Components are visible when they are supposed to be invisible

    post the complete code..i dont think the textfields are coming inside the if condition.
    Ramya:cool:

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Components are visible when they are supposed to be invisible

    Try qualifying setVisible(false) with the Component, c.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: Components are visible when they are supposed to be invisible

    Sometimes I set foreground and background color the same so it is effectively invisible.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Components are visible when they are supposed to be invisible

    In words: for all Components c in a collection, if c is a JTextField, set c invisible. The last statement in your code snippet doesn't set c invisble; it sets something else invisible ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. How To Make JFrame Invisible And Contents Visible?
    By Kuttappu in forum Advanced Java
    Replies: 3
    Last Post: 01-06-2012, 06:52 AM
  2. How to make image invisible and visible in applet
    By KSBeyaz in forum Java Applets
    Replies: 15
    Last Post: 12-04-2011, 05:58 PM
  3. I don't understand what I'm supposed to do...
    By colerelm in forum New To Java
    Replies: 2
    Last Post: 10-04-2011, 03:01 AM
  4. Replies: 2
    Last Post: 04-14-2009, 10:01 PM
  5. Help interpreting what a class is supposed to do
    By bornwithnoname in forum New To Java
    Replies: 2
    Last Post: 11-20-2008, 12:19 AM

Posting Permissions

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