user1633277
user1633277

Reputation: 510

Creating a number of JTextField s that is defined by user input in way to to read text from all of them in a push of a button

I used to make my UI by making declaring new JTextField in a for cycle while attaching an action listener by anon class to each JTextField, which means that you have to press enter fire an event which will read the text of the field and put it into an array here is the code Getting data from JTextField that is one of several local variables without a few minor changes . Now I have to modify it to so that i press a button like Apply in order to have values written into an array. While I found two ways to do this wonder what is the optimal way to do this .

  1. The horrible way . Create an array that acts as temporary storage , replace ActionListeners with DocumentListeners that will place the values into this temp array . And a button that will on press iterate through the the temp array placing its values into the target array.

  2. A better way which I found while searching , create a JTextField array as public and simply have a button that will on press iterate through the JTextField array and place its values into the target array.

Upvotes: 1

Views: 217

Answers (1)

trashgod
trashgod

Reputation: 205785

Adder is an example that maintains a List<JFormattedTextField> to enforce formatting. It uses a PropertyChangeListener & FocusListener to update on navigation events, such as the default key binding to Tab and Shift-Tab.

image

Upvotes: 4

Related Questions