Are there any methods that are used to get the data of the selected row? I just want to simply click a specific row with data on it and click a button that will print the data in the Console.
So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.
We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.
To add the data entered in the JTextFields you will need to register an ActionListener to your add button, in this case jButton1 . To add entries to your table model you could use a mutable model such as DefaultTableModel : DefaultTableModel model = new DefaultTableModel(data, columns);
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html
You will find these methods in it:
getValueAt(int row, int column)
getSelectedRow()
getSelectedColumn()
Use a mix of these to achieve your result.
You can use the following code to get the value of the first column of the selected row of your table.
int column = 0;
int row = table.getSelectedRow();
String value = table.getModel().getValueAt(row, column).toString();
if you want to get the data in the entire row, you can use this combination below
tableModel.getDataVector().elementAt(jTable.convertRowIndexToModel(jTable.getSelectedRow()));
Where "tableModel" is the model for the table that can be accessed like so
(DefaultTableModel) jTable.getModel();
this will return the entire row data.
I hope this helps somebody
Just simple like this:
tbl.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
String selectedCellValue = (String) tbl.getValueAt(tbl.getSelectedRow() , tbl.getSelectedColumn());
System.out.println(selectedCellValue);
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With