34

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 linkedlist is search (in this case it contains movie titles) and displays the values in the linked list in Jtextboxes

How do I do this?

Here is the code

My guess it retrieve the data from the selected row into an array, split it, and put it into the jtextareas. How can I do this ?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Brian
  • 1,951
  • 16
  • 56
  • 101
  • i provided a similar answer [here](http://stackoverflow.com/questions/29345792/java-jtable-getting-the-data-of-the-selected-row/37590500#37590500) hope it helps – Damilola Fagoyinbo Jun 02 '16 at 11:29

6 Answers6

85

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.

adheus
  • 3,985
  • 2
  • 20
  • 33
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
  • 18
    You can check if (!e.getValueIsAdjusting() && table.getSelectedRow() != -1) before System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString()); – Rod Lima Oct 05 '16 at 12:55
  • @RodrigoGarcia - can you please explain why they should add that check? – Ascalonian Apr 05 '19 at 18:57
  • 1
    Sorry @Ascalonian, I can't remember anymore, I should have explained better. – Rod Lima Apr 05 '19 at 19:25
  • 1
    @RodLima, what is the matter with you Rod, why can not you remember, we were coding together and you found the solution to prevent calling the event twice for each selection ! – Raiden Core Oct 14 '21 at 14:11
7
 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

swapnil gandhi
  • 816
  • 1
  • 20
  • 38
Shinwar ismail
  • 299
  • 2
  • 9
5

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • If you have trouble implementing your listener, please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem. – trashgod Apr 12 '12 at 20:30
2

From the source with some enhancement and edit:

public class RowSelectionListener implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent event) {

        int viewRow = table.getSelectedRow();

        if (!event.getValueIsAdjusting() && viewRow != -1) {

            int columnIndex = 1;

            // Better to access table row using modelRow rather than viewRow
            int modelRow = table.convertRowIndexToModel(viewRow);

            // Access value at selected row at the second column (columnIndex = 1)
            Object modelvalue = table.getModel().getValueAt(modelRow, columnIndex);

            // Not recommended: same as above but access row using viewRow
            Object tablevalue = table.getValueAt(viewRow, columnIndex);

            // Print cell value
            System.out.println(modelvalue + "=" + tablevalue);
        }
    }
}

Then add ListSelectionListener to JTable:

 table.getSelectionModel().addListSelectionListener(new RowSelectionListener());

Important Note:

viewRow and modelRow become effectively different when applying TableRowSorter.

Saleh Rezq
  • 193
  • 1
  • 13
1

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

Community
  • 1
  • 1
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • So i tried out the code, theo e of the mouselistener, how do I store in an array after I getSelectedRow() and getSelectedColumn() ? – Brian Apr 12 '12 at 18:36
0

You can use the MouseClicked event:

private void tableMouseClicked(java.awt.event.MouseEvent evt) {
 // Do something.
}
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
Trieu Doan
  • 149
  • 1
  • 3