1

I have an ArrayList which holds employee record details from data inputted into JTextFields. I have displayed this with a showMessageDialog box to confirm that they are being added to the list correctly.

The employees are created and added to the list correctly but how do I cycle through the ArrayList one record at a time and have the information displayed in the JTextFields?

Edited as below:

I don't think I have asked the question properly. I have a button that enables me to show the contents of the first element of my array list to the GUI. See below:

nField.setText(nameList.get(0).getName()); bField.setText(nameList.get(0).getBirth()); jField.setText(nameList.get(0).getID());

What I need is something to show the next element.

I have a list iterator that I am trying to use but I simply can’t get it to work.

I have tried this but it results in the last element being shown:

for (int i = 0; i < nameList.size(); i++) {

nameList.get(i);

nField.setText(nameList.get(i).getName()); bField.setText(nameList.get(i).getBirth()); jfield.setText(nameList.get(i).getID()); }

4

2 Answers 2

2

There are different options, but the short of it is, you'll have to tie your employee record to the JTextField somehow. One way to do this is by creating a simple object to hold the relation:

public class EmployeeRecordToJTextfield {
  private EmployeeRecord employeeRecord;
  private JTextField jTextField;

  public EmployeeRecordToJTextfield(EmployeeRecord employeeRecord, JTextField jTextField) {
    this.employeeRecord = employeeRecord;
    this.jTextField = jTextField;
  }

  public EmployeeRecord getEmployeeRecord() {
    return employeeRecord;
  }

  public JTextField getJTextField() {
    return jTextField;
  }
}

Instead of the EmployeeRecord, you stick instances of this class in the list:

List<EmployeeRecordToJTextfield> list = new ArrayList<>();
list.add(new EmployeeRecordToJTextfield(employeeRecord, jTextField));

When you iterate over this list, you can transfer the data you choose:

for (EmployeeRecordToJTextfield e : list) {
  JTextField textField = e.getJTextField();
  EmployeeRecord record = e.getEmployeeRecord();
  textField.setText(record.getName());
}

Another option, maybe simpler, is by converting your list to a map and map EmployeeRecords to their JTextField.

Update based on the Question update

I hope I now understand what you're asking, but if so, I'd just do this:

Store the index of the 'current' shown item in a variable or hidden GUI field.

private int currentIndex = 0; // Start with the first item.

When you want to show the next item (perhaps through a Next or Previous button), get the next value. Compensate for list length:

int nextIndex = currentIndex+1;
if (nextIndex >= nameList.size()) nextIndex = 0;
if (nextIndex < 0) nextIndex = nameList.size()-1;

Now you can use nextIndex to fetch the correct entry from your list:

nField.setText(nameList.get(nextIndex).getName());
bField.setText(nameList.get(nextIndex).getBirth()); 
jfield.setText(nameList.get(nextIndex).getID());

And don't forget to update the currentIndex:

this.currentIndex = nextIndex;
Sign up to request clarification or add additional context in comments.

5 Comments

I very much appreciate your reply but I think I over complicated the question. I have edited my question and included source code.
@JavaNewbie I updated the answer, is this what you're looking for?
Perfect, thank you Ricardo. This has really helped. It even goes back to the first record after the final record. I have used the same code for the previous button but used currentIndex-1. This works until you get to the first record. It then breaks down when you try to go to the previous record. What am I missing?
@JavaNewbie You'd have to add a line to cover for when you're on the first element: if (nextIndex < 0) nextIndex = nameList.size()-1;
Can't thank you enough. Been struggling with this for a while and can finally move on to other aspects. You are a credit to the site.
0

Use for loop:

for(JTextField a : your_list){
    do_something();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.