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
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
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.
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
You can use the MouseClicked
event:
private void tableMouseClicked(java.awt.event.MouseEvent evt) {
// Do something.
}
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.