Sunday, September 17, 2006

How to Create Double-Click Event on a JTable in Swing?

I have created a basic application with buttons, combo boxes a JTable with a JScrollPane and want to have a double-click event on my JTable which opens an "Edit Detail" screen that pulls up the record's data and allows data to be filled in and then saved.

Here is how to set up a double-click event that opens a new form:

1) First set up Mouse Listener on your JTable:
  myJTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
// do some action
}
}
});
This will give you the row and column that was double clicked.

2) You can then use this to get the value of that row's ID and open a new form and populate it with this record's data. Here is how you would open a new form
   JFrame newFrame = new JFrame();
newFrame.setTitle("Detail Screen");

newFrame.setVisible(true);


The below link was useful in working out how to do this and has many other helpful items:

Detect Double Click


14 comments:

Anonymous said...

Just a suggestion; you might want to use the ContextMenu / right click instead. The right click is the norm for desktop / fat clients; double click is the norm for web apps.

To do this, your mouseListener should implement both mousePressed and mouseReleased, and then just show a popupMenu. you can add menus, actions etc to your popup menu, and dynamically change text / actions according to the content of the cell etc.

public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(table, e.getX(), e.getY());
}
}

public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.show(table, e.getX(), e.getY());
}
}

Blackfoot said...

Thanks for the tip. How do you implement a double or single right-click?

Anonymous said...

Note that your code will not work if a cell is editable, because the MouseEvent will be catched by CellEditor listener to start editing.

Blackfoot said...

Thanks for the note. I haven't had the use of an editable cell table, but this is good to know for the future.

Anonymous said...

"The right click is the norm for desktop / fat clients; double click is the norm for web apps."

Strange, I've never heard about that "norm" before. Where did you see that? Double-click on lists have been common practice for years in fat clients. It's present in both Windows and Linux (Gnome, KDE), in many applications of several kinds.

CABallero said...

Dude, you saved my day.

Blackfoot said...

Glad I could help Crispin.

Unknown said...

Thanks for the tip. But what about delay between clicks?
It's depend on your operating system configuration or what?

Jibi said...

Thanks mate. This was of great help.

Unknown said...

This code snippet helped me !!!
Thanks

Anonymous said...

English language is not my main language, but I could understand it using the google translator. Amazing publish, keep them coming! Thanks for your time!

Nikola said...

Very good stuff.....simple and it's working... :)

Black Widow said...

This entire forum has been very helpful. However, can you tell me if there is a way to simulate double clicking in a specific location of an editable cell? For example, in the middle of existing test.
Or does the double-click place focus after existing data?
I realize the behavior in Excel is different than the default java table. But I want to determine if keystrokes are sent to a cell, at the same time, can that focus be directed to a specific spot/location area within the editable cell. Thanks, Barbara

Mangesh said...

it was really helpful for me.
just help me some more. pls guide me, how can i create pop up menu? i am implementing original post logic.