cell editor removed when table resized
I'm using Java 1.5.0_06 in my application. I have a JFrame which contains a JTable. I use FormLayout (which is like GridBagLayout). When the frame resizes, the table resizes too.
When I enter edit mode in a cell of the table and resize the frame (and the table), the cell exits edit mode. The editor of the cell is removed when the table is resized which causes the value that I had entered in the cell to be lost.
Is there a way to stop cell editing when the frame is resized instead of the cell just exiting edit mode.
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
publicclass ResizableTableTestextends JFrame{
final JScrollPane _scrollPane;
publicstaticvoid main(String[] args){
ResizableTableTest frame =new ResizableTableTest();
frame.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.setSize(451, 340);
frame.setVisible(true);
}
public ResizableTableTest(){
super();
getContentPane().setLayout(new FormLayout(
new ColumnSpec[]{
new ColumnSpec("default:grow(1.0)"),
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC},
new RowSpec[]{
new RowSpec("default:grow(1.0)"),
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC}));
_scrollPane =new JScrollPane();
getContentPane().add(_scrollPane,new CellConstraints(1, 1, CellConstraints.FILL, CellConstraints.FILL));
addTableToScrollPane();
}
privatevoid addTableToScrollPane(){
JTable table =new JTable();
DefaultTableModel model = (DefaultTableModel)table.getModel();
table.setRowSelectionAllowed(false);
table.setCellSelectionEnabled(true);
//Add some columns
model.addColumn("column A");
model.addColumn("column B");
model.addRow(new Object[]{"item1","apple"});
model.addRow(new Object[]{"item2","banana"});
model.addRow(new Object[]{"item3","carrot"});
model.addRow(new Object[]{"item1","grape"});
_scrollPane.setViewportView(table);
}
}
thanks,

