Results 1 to 6 of 6
  1. #1
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default [SOLVED] accessing JPanel.add components

    I have a JPanel and i want to access the components i've added to it, but i have no idea how to do this.

    For example:

    Java Code:
    JPanel.add(new TextField());
    But then how do i access the TextField inside it?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Save a reference to them that you can use later to refer to them.
    Java Code:
    class Pseudo {
        JTextField textField;
        JButton button = new JButton("hi");
    
        private void buildUI() {
            testField = new JTextField(12);
            JPanel panel = new JPanel(someLayout);
            panel.add(textField);
            panel.add(button);
        }
    }
    Or you can use the Container methods like getComponents to access them.
    Java Code:
    Component[] c = panel.getComponents();
    for(int i = 0; i < c.length; i++) {
        System.out.println(c[i].getClass().getSimpleName());
    }

  3. #3
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Thank you very much!
    This looks like the perfect answer, i'll play with it now :)

  4. #4
    VeasMKII's Avatar
    VeasMKII is offline Member
    Join Date
    Jan 2009
    Posts
    18
    Rep Power
    0

    Default

    Hmm, this is somewhat useful. But how would i go about setting the text of the textfield (after creation) for example?

    *edit*
    I see now, i simply access the textField and the information on it is changed.
    I was confused because i had created an array of panels containing the components, but i think i'll need an array of text fields instead. (or aswell)
    Last edited by VeasMKII; 06-19-2009 at 01:01 AM.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    In your event code or even in your buildUI method
    Java Code:
    textField.setText("hello world");

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    hmm... I would think ((TextField)myJPanel.getComponent(textFieldIndex)) .setText("new text"); would work as well, butI'm not sure...

    Also, try not to mix AWT components (extend Component, class names are just xxx) with Swing components (extend JComponent, class names are Jxxx). Names may not all follow the basic xxx or Jxxx pattern, but most do.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. How to place components on JPanel
    By deshprateek in forum AWT / Swing
    Replies: 1
    Last Post: 02-22-2009, 05:34 AM
  2. help?! paint on top of swing components on JPanel
    By beam2008 in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2008, 04:15 AM
  3. GUI components not display
    By Eranga in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2008, 02:16 AM
  4. Removing components from JPanel
    By Echilon in forum New To Java
    Replies: 0
    Last Post: 12-30-2007, 04:05 PM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 04:04 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
  •