Search code examples
javaswingjtableactionlistener

ListSelectionListener won't work propertly


Here is my code. It got problem on while you selete from left to right..

        import javax.swing.*;
        import javax.swing.event.*;
        public class swingex7 extends JFrame{
        swingex7(){
        JFrame f = new JFrame("Table Example");  
        String row[][]= {{"101","Hein Htet","10000000"},{"102","Hein Htet1","20000000"},{"103","Hein 
        Htet2","30000000"}};
        String column[]= {"Id","Name","Salary"};
        final JTable jt = new JTable(row,column);
        jt.setCellSelectionEnabled(true);
        ListSelectionModel lsm = jt.getSelectionModel();
        lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsm.addListSelectionListener(new ListSelectionListener() {      
            @Override
            public void valueChanged(ListSelectionEvent e) {    
                String data=null;
                int[] rows=jt.getSelectedRows();
                int[] columns = jt.getSelectedColumns();
                for(int i=0;i<rows.length;i++) {
                    for(int j=0;j<columns.length;j++) {
                        data = (String)jt.getValueAt(rows[i], columns[j]);
                    }
                }               
                System.out.println("Table element seleted is "+data);
            }   
         });

         JScrollPane js = new JScrollPane(jt);
         f.add(js);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.setSize(400, 400);
         f.setVisible(true);
     }
         public static void main (String[]args) {
         new swingex7();
     }
     }

It got problem on while you Selete from left to right. I also want to output only once per action.


Solution

  • There are 2 problems in the code.

    1. First problem is ListSelectionListener is called 2 times when mouse is clicked and when mouse is released. But instead of that if you can add MouseListener to your JTable as below.

    MouseListener tableMouseListener = new MouseAdapter() {
    
                      @Override
                      public void mouseReleased(MouseEvent e) {
                          String data = null;
                          int[] rows = jt.getSelectedRows();
                          int[] columns = jt.getSelectedColumns();
                          for (int i = 0; i < rows.length; i++) {
                                for (int j = 0; j < columns.length; j++) {
                                    data = (String) jt.getValueAt(rows[i], columns[j]);
                                    System.out.println("Table element selected is " + data);
                                }
                          }
                      }
                   };
                   jt.addMouseListener(tableMouseListener);
    

    2. Second issue is the place where you printed the data. It should be inside the for loop. Otherwise the data will be rewritten in each iteration in the loop and only last value will be printed.

    for (int i = 0; i < rows.length; i++) {
        for (int j = 0; j < columns.length; j++) {
                data = (String) jt.getValueAt(rows[i], columns[j]);
                System.out.println("Table element selected is " + data);
        }
    }