insilenzio
insilenzio

Reputation: 938

JTable how to fire event selecting a row or double click on a row

I'm using a javax.swing.JTable to show rows in a database table. I need to fire two different event for two different cases:

I already looked for an answer on stack overflow, but I didn't find anything satisfying . Any idea?

Upvotes: 1

Views: 13114

Answers (2)

eatSleepCode
eatSleepCode

Reputation: 4637

You can add a mouse listener to the table and capture event over there with mouse event like below

table.addMouseListener
      (
        new MouseAdapter()
        {
          public void mouseClicked(MouseEvent e)
          {
            if (e.getClickCount() == 2)
            {
            }
            if (e.getClickCount() == 1)
            {
            }
          }
        }
      );
    }

and to capture selection event you can use

table.getSelectionModel().addListSelectionListener(...);

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347204

when is selected at least a row (or a click on at least a row is performed).

You should monitor changes to the row selection using the JTables ListSelectionModel via a ListSelectionListener. This will notify you when the selection is changed by the user using the mouse or the keyboard or if the selection is changed programmatically for some reason

See How to Write a List Selection Listener for more details

when a double click on a row is performed

The only way you can detect this is through a use of a MouseListener. Normally, users expect that a left mouse button click will do one action and the right mouse button will do something else.

You will want to use SwingUtilities.isLeftMouseButton or SwingUtilities.isRightMouseButton to determine what the user is actually doing.

See How to Write a Mouse Listener for more details

Upvotes: 4

Related Questions