Basically, I’ve got a JList in my class that contains my main function, and I have a button that calls a JDialog. An Add button in this JDialog adds an object to a List that I’ve declared in the first class and also adds the name from the text area into a ListModel so that it will be displayed in the JList.
The problem is that for some reason, adding an object to a list from another class is making things all screwy with the list.
After I add one object, the size of the list is 1
After I add a second object, two more are added to my list
After I add a third object, three more are added to my list
So by the time I’ve added 4 names through the JDialog class, my list is now size 10. I’m really not sure how to go about fixing it. This is the code in question:
In my StartUpWindow class:
**static DrafterList listOfDrafters = new DrafterList();
static DefaultListModel listModel = new DefaultListModel();
public static class addDrafterActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
AddNameDialog addNameWindow = new AddNameDialog();
Component contents = addNameWindow.createComponents();
addNameWindow.getContentPane().add(contents);
addNameWindow.pack();
addNameWindow.setVisible(true);
partList.repaint();
}
}**
And in my class that extends JDialog:
**public static class addActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String newName = nameArea.getText(); ** *// A textArea*
*// Add a Drafter object to the DrafterList in StartUpWindow class*
**StartUpWindow.listOfDrafters.addDrafter(new Drafter(newName));**
*// Add an element to the ListModel for the JList*
**StartUpWindow.listModel.addElement(newName);**
**nameArea.setText("");
}
}**
Now, I suppose I could just hack it so that my program runs through the list and listModel and takes out any blank entries, but it would be nice to not have to do that. Any help would be greatly appreciated.