Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-19-2009, 01:12 AM
VeasMKII's Avatar
Member
 
Join Date: Jan 2009
Posts: 18
Rep Power: 0
VeasMKII is on a distinguished road
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:

Code:
JPanel.add(new TextField());
But then how do i access the TextField inside it?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-19-2009, 01:22 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,561
Rep Power: 4
hardwired is on a distinguished road
Default
Save a reference to them that you can use later to refer to them.
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.
Code:
Component[] c = panel.getComponents();
for(int i = 0; i < c.length; i++) {
    System.out.println(c[i].getClass().getSimpleName());
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-19-2009, 01:26 AM
VeasMKII's Avatar
Member
 
Join Date: Jan 2009
Posts: 18
Rep Power: 0
VeasMKII is on a distinguished road
Default
Thank you very much!
This looks like the perfect answer, i'll play with it now
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-19-2009, 01:55 AM
VeasMKII's Avatar
Member
 
Join Date: Jan 2009
Posts: 18
Rep Power: 0
VeasMKII is on a distinguished road
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 02:01 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-19-2009, 02:00 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,561
Rep Power: 4
hardwired is on a distinguished road
Default
In your event code or even in your buildUI method
Code:
textField.setText("hello world");
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-19-2009, 02:29 AM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
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.
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to place components on JPanel deshprateek AWT / Swing 1 02-22-2009 06:34 AM
help?! paint on top of swing components on JPanel beam2008 AWT / Swing 1 12-05-2008 05:15 AM
GUI components not display Eranga AWT / Swing 2 03-12-2008 03:16 AM
Removing components from JPanel Echilon New To Java 0 12-30-2007 05:05 PM
Gui Components Marty New To Java 1 05-28-2007 05:04 AM


All times are GMT +2. The time now is 04:36 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org