Double click event in JTable

Hello,

how do I listen for a double click event, for example in aJTable list selection? At the moment the user has to select a row and click on a button to switch to detailed view - is it possible to respond to a double click in theJTable, to switch to detailed view, too?

Any help would be greatly appreciated

- Stephan

[361 byte] By [Jacklera] at [2007-11-27 11:03:54]
# 1

Maybe something like jtable.addMouseListener( new MouseAdapter() {

public void mouseClicked (MouseEvent e) {

if (e.getClickCount() == 2) {

// Do something

}

}

} );

JayDSa at 2007-7-29 12:54:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

Yes ... and the other poster's idea of an ActionListener is a good one but you may (depending on whether you're using a custom table renderer) need to move the code off of the table and into the renderer. If you're not using a renderer then I think you should be safe putting it on the table but you may end up needing a renderer to get the level of control you want.

PS.

puckstopper31a at 2007-7-29 12:54:05 > top of Java-index,Desktop,Core GUI APIs...
# 3

Double-clicking a cell in a JTable usually activates the cell editor, you can piggy back the editCellAt method to accomplish what you wanted to do. For example:

import javax.swing.*;

import javax.swing.table.AbstractTableModel;

import javax.swing.event.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

public class TableDemo extends JFrame {

private boolean DEBUG = true;

boolean T=true;

public TableDemo() {

super("TableDemo");

MyTableModel myModel = new MyTableModel();

JTable table = new JTable(myModel) {

/************/

public boolean editCellAt(int row, int col, EventObject e) {

if (e instanceof MouseEvent && ((MouseEvent)e).getClickCount()>1) {

if (T) {

T=false;

System.out.println("double-click trapped");

return false;

} else {

T=true;

}

}

return super.editCellAt(row,col,e);

}

};

/************/

table.setPreferredScrollableViewportSize(new Dimension(500, 70));

//Create the scroll pane and add the table to it.

JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this window.

getContentPane().add(scrollPane, BorderLayout.CENTER);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

class MyTableModel extends AbstractTableModel {

final String[] columnNames = {"First Name",

"Last Name",

"Sport",

"# of Years",

"Vegetarian"};

final Object[][] data = {

{"Mary", "Campione",

"Snowboarding", new Integer(5), new Boolean(false)},

{"Alison", "Huml",

"Rowing", new Integer(3), new Boolean(true)},

{"Kathy", "Walrath",

"Chasing toddlers", new Integer(2), new Boolean(false)},

{"Sharon", "Zakhour",

"Speed reading", new Integer(20), new Boolean(true)},

{"Angela", "Lih",

"Teaching high school", new Integer(4), new Boolean(false)}

};

public int getColumnCount() {

return columnNames.length;

}

public int getRowCount() {

return data.length;

}

public String getColumnName(int col) {

return columnNames[col];

}

public Object getValueAt(int row, int col) {

return data[row][col];

}

/*

* JTable uses this method to determine the default renderer/

* editor for each cell. If we didn't implement this method,

* then the last column would contain text ("true"/"false"),

* rather than a check box.

*/

public Class getColumnClass(int c) {

return getValueAt(0, c).getClass();

}

public boolean isCellEditable(int row, int col) {

return true;

}

/*

* Don't need to implement this method unless your table's

* data can change.

*/

public void setValueAt(Object value, int row, int col) {

data[row][col] = value;

fireTableCellUpdated(row, col);

}

}

public static void main(String[] args) {

TableDemo frame = new TableDemo();

frame.pack();

frame.setVisible(true);

}

}

;o)

V.V.

viravana at 2007-7-29 12:54:05 > top of Java-index,Desktop,Core GUI APIs...