JTable - Selected Row click event

前端 未结 5 936
名媛妹妹
名媛妹妹 2021-02-02 09:36

I have a Jtable that is populated with a linkedlist through an AbstractTableModel.

What I want to do is when I click (left-mouse click) on a row in the JTable, the linke

相关标签:
5条回答
  • 2021-02-02 09:47
     private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
         JTable source = (JTable)evt.getSource();
                int row = source.rowAtPoint( evt.getPoint() );
                int column = source.columnAtPoint( evt.getPoint() );
                String s=source.getModel().getValueAt(row, column)+"";
    
                JOptionPane.showMessageDialog(null, s);
    
    
    } 
    

    if you want click cell or row in jtable use this way

    0 讨论(0)
  • 2021-02-02 09:51

    To learn what row was selected, add a ListSelectionListener, as shown in How to Use Tables in the example SimpleTableSelectionDemo. A JList can be constructed directly from the linked list's toArray() method, and you can add a suitable listener to it for details.

    0 讨论(0)
  • 2021-02-02 09:58

    I would recommend using Glazed Lists for this. It makes it very easy to map a data structure to a table model.

    To react to the mouseclick on the JTable, use an ActionListener: ActionListener on JLabel or JTable cell

    0 讨论(0)
  • 2021-02-02 10:00

    You can use the MouseClicked event:

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {
     // Do something.
    }
    
    0 讨论(0)
  • 2021-02-02 10:09

    Here's how I did it:

    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent event) {
                // do some actions here, for example
                // print first column value from selected row
                System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
            }
        });
    

    This code reacts on mouse click and item selection from keyboard.

    0 讨论(0)
提交回复
热议问题