1

I am attempting to obtain the row number someone has double-clicked in a JTable. The trigger fires, but it doesn't think I've clicked a row. When I retrieve the row number it is always -1:

informationTable.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            int row = informationTable.getSelectedRow();
            System.out.println(row); // always -1
            informationTable.setValueAt('1', row, MEAL_COL); // fails...
        }
    }
});

Why is this not giving me the correct row, any row for that matter?

Edit:

To answer how I turned off editing (for specific columns) I overrode the isCellEditable method of the DefaultTableModel class as follows:

private class KAMDTM extends DefaultTableModel {
    private final boolean[] canEdit = new boolean[] {false, false, false, false, false, false, true};

    public KAMDTM(Object[][] data, String[] cols) {
        super(data, cols);
    }
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit[columnIndex];
    }
}
Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28
  • why do you double click? when I try doing that, it takes me into editing a cell where my mouse is pointed at – Kebab Programmer Dec 16 '15 at 19:56
  • Editing is turned off. Even when I change it to do a single click it fails. – Rabbit Guy Dec 16 '15 at 19:57
  • How do you turn off editing? – Kebab Programmer Dec 16 '15 at 20:00
  • Please post your full code to see how you have turned off the editing? – Prabhakaran Dec 16 '15 at 20:03
  • K, this works for me, on my table, my Row is never -1, and if you are clicking outside the cells, you will get -1 – Kebab Programmer Dec 16 '15 at 20:04
  • I changed it back so that my cells are editable and still get -1 for a row selected. – Rabbit Guy Dec 16 '15 at 20:05
  • 3
    Your listener is probably added to a JTable that has not been added to the frame. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. That is create a frame add a table to the frame and then add the listener to the table. The whole code will be about 15 lines of code. Then I'm sure you will find the basic concept works and you will now need to debug you code to find the difference between the working code and your `SSCCE`. – camickr Dec 16 '15 at 20:07
  • are you using informationTable.setEnabled(false) to capture the mouse double click event? – Prabhakaran Dec 16 '15 at 20:12
  • try using selectionListener to select the row of the table. it seems like now row has been selected in the JTable. – Prabhakaran Dec 16 '15 at 20:27
  • This code works for me, I don't know how come it doesn't work for him, post a picture of your UI – Kebab Programmer Dec 16 '15 at 20:30
  • When I attempted to make an sscce (removing all my other components), it works. I am attempting to find the error now. I'll post my error when I find the solution. – Rabbit Guy Dec 16 '15 at 20:31
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Dec 16 '15 at 22:21
  • 1
    @camickr The listener is probably added to a component that is on the screen (otherwise it wouldn't not trigger), BUT they might not be using a reference to the component which is on the screen. BUT, the solution is still, runnable example – MadProgrammer Dec 16 '15 at 22:22
  • Instead of using a MouseListener on the JTable, you should use `table.getSelectionModel().addListSelectionListener( ...);` – FredK Dec 17 '15 at 15:39

1 Answers1

2

You can probably get answer from this post : Double click listener on JTable in Java , look at the second answer the code suggests to use :

Point p = evt.getPoint();
int row = table.rowAtPoint(p);
Community
  • 1
  • 1
SomeDude
  • 13,876
  • 5
  • 21
  • 44