neza
neza

Reputation: 124

JTable choose other rows and get their data when certain cell is clicked

I have a JTable. I want to create an event for one cell that if the user clicks on it, he is able to choose one or more rows from the table and the corresponding IDs are saved in that cell.

enter image description here

So in the example the user would click on "Click here to choose" in row 2 and then click on e.g. row 1 and row 3. The cell "click here to choose" should then be overwritten with something like 1 and 3 afterwards:

enter image description here

I'm thinking of somehow creating a MouseAdapter Event on click on the cell but I have no real idea how to do it. Any idea how I can approach this?

Upvotes: 0

Views: 49

Answers (2)

trashgod
trashgod

Reputation: 205785

Use a ListSelectionListener with MULTIPLE_INTERVAL_SELECTION. In the handler, update the table's model using setValueAt() to reflect the change.

Upvotes: 1

TheMMSH
TheMMSH

Reputation: 510

yes you definitely need to use MouseAdapter as below(you have the cell if "if condition become true"):

jt.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int r = jt.rowAtPoint(evt.getPoint());
        int c = jt.columnAtPoint(evt.getPoint());
        if (r >= 0 && c >= 0) {
            ......

        }
    }
});

Upvotes: 0

Related Questions