• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting JComponents

 
Nick Lebian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
How can I get the JComponentes of a GUI? With AWT it's easy, I just have to type
Component comp=this.getComponent(1);
But how does this work with swing? There isnt't a method like
getJComponents().
Bye,
Nick
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JComponent extends Container which extends Component. getComponents() still does what you want in Swing.
 
Nick Lebian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. But now I have the next problem. I can't read out the array of getComponents() in an other method:
...
public Text(List list, int gaps)
{
this.gaps=gaps;
setLayout(new FlowLayout());
it=list.iterator();
while (it.hasNext())
{
add(new JLabel((String)it.next()));
if (it.hasNext())
add(new JTextField("",7));
}
}
...
public List readout()
{
Component comp[]=this.getComponents();
for(int i=1; i<2*gaps; i=i+2)
{
JTextField jtext=(JTextField)comp[i];
result.add(jtext.getText());
}
return result;
}
This doesn't work, the List result is empty. How can I get access to the JTextField Components and their content, in an other method where they were created?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you arn't doing something right... I can't tell what the 'gaps' variable is supposed to do in your component, but you should be looping through the entire array you get back from 'getComponents()' not '2*gaps'. Here is a short example that shows that this works :

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic