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,

[5170 byte] By [tsca] at [2007-11-27 4:29:00]
# 1
Not sure if this works of frame resizing...table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
camickra at 2007-7-12 9:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 2
No, that did not work when table is resized.thanks,
tsca at 2007-7-12 9:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 3
I would assume that on the resize of the table the editing cancelled event is being fired...You could try overriding the method in the JTable and when it gets fired, fire the editing stopped method instead... not sure whether that will work though
c0demonk3ya at 2007-7-12 9:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I would assume that on the resize of the table the

> editing cancelled event is being fired...

>

> You could try overriding the method in the JTable and

> when it gets fired, fire the editing stopped method

> instead... not sure whether that will work though

I tried that, but it didn't help. On searching for this problem, I saw a bug report on this very problem.

Here's info on a related bug in java:

[url=http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4503845]Cell editing does not complete when JTable loses focus[/url]

[url=http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4330950]Lost newly entered data in the cell when resizing column width[/url]

And here's an interesting article:

[url=http://www.erasurewars.net/editabletablecells/]Why Editable Table Cells Are Evil[/url]

Message was edited by:

petes1234

petes1234a at 2007-7-12 9:37:53 > top of Java-index,Desktop,Core GUI APIs...