In a JTable i have a custom Editor and Renderer that edit and display the same panel, containing a JTextField + JButton.
To edit the text field the standard gesture would be the double click, because it is in a JTable cell.
To click the button the standard gesture would be just a single click on it.
The CellEditor interface provides an easy way to select a gesture, but it applies to the whole cell, so making alternatively the JButton or the JTextField non standard.
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) { return ((MouseEvent)anEvent).getClickCount() >= 2; }
return true;
}
I want double click for the JTextField and single click for the JButton!
Thanks in advance for any suggestion!
Agostino
The SSCE: try it for the single click and uncomment the code in the isCellEditable for the double click!
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.table.*;
public class SSCE2 {
public static void main(String[]args) {
JFrame f = new JFrame("Test");
JTable t = new JTable(2,2);
t.getColumnModel().getColumn(1).setCellEditor(new RE());
t.getColumnModel().getColumn(1).setCellRenderer(new RE());
f.add(t);
f.pack();
f.setVisible(true);
}
public static class RE extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
JPanel p = new JPanel(new BorderLayout());
JTextField tf = new JTextField();
JButton b = new JButton("...");
public RE() {
b.setFocusable(false);
p.add(tf);
p.add(b, BorderLayout.EAST);
}
public Object getCellEditorValue() {
return tf.getText();
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
tf.setText((String)value);
return p;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
tf.setText((String)value);
return p;
}
@Override
public boolean isCellEditable(EventObject anEvent) {
//UNCOMMENT TO ENABLE DOUBLE-CLICK-EDIT
//if (anEvent instanceof MouseEvent) {
// return ((MouseEvent)anEvent).getClickCount() >= 2;
//}
return true;
}
}
}
Edited by: Darryl Burke -- broke two long code lines