-3

I would like to insert data into sql from JList. However, the getModel() at ListModel is stating that "cannot find symbol"

This is the action when the next button from previous form is clicked:

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:


    DefaultListModel list = new DefaultListModel();
    System.out.println(""+songsdetails.get(0));
    for(int i=0; i <songsdetails.size();i++){
        list.addElement(songsdetails.get(i));
    }
    jList1.setModel(list);


}                 



        }

This is the code for current form:

private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {
DefaultListModel list = new DefaultListModel();

        try {
            Class.forName("com.mysql.jdbc.Driver");

            cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sdmusic","root","");

            st=cn.prepareStatement("INSERT `Username`, `SongsSelection` FROM `user` WHERE `Username`=?  'SongsSelection' = ?");


            ListModel model = list.getModel();
            for(int i = 0; i < model.getSize(); i++) {
            System.out.println(model.getElementAt(i));
            }
Ain Hasnor
  • 1
  • 1
  • 7
  • What is the stackTrace of the exception? – Yassin Hajaj Dec 07 '15 at 14:42
  • @YassinHajaj auto creation from the Netbeans – Ain Hasnor Dec 07 '15 at 14:42
  • 1
    @YassinHajaj Compiler errors don't have a stacktrace. – Tom Dec 07 '15 at 14:43
  • 1
    Please read the [JavaDoc](https://docs.oracle.com/javase/8/docs/api/javax/swing/DefaultListModel.html) to know which methods are available. `getModel` is non of them. – Tom Dec 07 '15 at 14:44
  • @Tom Indeed but if he had tried to execute the code, he would have had the answer immediately within the stackTrace `The method getModel() is undefined for the type DefaultListModel` – Yassin Hajaj Dec 07 '15 at 14:47
  • 1
    @YassinHajaj His compiler is telling him that already with "cannot find symbol" :P. – Tom Dec 07 '15 at 14:47
  • @Tom Which does not seem clear to me but whatever :D You're right – Yassin Hajaj Dec 07 '15 at 14:48
  • Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Raedwald Feb 26 '16 at 20:19

1 Answers1

0

DefaultListModel list = new DefaultListModel();

list is already set to DefaultListModel. You can't get ListModel of a ListModel.

What you might be looking for is:

DefaultListModel list = (DefaultListModel) jList1.getModel();
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Jaskaranbir Singh
  • 2,034
  • 3
  • 17
  • 33