Reshad
Reshad

Reputation: 2652

Auto refresh jcombobox

Hello I have the following setup.

enter image description here

This are the 6 classes I have. Inside the BankApp I have an arraylist that stores all the person objects.

The BankAccView and the PersonView both have a JComboBox so I can choose a "Person" object for example to give him or her a bankAccount number.

The problem I am going through is that the JComboBox's don't autoreload. It looks as follows

  in my constructor I have this

personenList = new JComboBox();
personenList.addItemListener(new ItemChangeListener(this));

And in my ActionListener for example this (but I think its not good) So when Creating a new person the JComboBox should autorefresh everytime. ( this is whats not working )

personenList.addItem(persoon);

And I think this is also relevant. the itemchangelistener is like this.

    public class ItemChangeListener implements ItemListener {

        Persoon selectedPerson;
        RekeningApp app;
        PersoonView view;

        public ItemChangeListener(PersoonView view) {

            this.view = view;

        }

        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                Object item = event.getItem();
                // do something with object
                if(item instanceof Persoon) {
                    this.selectedPerson = (Persoon) item;
                    view.setOverzicht(this.selectedPerson);
                } else {
                    this.selectedPerson = null;
                }
            }
        }

    }

Upvotes: 0

Views: 854

Answers (1)

durron597
durron597

Reputation: 32323

You need to somehow maintain state after the window closes. This can be done in a number of ways:

  1. When the window is closed, don't actually close it, just hide it. See JFrame.setDefaultCloseOperation This is probably the easiest solution, but it's not robust, as it's not easy to access the stored people from the rest of your application.

  2. Store the data in an external data structure, and then pass the data into the view when it's recreated. This is probably the best solution, as it allows you to do other things with the data, like write it to a file or database for the next time you run the program.

  3. Create your own ComboBoxModel implementation that is backed by the external data structure from #2. This is probably the very best solution, as you don't have to manually load the data each time (it just takes your one array list etc.), but it may be overkill for your situation. I would recommend this for a production application but not for homework.

Here's some code you might use for 3:

public class PersonComboBoxModel extends AbstractListModel<Person> implements MutableComboBoxModel<Person> {
    private List<Person> comboData;

    public PersonComboBoxModel(List<Person> initialPeople) {
        comboData = new ArrayList<Person>(initialPeople);
    }

    // Implement the interface methods.
    // Make sure to call the fireXXXchanged methods from AbstractListModel at the appropriate times.
}

You might be able to use this for #2:

public PersonView(List<Person> initialPeople) {
    personenList = new JComboBox();
    for(Person p : initialPeople) {
        personenList.addItem(p);
    }
    personenList.addItemListener(new ItemChangeListener(this));
    // etc.
}

Upvotes: 2

Related Questions