Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java get JPanel Components

I have a JPanel full of JTextFields...

for (int i=0; i<maxPoints; i++) {
    JTextField textField = new JTextField();
    points.add(textField);
}

How do I later get the JTextFields in that JPanel? Like if I want their values with

TextField.getText();

Thanks

like image 410
Devoted Avatar asked Dec 16 '08 02:12

Devoted


People also ask

How do I get components from a JPanel?

You should call the getComponents method this returns with an array of all elements on your JPanel. After you can iterate on the array and check if its equals with the sought after component.

What is JTextField in Java?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt. TextField where it is reasonable to do so.

Can a JPanel contain JFrame?

JFrame is the window; it can have one or more JPanel instances inside it. JPanel is not the window.

Why is JPanel used in swings?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.


2 Answers

Every JPanel in Java is also an AWT container. Thus, you should be able to use getComponents to get the array of contained components in the panel, iterate over them, check their types (To make sure you didn't get other controls), and do whatever you need with them.

However, this is generally poor design. If you know that you will need to access specific components, it is better to maintain an array of those text fields and pass it around, even though they are visually contained within the container.

If this is a recurrent task or could be done from multiple points, it may even make sense to have a special class representing a panel with text fields, that will then provide through its interface means of accessing these fields.

like image 172
Uri Avatar answered Sep 17 '22 12:09

Uri


Well bear in mind they didn't get there by them selves ( I think a read some questions about dynamically creating these panels at runtime )

In the answers posted there, someone said you should kept reference to those textfields in an array. That's exactly what you need here:

List<JTextField> list = new ArrayLists<JTextField>();

// your code...
for (int i=0; i<maxPoints; i++) { 
    JTextField textField = new JTextField();
    points.add(textField);
    list.add( textField ); // keep a reference to those fields.
}

// Later

for( JTextField f : list ) { 
   System.out.println( f.getText() ) ;
}

Wasn't that easy?

Just remember to keep these kinds of artifacts ( list ) as private as possible. They are for your control only, I don't think they belong to the interface.

Let's say you want to get the array of texts, instead of

 public List<JTextField> getFields();

You should consider:

 public List<String> getTexts(); // get them from the textfields ... 
like image 21
OscarRyz Avatar answered Sep 18 '22 12:09

OscarRyz