Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable Double click  RSS feed

 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how to capture the double click event on a JTable
Here is what I used for a single click:
m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSM = m_table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) {
return;
}
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
if (lsm.isSelectionEmpty()) {
System.out.println("No rows are selected.");
}
else {
int selectedRow = lsm.getMinSelectionIndex();
System.out.println("Row " + selectedRow + " is now selected.");
// Display data from column
//selected
System.out.println((String) m_table.getValueAt(m_table.getSelectedRow(), m_table.getSelectedColumn()));
// Display data from first
column of select row
System.out.println((String) m_table.getValueAt(m_table.getSelectedRow(), 0));
}
}
});

What I am realy looking for is the first column's data of the double clicked row.
Thanks
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if you want to try mouseListener. Here is some code in my project. I hope it can solve your problem.
yourTable.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
JTable table =(JTable) me.getSource();
Point p = me.getPoint();
int row = table.rowAtPoint(p);
int col = table.columnAtPoint(p);
if ((col == 0) &&(me.getClickCount() ==2) ) {

// stop table editing and get your data..
}
}
});
good luck!
 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou the mouse listner works, but it only works if I double-click on the first column (0). Is there anyway to double click also on the other columns?
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dean,
How about using Renees' code and removing the following:
( col == 0 )
Just a thought ...
Manfred.
 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone,
Thanks! That did it, I was no paying attention. I appreciate it!
Dean Reedy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!