Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable TableCellEditor, start editing on single or double click?

810805Dec 2 2010 — edited Dec 3 2010
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2010
Added on Dec 2 2010
11 comments
3,186 views