简体   繁体   中英

Best way to grab and print data from a large amount of text fields?

I'm trying to figure out the best way to read data from around 60 text fields and then print it in a spaced line.

For example

System.out.println(field1.getText() + " " + field2.getText());

The thing is, I don't want to have a crap load of getText() method.

So my main question is, is there an easier and perhaps better (performance-wise) way to do this?

Image: my text fields

You could use StringBuilder instead of string concatenation for better performance:

StringBuilder s = new StringBuilder();
s.append(field1.getText()).append(" ")
 .append(field2.getText()).append(" ")
 .append(field3.getText());
System.out.println(s.toString());

And if your fields were stored in a collection you could iterate over that collection instead of calling each individual field:

List<JTextField> fields = new ArrayList<JTextField> ();
for(int i = 0; i < 30; i++) {
    fields.add(new JTextField(Integer.toString(i)));
}
//...
StringBuilder s = new StringBuilder();
for (JTextField t : fields) {
    s.append(t.getText()).append(" ");
}
//...

I couldn't think of any other performance wise better method.

Firstly,

  • Use TextField array or list to hold all your text fields. Then you could iterate them using a for loop.

Secondly,

  • Use StringBuilder whereever possible.
  1. Add your fields to an array or collection, then iterate over them in a for-each loop, printing out the output.

  2. Use reflection over the class, to identify the fields by some naming convention, like the regular expression textField.+

  3. Write your own annotation, make it retainable in runtime, and annotate every field you want to visit, then use reflection to iterate over class fields, checking for the annotation, and printing the value of the text field if it has the annotation attached.

With good naming convention/pattern, 2. is best. 3. is a bit heavyweight, but very precise and flexible. 1. is a quick and easy hack too.

You could try using reflection. Performance-wise it will be slower though:

public class Snippet {

    JTextField field1 = new JTextField("1");
    JTextField field2 = new JTextField("2");

    JTextField field3 = new JTextField("3");
    JTextField field4 = new JTextField("4");

    // add more fields here

    public static void main(String[] args) throws Exception {
        new Snippet().run();
    }

    private void run() throws Exception {
        for (int i = 1; i <= 4; i += 2) {
            JTextField textfieldA = getTextField(i);
            JTextField textfieldB = getTextField(i + 1);
            System.out.println(textfieldA.getText() + " " + textfieldB.getText());
        }
    }

    private JTextField getTextField(int i) throws NoSuchFieldException, IllegalAccessException {
        Field field = Snippet.class.getDeclaredField("field" + i);
        JTextField textfield = (JTextField) field.get(this);
        return textfield;
    }
}

This is quick and dirty and in the long run you will be better off putting your textfields into an appropriate data structure (eg List or Map) and simply iterate over it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM