tags:

views:

636

answers:

4

Hello! I'm trying for a long time to make a table that works like Excell do. It means, when user starts to insert data into the cells the content into them is selected and changed by the new data inserted. I don't know how to do it, I googled it a lot and didn't give any good idea.

All the best!

A: 

You should look at extJS. There is a pretty steep learning curve, though..

I found extJS working with java script and it is GPL liscence. I need at least a LGPL Liscence.
Leandro
+1  A: 

You can create a custom TableCellEditor for your table. This class will have an instance variable of a TextField, lets call it textField. Then the getTableCellEditorComponent method could look like this:

public Component getTableCellEditorComponent(JTable table, Object value, 
                             boolean isSelected, int row, int column ) {
    textField.setText(value.toString());
    textField.selectAll();
    return textField;
}
akf
Very good solution. Thank you!
Leandro
A: 

Creating a custom editor works fine if you only ever have String data in the table and only need a single editor. However, if you have multiple different types of data, like String, Integer, Double, currencies, percentages etc which all use a JTextField as an editor then you need to create multiple custom editors.

You can read up on the Table Select All Editor for another possible solution.

camickr
A: 

Note that there is also another possibility, you can override JTable#prepareEditor like the following:

@Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
    Component c = super.prepareEditor(editor, row, column);
    if (c instanceof JTextComponent) {
        ((JTextComponent) c).selectAll();
    } 
    return c;
}
Touko