> When I editing cells in a JTable I press F2, delete the content of the cell,
> then I update the cell.
> What is the simpliest way to owrite the content of the cell when I type a
> new content?
One way is to override the table's editCellAt(...) method,
and use the overridden table class instead of JTable:
import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.ListSelectionModel;
import java.util.Vector;
import java.util.EventObject;
import java.awt.Component;
import javax.swing.*;
import javax.swing.text.JTextComponent;
public class OverWriteTable extends JTable {
public OverWriteTable () {
}
public OverWriteTable (TableModel dm) {
super(dm);
}
public OverWriteTable (TableModel dm, TableColumnModel cm) {
super(dm, cm);
}
public OverWriteTable (TableModel dm, TableColumnModel cm,
ListSelectionModel sm) {
super(dm, cm, sm);
}
public OverWriteTable (int numRows, int numColumns) {
super(numRows, numColumns);
}
public OverWriteTable (Vector rowData, Vector columnNames) {
super(rowData, columnNames);
}
public OverWriteTable (Object[][] rowData, Object[] columnNames) {
super(rowData, columnNames);
}
public boolean editCellAt(int row, int column, EventObject e) {
boolean res = super.editCellAt(row, column, e);
Component c = this.getEditorComponent();
if (c instanceof JTextComponent) {
( (JTextField) c).selectAll();
}
return res;
}
}
Another way is to use a custom CellEditor where you need it:
import java.awt.Component;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.*;
import java.util.*;
public class TextCellEditor extends DefaultCellEditor implements
FocusListener {
JTextField tf;
public TextCellEditor(JTextField tf){
super(tf);
this.tf = tf;
tf.addFocusListener(this);
super.setClickCountToStart(1);
}
public boolean stopCellEditing() {
String s = (String)getCellEditorValue();
if( !isValid(s) ) {
// Display errormessage here...
return false;
}
return super.stopCellEditing();
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row, int column) {
tf.setText(value.toString());
return tf;
}
public void focusGained(FocusEvent e) {
tf.selectAll();
}
public void focusLost(FocusEvent e) {
}
private boolean isValid(String s) {
//Do validation here
return true;
}
}

Signature
Dag
58?26'15.9" N 008?46'45.5" E