Adding to existing JList
- by Máca Danilov
I need some help about adding items to JList. I work on some "library" kind of project. And I need to add readers to already existing JList. But when I try to add it, JList just resets, removes all the readers and starts adding readers to a new blank JList. But I don't need it to make new list but add it to the already existing one.
I know it's something about creating new model after adding, but i don't know where to fix it.
panelHorni = new JPanel();
listModel = new DefaultListModel();
listCtenaru = new JList(listModel);
FileInputStream fis = new FileInputStream("myjlist.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
listCtenaru = (JList)ois.readObject();
listScroll = new JScrollPane();
listScroll.add(listCtenaru);
listCtenaru.setPreferredSize(new Dimension(350, 417));
listCtenaru.setBackground(new Color(238,238,238));
panelHorni.add(listCtenaru);
listener
public void actionPerformed(ActionEvent e) {
String jmeno = pole1.getText();
String prijmeni = pole2.getText();
listModel.addElement(jmeno +" "+ prijmeni);
listCtenaru.setModel(listModel);
pole1.setText("");
pole2.setText("");
pole1.requestFocus();