Remove (Clear) and remove Selected element

 


///===========Clear=================

        jList1.setListData(new String[]{});


//==============Remove Selected Element =========

      // TODO add your handling code here:

     int count=0;

        DefaultListModel listModel1 = new DefaultListModel();

        ListModel<String> tempL=  jList1.getModel();

  

        

        //Create New Array and add element 

        for(int i=0;i<tempL.getSize();i++){

            listModel1.add(i, tempL.getElementAt(i));

        }

        

        //===Delete From Array

        for(int index : jList1.getSelectedIndices()){

      

          /// System.out.println(index);

          //((DefaultListModel) jList1.getModel()).remove(index);

          listModel1.remove(index-count);

          count++;

 }

        

        


       // jList1.setListData(tempL);

       

       jList1.setModel(listModel1);

       

Comments

Popular posts from this blog

JSpinner

🛢️ MySQL Connection & CRUD Operations in Java