Adding to existing JList

Posted by Máca Danilov on Stack Overflow See other posts from Stack Overflow or by Máca Danilov
Published on 2013-05-23T17:03:44Z Indexed on 2014/06/05 9:26 UTC
Read the original article Hit count: 170

Filed under:
|
|
|

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();

© Stack Overflow or respective owner

Related posts about java

Related posts about swing