ListSelectionListener 将无法正常工作

这是我的代码。从左到右选择时出现问题。


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

     }

     }

当您从左向右选择时出现问题。我还想每个动作只输出一次。


慕婉清6462132
浏览 74回答 1
1回答

九州编程

代码中有2个问题。1. 第一个问题是当鼠标单击和释放鼠标时,ListSelectionListener 被调用两次。但如果您可以将 MouseListener 添加到您的 JTable 中,如下所示。MouseListener tableMouseListener = new MouseAdapter() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void mouseReleased(MouseEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String data = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] rows = jt.getSelectedRows();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] columns = jt.getSelectedColumns();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < rows.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < columns.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = (String) jt.getValueAt(rows[i], columns[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Table element selected is " + data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;jt.addMouseListener(tableMouseListener);2. 第二个问题是你打印数据的地方。它应该在 for 循环内。否则,数据将在循环中的每次迭代中被重写,并且仅打印最后一个值。for (int i = 0; i < rows.length; i++) {&nbsp; &nbsp; for (int j = 0; j < columns.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = (String) jt.getValueAt(rows[i], columns[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Table element selected is " + data);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java