Reputation: 35
I have the following objects accessible (plus many others which don't seem to be relevant to the question at this time).
Objects:
listModel = new DefaultListModel();
inputField = new JTextField(10);
addButton = new JButton("+");
usernameList = new JList(listModel)
The ActionListener for the 'addButton':
public void actionPerformed(ActionEvent e) {
System.out.println("addButton clicked!");
Variables.username = inputField.getText();
System.out.println("Username now: " + Variables.username);
listModel.addElement(Variables.username);
inputField.setText(null);
}
At the moment, the 'addButton' grabs the input from the JTextField (inputField) and adds it to the listModel, and updates the JList (usernameList) with the new string. What I need to do now is take each element and either add each element to a different element within a pre-declared string array. How would I accomplish grabbing all of the elements passed to the JList?
Upvotes: 0
Views: 38
Reputation: 24
ListModel model = usernameList.getModel();
for(int i = 0; i < model.getSize(); i++) {
//do something
}
Upvotes: 1