Why does JXTable lose input where JTable is not?

When I use JXTable to render and edit my data, some data in CellEditors is lost. If I click on the "Resize" section in the JXTable-ColumnHeader or change the width of the JFrame, CellEditor will stop working without assigning a value. Values ​​are saved if I use JTable.

I want to use JXTable because of its other features, so is there a way to fix JXTable?

Screenrecording

Example:

package table.columnresize; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import org.jdesktop.swingx.JXTable; /** * Demo of differing behaviour of JXTable and JTable. JXTable loses input in a TableCell where JTable persists * it. * <p> * <table border=1> * <tr> * <th></th> * <th>JXTable</th> * <th>JTable</th> * </tr> * <tr> * <td>Click on TableColumnHeader</td> * <td>saved</td> * <td>saved</td> * </tr> * <tr> * <td>Resizing with Divider of TableColumnHeader</td> * <td>lost</td> * <td>saved</td> * </tr> * <tr> * <td>Changing the width of JFrame</td> * <td>lost</td> * <td>saved</td> * </tr> * * </table> * </p> * * @author bobndrew 2015-01-29 */ public class JXTableAndJTableEditLossDemo { private static class DataModel extends DefaultTableModel { public DataModel( Object[][] data, Object[] columnNames ) { super( data, columnNames ); } } private static void createAndShowUI() { Object[][] DATA = { { "One", 1 }, { "Two", 2 }, { "Three", 3 }, { "Four", 4 }, { "Five", 5 } }; String[] COLUMNS = { "A", "B" }; DataModel dataModel = new DataModel( DATA, COLUMNS ); JFrame frame1 = new JFrame( "JXTable" ); JXTable jXTable = new JXTable( dataModel ); //does not change anything: jXTable.setTerminateEditOnFocusLost( true ); System.out.println( jXTable.isTerminateEditOnFocusLost() ); frame1.add( new JScrollPane( jXTable ) ); frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame1.pack(); frame1.setVisible( true ); JFrame frame2 = new JFrame( "JTable" ); JTable jTable = new JTable( dataModel ); //does not change anything: jTable.putClientProperty( "terminateEditOnFocusLost", Boolean.FALSE ); System.out.println( jTable.getClientProperty( "terminateEditOnFocusLost" ) ); frame2.add( new JScrollPane( jTable ) ); frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame2.pack(); frame2.setLocation( (int) frame1.getLocation().getX() + frame1.getWidth() + 100, (int) frame1 .getLocation().getY() ); frame2.setVisible( true ); } public static void main( String[] args ) { java.awt.EventQueue.invokeLater( new Runnable() { @Override public void run() { createAndShowUI(); } } ); } } 
+8
java swing swingx jtable jxtable
source share
2 answers

When debugging JXTable and JTable I found the reason for the loss of CellEdits. The difference lies in the columnMarginChanged() method:

JXTable:

 if (isEditing()) { removeEditor(); } 

JTable:

 if (isEditing() && !getCellEditor().stopCellEditing()) { getCellEditor().cancelCellEditing(); } 

At first I thought that the removeEditor() method is an improvement on JTable ... But then I found this OpenJDK changeset since September 2010 that fixes the error "4330950: recently lost data in a cell when resizing a column". It seems that the changes from the JDK have not been applied to the SwingX source code.

I will agree to my own answer because the reason for the other behavior is now clear. To fix this for me and other SwingX users, I will go to the SwingX mailing list and bug tracker.

+3
source share

When you look at the frameInit () method of JTable, you will see that it is associated with all AWTEvent.WINDOW * events. In JXTable, the initActionsAndBindings () method is bound to specific actions (for example, a changed value) and only for a table.

You will need to add your own listener.

  jXTable.getColumnModel().addColumnModelListener(new TableColumnModelListener() { @Override public void columnMarginChanged(ChangeEvent e) { } }); 

and then you have to set some table functionality to allow the event to trigger a table update. Or perhaps you can call TableModelEvent from there.

+1
source share

All Articles