Dave Pradana
Dave Pradana

Reputation: 145

Reading the Data of a Highlighted Jtable Row

I have a JTable with rows of Data

I have this event that listen every time a row got mouse clicked

private void tablePOMouseClicked(java.awt.event.MouseEvent evt) {                                      
    try {
        int row1 = tablePO.getSelectedRow();
    cellA = tablePO.getValueAt(row1, 0).toString();
    cellB = tablePO.getValueAt(row1, 1).toString();
    cellC = tablePO.getValueAt(row1, 2).toString();
    cellD= tablePO.getValueAt(row1, 3).toString();
    cellE = tablePO.getValueAt(row1, 4).toString();
    cellF = tablePO.getValueAt(row1, 5).toString();
    cellG = tablePO.getValueAt(row1, 6).toString();
    cellH = tablePO.getValueAt(row1, 7).toString();

    } catch (Exception e) {
    }
}

variable cellA-H are all Strings.

its working good, but now I want to change it, I dont want the user to have the need to use the mouse, so instead, I want the user just select the row with using either UP/DOWN arrow to navigate the rows and put the selected row under the highlight, but I have no Idea how I am able to achieve it, reading the data from highlighted/selected row by using the UP/DOWN Keys (Not by pointing the row with mouse click).

Upvotes: 0

Views: 60

Answers (1)

camickr
camickr

Reputation: 324108

Add a ListSelectionListener to the table.

An event will be generated whenever the row selection changes whether you use the mouse or the keyboard.

Read the section from the Swing tutorial on How to Write a ListSelectionListener for more information and working examples.

Upvotes: 1

Related Questions