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

JTable Does not cancel the editing when rows are moved

    XMLWordPrintable

    Details

      Description

      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",
                                      "Sport",
                                      "# of Years",
                                      "Vegetarian"};

              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();
                      dtm.setDataVector(data,columnNames);
              table = new JTable(dtm);
              dtm.addTableModelListener(this);
              table.setPreferredScrollableViewportSize(new Dimension(500, 70));
              JScrollPane scrollPane = new JScrollPane(table);
                  add(scrollPane);
                  availableLaf = UIManager.getInstalledLookAndFeels();
          }
          
          public void tableChanged(TableModelEvent tme ){
              System.out.println(tme.getFirstRow());
              tableChangedFired=true;
          }
          
          /**
           * 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");
                              System.exit(1);
                      }
              });
              newContentPane.setOpaque(true);
              frame.setContentPane(newContentPane);
              frame.setSize(400,300);
              frame.setVisible(true);
          }

          
              private void moveRow(){
                      DefaultTableModel dtm =(DefaultTableModel)table.getModel();
                      try{
                                      Thread.sleep(delay);
                                      tableChangedFired=false;
                                      //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);
                                      robot.mousePress(InputEvent.BUTTON1_MASK);
                                      robot.mouseRelease(InputEvent.BUTTON1_MASK);
                                      robot.mousePress(InputEvent.BUTTON1_MASK);
                                      robot.mouseRelease(InputEvent.BUTTON1_MASK);
                                      robot.mousePress(InputEvent.BUTTON1_MASK);
                                      robot.mouseRelease(InputEvent.BUTTON1_MASK);
                                      Thread.sleep(3000);
                                      dtm.moveRow(0,1,2);
                                      System.out.println("Moving the Row from 0 to 2");
                                      if(newContentPane.waitForEvent()){
                                              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);
                                              failedAssertions++;
                                      }
                                      //Moving Multiple Rows
                                      dtm.moveRow(0,3,5);
                                      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);
                                              failedAssertions++;
                                      }
                      }catch(Exception e){
                              System.out.println("Some problem in Moving rows. " +e.toString());
                              e.printStackTrace();
                      }
              }
              
              public boolean waitForEvent(){
                      int iteration=0;
                      while(!tableChangedFired){
                              try{
                                      Thread.sleep(delay);
                              }catch(Exception e){
                              }
                              iteration++;
                              if(iteration==20){
                                      System.out.println("Event did not fire till now. Breaking....");
                                      return false;
                              }
                      }
                      return true;
              }
              
          public static void main(String[] args) {
                      newContentPane = new JTable_MoveRows();
                      try{
              javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                  public void run() {
                      createAndShowGUI();
                  }
              });
                      }catch(Exception e){
                              System.out.println("Some probelm in creating and showing the UI " +e.toString());
                              e.printStackTrace();
                      }
                      int i=0;
                      try{
                              
                      //for( i=0;i<availableLaf.length;i++){
                              //UIManager.setLookAndFeel(availableLaf[i].getClassName());
                              //SwingUtilities.invokeAndWait(new Runnable(){
                                      //public void run(){
                                              //SwingUtilities.updateComponentTreeUI(frame);
      // }
              // });
                      // currentlaf=availableLaf[i].getClassName();
                              //System.out.println("Testing for " + currentlaf);
                              newContentPane.moveRow();
                      //}
                      }catch(Exception e){
                              //System.out.println("Some problem in Changing the look and Feel from " +currentlaf +" to " +availableLaf[i].getClassName() +" " +e.toString());
                              e.printStackTrace();
                              newContentPane.failedAssertions++;
                      }
                      
                      if(newContentPane.failedAssertions>1){
                              System.out.println("Some assertions failed");
                              //System.exit(1);
                      }else{
                              System.out.println("Test Passed");
                              //System.exit(0);
                      }
          }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              peterz Peter Zhelezniakov
              Reporter:
              mjoshisunw Mayank Joshi (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Imported:
                Indexed: