On the form I have two elements jList and ComboBox that receive information from SQLite database. When I start the program, I get an empty jList , although in the ComboBox value of the first table from the jList selected, but when switching between tables, the program supplements jList elements from the selected table. And you need the program to change the contents of jList in accordance with the table selected from jComboBox .

  public Connection c=null; public DefaultComboBoxModel dcb = new DefaultComboBoxModel(); public DefaultListModel dlm = new DefaultListModel(); public void LoadList(){ // Запись из бд SQLite в Combobox try{ String qry = "Select name from sqlite_master where type = 'table' ;"; PreparedStatement pst = c.prepareStatement(qry); ResultSet rs = pst.executeQuery(); while (rs.next()){ dcb.addElement(rs.getString("name")) ; } jComboBox1.setModel(dcb); } catch(Exception e){ System.out.println(e); } // Выбор элемента из Combobox jComboBox1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ie) { String sa = (String)jComboBox1.getSelectedItem(); //Запись в jList try{ String qry = "Select * from '"+sa+"' ;"; PreparedStatement pst = c.prepareStatement(qry); ResultSet rs = pst.executeQuery(); while(rs.next()){ dlm.addElement(rs.getString("word")); } jList1.setModel(dlm); pst.close(); rs.close(); }catch(Exception e){ System.out.println(e); } }); } 

    1 answer 1

    So just transfer the DefaultListModel dlm = new DefaultListModel() before calling while(rs.next()) in jComboBox1.addActionListener . Those. create it before filling and pushing in jList1

    Or call dlm.clear() before calling while(rs.next()) in jComboBox1.addActionListener

    • Thank you very much, helped. But until the end the problem did not solve. There is a problem with reading from the ComboBox when launching the application, i.e. I run the program and get an empty jList until I select a table from the list, only then jList is filled. How to make it so that it fills up immediately at startup. P.S. When launched in ComboBox, the name of the first table is present - Interesant
    • @Interesant put all that in the ActionListener component into a separate function. And, respectively, call it in the ActionListener component and after the call to jComboBox1.setModel(dcb) - Chubatiy
    • Good idea, but if I’m asked to delete absolutely all the tables and create new ones, then I’m han. But anyway, thanks for the good idea! - Interesant