views:

24

answers:

2

I have a JFrame with a bunch of different JTextField and so on. How to collect data entered by user, without setting actions for each of this components?

Note: I create this JTextField using "inline" code, like this:

    layout.row().grid(new JLabel("Density")).add(new JTextField("1"))
            .spanRow();
    layout.row().grid(new JLabel("Minimal size"))
            .add(new JTextField("1")).spanRow();
+1  A: 

You would probably need to iterate through the child elements of each container, check if it is an instance of JTextField and then read the contents using the getText method.

Gian
Ok, that would be fine, but how to let my iterating code know, what editor it deals with?
Shaman
I have no idea what you are asking, I'm sorry. Are you asking how to uniquely identify the component you are reading from?
Gian
Yes, If I iterate through the child elements, as you answered, then how to identify where to put values read?
Shaman
You could use the hash code for the text field, or find the corresponding label text?
Gian
+1  A: 

The Swing Utils class has some convenience methods that makes this easy:

List<JTextField> components = SwingUtils.getDescendantsOfType(JTextField.class, container, true);

for (JTextField component: components)
{
    // add custom code here
}
camickr