neza neza - 3 months ago 7x
Java Question

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?


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() {
    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) {