Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Setting the Activation Click Count for a Table Cell Editor in a JTable Component

import java.awt.Component; import java.awt.event.MouseEvent; import java.util.EventObject; import javax.swing.AbstractCellEditor; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.TableCellEditor; import javax.swing.table.TableColumn; public class Main {   public static void main(String[] argv) throws Exception {     JTable table = new JTable();     TableColumn col = table.getColumnModel().getColumn(0);     col.setCellEditor(new MyTableCellEditor());   } } class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {   public boolean stopCellEditing() {     String s = (String) getCellEditorValue();     return super.stopCellEditing();   }   JTextField field = new JTextField();   public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {         return field;   }      public boolean isCellEditable(EventObject evt) {       if (evt instanceof MouseEvent) {           int clickCount;           clickCount = 2;           return ((MouseEvent)evt).getClickCount() >= clickCount;       }       return true;   }   public Object getCellEditorValue() {     return null;   } }