Results 1 to 6 of 6
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Access Child Components of JFrame?

    Hello,

    I want to make a method that accesses a component that lies on a JFrame which is a class variable.

    I don't know how to access that component within my JFrame.

    Java Code:
    public class Example{
    
    
       JFrame mainFrame;
    
    
    
       public void initialize(){
    
           JTextField myText = new JTextField();
    
           mainFrame.getContentPane().add(myText);
    
    
           setToInvisible();
    
    
       }
    
    
       public void setToInvisible(){
    
        //I want to call myFrame.myText.setVisible(false).
    
       }
    
    }

    So, as the code above shows, I am trying to access a JTextField that lies in the content pane of my JFrame, which is a class variable.


    I can't really do myFrame.myText.setVisible(false) nor can I do myFrame.getContentPane().myText.setVisible(false) as far as I can tell.

    How do I access this component directly??


    Also... if I will have to do something like myFrame.getComponents()[index] it won't be the best solution because I have no clue what the index of myText is (I have many components in my JFrame, which I haven't shown).

    Plz help

    Prince

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Can't you just make myText a class variable, like you do with mainFrame?
    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
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Or, in your example, you could pass myText into the setToInvisible() method as a parameter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    I'm guessing there is no straightforward and simple way to accomplish that.

    I guess I will make my JTextField a class variable then.


    Thanks.

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

    Default

    Quote Originally Posted by PrinceSendai View Post
    I'm guessing there is no straightforward and simple way to accomplish that.

    I guess I will make my JTextField a class variable then.
    .
    It can be done but by making it a class variable you would simplify things greatly. What is your reason for avoiding doing this in the first place?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Quote Originally Posted by PrinceSendai View Post
    I'm guessing there is no straightforward and simple way to accomplish that.
    I guess I will make my JTextField a class variable then.
    Thanks.
    How is that not simple and straightforward? I would say doing anything else is needlessly complicating things.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Get more components from JFrame Form.
    By ZeCute in forum AWT / Swing
    Replies: 0
    Last Post: 05-09-2011, 11:46 AM
  2. Access Parent and child node
    By emyk in forum Advanced Java
    Replies: 0
    Last Post: 05-03-2011, 12:16 AM
  3. Cannot add or update a child row....
    By Pierced1 in forum JDBC
    Replies: 2
    Last Post: 06-29-2009, 03:32 AM
  4. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 03:24 AM
  5. Child Labor
    By freddieMaize in forum Forum Lobby
    Replies: 1
    Last Post: 11-16-2008, 09:59 PM

Tags for this Thread

Posting Permissions

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