Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6236158

JTable Does not cancel the editing when rows are moved




      If a cell is edited and rows are moved, the editing
      Please run the attached test case.
      Notice that the cell(2,2) is made editable.
      Now the rows moves.
      After Move, Row-6 is on index 1.
      The value at (5,2) was pool but after the move it is Rowing, which was value at (2,2).
      Now you click the mouse outside, to unedit the cell, notice that value is still rowing and not pool.
      Please see the attached image for before and after behaviour.
      ###@###.### 2005-03-04 12:21:11 GMT
      I am adding the test case (JTable_MoveRows.java) here so it will be visible to the
      jdk.dev.java.net community:

      import java.awt.Dimension;
      import java.awt.GridLayout;
      import java.awt.Rectangle;
      import java.awt.Robot;
      import java.awt.event.InputEvent;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.SwingUtilities;
      import javax.swing.UIManager;
      import javax.swing.event.TableModelEvent;
      import javax.swing.event.TableModelListener;
      import javax.swing.table.DefaultTableModel;

      public class JTable_MoveRows extends JPanel implements TableModelListener{
          private boolean DEBUG = false;
              static JTable table;
              static boolean tableChangedFired=false;
              static JTable_MoveRows newContentPane;
              int failedAssertions=0;
              static UIManager.LookAndFeelInfo availableLaf[];
              static JFrame frame;
              static String currentlaf="";
              static int delay=2000;
          public JTable_MoveRows() {
              super(new GridLayout(1,0));

              String[] columnNames = {"First Name",
                                      "Last Name",
                                      "# of Years",

              Object[][] data = {
                  {"Row1", "Campione",
                   "Snowboarding", new Integer(5), new Boolean(false)},
                  {"Row2", "Huml",
                   "Rowing", new Integer(3), new Boolean(true)},
                  {"Row3", "Walrath",
                   "Knitting", new Integer(2), new Boolean(false)},
                  {"Row4", "Zakhour",
                   "Speed reading", new Integer(20), new Boolean(true)},
                  {"Row5", "Milne",
                   "Pool", new Integer(10), new Boolean(false)},
                              {"Row6", "Milne",
                                                       "Pool", new Integer(10), new Boolean(false)},
                              {"Row7", "Milne",
                                                       "Pool", new Integer(10), new Boolean(false)},
                              {"Row8", "Milne",
                                                       "Pool", new Integer(10), new Boolean(false)},
                              {"Row9", "Campione",
                                                       "Snowboarding", new Integer(5), new Boolean(false)},
                              {"Row10", "Campione",
                                                       "Snowboarding", new Integer(5), new Boolean(false)}
                      DefaultTableModel dtm = new DefaultTableModel();
              table = new JTable(dtm);
              table.setPreferredScrollableViewportSize(new Dimension(500, 70));
              JScrollPane scrollPane = new JScrollPane(table);
                  availableLaf = UIManager.getInstalledLookAndFeels();
          public void tableChanged(TableModelEvent tme ){
           * Create the GUI and show it. For thread safety,
           * this method should be invoked from the
           * event-dispatching thread.
          private static void createAndShowGUI() {
             frame = new JFrame("SimpleTableDemo");
              frame.addWindowListener(new WindowAdapter(){
                      public void windowClosing(WindowEvent we){
                              System.out.println("User Closed the Frame. Test case failed");

              private void moveRow(){
                      DefaultTableModel dtm =(DefaultTableModel)table.getModel();
                                      //Moving Single Row
                                      Rectangle rect =table.getCellRect(2,2,true);
                                      Robot robot = new Robot();
                                      robot.mouseMove(new Double(rect.getCenterX()).intValue(),new Double(rect.getCenterY()).intValue()+20);
                                      System.out.println("Moving the Row from 0 to 2");
                                              System.out.println("TableChangedEvent fired for " + 0 +" 1");
                                              System.out.println("Row Moved from 0 to 2");
                                              System.out.println("Assertion Passed passed for "+currentlaf);
                                      }else {
                                              System.out.println("TableChangedEvent NOT fired for " + 0 +" 1");
                                              System.out.println("Row NOT Moved from 0 to 2");
                                              System.out.println("Assertion Failed for "+currentlaf);
                                      //Moving Multiple Rows
                                      System.out.println("Moving the Row through 0 to 3, to 5");
                                      if(newContentPane.waitForEvent() ){
                                              System.out.println("TableChangedEvent fired for " + 0 +" 1");
                                              System.out.println("Row Moved from 0 to 3, to 5");
                                              System.out.println("Assertion Passed for " +currentlaf);
                                      }else {
                                              System.out.println("TableChangedEvent NOT fired for " + 0 +" 1");
                                              System.out.println("Row NOT Moved from 0 to 3, to 5");
                                              System.out.println("Assertion Failed for " +currentlaf);
                      }catch(Exception e){
                              System.out.println("Some problem in Moving rows. " +e.toString());
              public boolean waitForEvent(){
                      int iteration=0;
                              }catch(Exception e){
                                      System.out.println("Event did not fire till now. Breaking....");
                                      return false;
                      return true;
          public static void main(String[] args) {
                      newContentPane = new JTable_MoveRows();
              javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                  public void run() {
                      }catch(Exception e){
                              System.out.println("Some probelm in creating and showing the UI " +e.toString());
                      int i=0;
                      //for( i=0;i<availableLaf.length;i++){
                              //SwingUtilities.invokeAndWait(new Runnable(){
                                      //public void run(){
      // }
              // });
                      // currentlaf=availableLaf[i].getClassName();
                              //System.out.println("Testing for " + currentlaf);
                      }catch(Exception e){
                              //System.out.println("Some problem in Changing the look and Feel from " +currentlaf +" to " +availableLaf[i].getClassName() +" " +e.toString());
                              System.out.println("Some assertions failed");
                              System.out.println("Test Passed");


          Issue Links



              peterz Peter Zhelezniakov
              mjoshisunw Mayank Joshi (Inactive)
              0 Vote for this issue
              0 Start watching this issue