Reputation: 938
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
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
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 JTable
s 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