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

Arrow Keys reverse if setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT

XMLWordPrintable

    • b10
    • x86
    • windows_xp
    • Verified

      FULL PRODUCT VERSION :
      1.6.0-b105

      ADDITIONAL OS VERSION INFORMATION :
      windows XP ver 5.1.2600

      A DESCRIPTION OF THE PROBLEM :
      In JTable
      when you setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)
      the left & rigth arrow keys (VK_LEFT & VK_RIGTH ) works reverse
      means that when you press left key it goes to rigth cell and
      when you press rigth key it goes to left key


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package editorTest;

      import java.awt.BorderLayout;

      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import java.awt.*;
      import javax.swing.JTable;
      import javax.swing.JPanel;
      import javax.swing.JButton;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      /**
       * <p>Title: </p>
       *
       * <p>Description: </p>
       *
       * <p>Copyright: Copyright (c) 2006</p>
       *
       * <p>Company: </p>
       *
       * @author not attributable
       * @version 1.0
       */
      public class JTableDemo extends JFrame {
          JScrollPane jScrollPane1 = new JScrollPane();
          JTable jTable1 = new JTable(3,5);
          JPanel contentPane;
          JButton jButton1 = new JButton();
          public JTableDemo() {
              try {
                  setDefaultCloseOperation(EXIT_ON_CLOSE);
                  jbInit();
              } catch (Exception exception) {
                  exception.printStackTrace();
              }
          }

          private void jbInit() throws Exception {
              contentPane = (JPanel) getContentPane();
             contentPane.setLayout(null);
              setSize(new Dimension(500,400));
              setPreferredSize(getSize());
              jScrollPane1.setBounds(new Rectangle(28, 21, 372, 228));
              jButton1.setBounds(new Rectangle(140, 261, 217, 25));
              jButton1.setText("setComponetOrientation(RTL)");
              jButton1.addActionListener(new JTableDemo_jButton1_actionAdapter(this));
              contentPane.add(jScrollPane1);
              contentPane.add(jButton1);
              jScrollPane1.getViewport().add(jTable1);
          }

          public static void main(String[] args) {
              JTableDemo jtabledemo = new JTableDemo();


              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              Dimension frameSize = jtabledemo.getSize();
              if (frameSize.height > screenSize.height) {
                  frameSize.height = screenSize.height;
              }
              if (frameSize.width > screenSize.width) {
                  frameSize.width = screenSize.width;
              }
              jtabledemo.setLocation((screenSize.width - frameSize.width) / 2,
                                (screenSize.height - frameSize.height) / 2);
              jtabledemo.pack();
              jtabledemo.setVisible(true);

          }

          public void jButton1_actionPerformed(ActionEvent e) {
           jTable1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
          }


      }


      class JTableDemo_jButton1_actionAdapter implements ActionListener {
          private JTableDemo adaptee;
          JTableDemo_jButton1_actionAdapter(JTableDemo adaptee) {
              this.adaptee = adaptee;
          }

          public void actionPerformed(ActionEvent e) {
              adaptee.jButton1_actionPerformed(e);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      /**
      *Active grid changes the left and rigth key actions
      */
      public class ActiveGrid extends JTable {
              /**
               *
               */
               protected final KeyStroke GRID_LEFT_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
                  protected final KeyStroke GRID_RIGTH_KEY_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
              private final long serialVersionUID = -6797101961102906866L;
          public final int DEFAULT_GRID_STOP_EDIT_EVENT=KeyEvent.VK_ENTER;
              public ActiveGrid() {
                      super();
              }
      // *******************************************************************************
              public ActiveGrid(TableModel dm) {
                      super(dm);
              }
      // *******************************************************************************
              public ActiveGrid(TableModel dm, TableColumnModel cm) {
                      super(dm, cm);
              }
      // *******************************************************************************
              public ActiveGrid(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
                      super(dm, cm, sm);
              }
      // *******************************************************************************
              public ActiveGrid(int numRows, int numColumns) {
                      super(numRows, numColumns);
              }
      // *******************************************************************************
              public ActiveGrid(Vector rowData, Vector columnNames) {
                      super(rowData, columnNames);
              }
      // *******************************************************************************
              public ActiveGrid(Object[][] rowData, Object[] columnNames) {
                      super(rowData, columnNames);
              }
      // *******************************************************************************
              /**
                *@Override JTable.processKeyBinding(KeyStroke stroke, KeyEvent evt, int condition, boolean pressed)<br/>
                * Default JTable starts Editing on ESC key this code prevents start edinting when key is ESC<br/>
                * if((editorComp==null)&&(evt.getKeyCode()==KeyEvent.VK_ESCAPE))
                * return false;
                * if editing starts editorComp is not null
                * the condition is true just when editing has not been starting
                * @author salarkia
                */
              protected boolean processKeyBinding(KeyStroke stroke, KeyEvent evt, int condition, boolean pressed)
               {
                      if((editorComp==null)&&(evt.getKeyCode()==KeyEvent.VK_ESCAPE))
                              //Salarkia handelling startEding will not occure with ESC key
                      return false;
                       boolean retvalue=super.processKeyBinding(stroke, evt, condition, pressed);
          
                      return retvalue;
               }
          
      //*******************************************************************
               public boolean isFunctionalKey(KeyEvent evt){
                       return evt.getKeyCode()==KeyEvent.VK_ENTER;
               }
      // *******************************************************************
                  private void initActionManagement(){
                      InputMap im = this.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                              InputMap imFocusComp = this.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                              KeyStroke right = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
                              KeyStroke left = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
                              final Action oldRigthAction = this.getActionMap().get(imFocusComp.get(GRID_RIGTH_KEY_STROKE));
                           final Action oldLeftAction = this.getActionMap().get(imFocusComp.get(GRID_LEFT_KEY_STROKE));
          
                   if(!getComponentOrientation().isLeftToRight()&&getJdkVesion()>=5 ){
                          this.getActionMap().put(im.get(left),createLeftKeyAction(oldRigthAction));
                            this.getActionMap().put(im.get(right),createRigthKeyAction(oldLeftAction));
                        } else{
                            this.getActionMap().put(im.get(left),createLeftKeyAction(oldLeftAction));
                            this.getActionMap().put(im.get(right),createRigthKeyAction(oldRigthAction));
                        }
                  }
      // *********************************************************************
                  /**
                   * getVersion of jdk
                   *@deprecated
                   */
                   private int getJdkVesion(){
                       String key =System.getProperty("java.vm.version");
                       try {
                                      return Integer.parseInt(key.substring(2,3));
          
                              } catch (NumberFormatException e) {
                                      e.printStackTrace();
                              }
                              return 4;
                   }
      //*********************************************************************
                   /**
                 * modifies rigth key action
                 * @return Action
                 */
                protected Action createRigthKeyAction(Action action){
          
                    final Action oldRigthAction =action;
                Action rigthAction = new AbstractAction()
                {
                    /**
                             *
                             */
                            private static final long serialVersionUID = -5071250218559880658L;
          
                            public void actionPerformed(ActionEvent e)
                    {
                        oldRigthAction.actionPerformed( e );
                        JTable table = (JTable)e.getSource();
                        if(actionIsInvalid(table))return;
                        int rowCount = table.getRowCount();
                        int columnCount = table.getColumnCount();
                        int row = table.getSelectedRow();
                        int column = table.getSelectedColumn();
                        int restoreRow=row;
                        int restoreColumn=column;
                        while (!(table.getColumnModel().getColumn(column).getPreferredWidth()>0)&&table.isEnabled())
                        {
                            int []rowCol;
                            if(getComponentOrientation()==ComponentOrientation.LEFT_TO_RIGHT){
                                    rowCol= getRowAndColumnForRigthNavigation(row,column,rowCount,columnCount);
                            }
                            else{
                                    rowCol=getRowAndColumnForLeftNavigation(row,column,rowCount,columnCount);
                            }
                            row=rowCol[0];
                            column=rowCol[1];
          
                         if(row!=restoreRow||row<0||row>rowCount){//no active row found
                                       //stay where we are
                                int temp=restoreColumn;
                                temp+=getComponentOrientation().isLeftToRight()?-1:1;
                                if(temp >0&&temp<table.getColumnCount()){
                                        restoreColumn=temp;
                                }
                                 row=restoreRow;
                                 column=restoreColumn;
                                 break;
                         }
      // Back to where we started, get out.
                            if (row == table.getSelectedRow()
                            && column == table.getSelectedColumn())
                            {
                                break;
                            }
                        }
                       table.changeSelection(row, column, false, false);
          
                    }
                };
                return rigthAction;
               }
      // *******************************************************************************
               protected Action createLeftKeyAction(Action action){
      // InputMap im = this.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
               //
      // final Action oldLeftAction = this.getActionMap().get(im.get(GRID_LEFT_KEY_STROKE));
                     final Action oldLeftAction =action;
                Action leftAction = new AbstractAction()
                {
                    /**
                             *
                             */
                            private static final long serialVersionUID = -5071250218559880658L;
          
                            public void actionPerformed(ActionEvent e)
                    {
                        oldLeftAction.actionPerformed( e );
                        JTable table = (JTable)e.getSource();
                        if(actionIsInvalid(table))return;
                        int rowCount = table.getRowCount();
                        int row = table.getSelectedRow();
                        int column = table.getSelectedColumn();
                        int restoreRow=row;
                        int restoreColumn=column;
                        //while (! table.isCellEditable(row, column) && table.isEnabled())
                         while (!(table.getColumnModel().getColumn(column).getPreferredWidth()>0)&&table.isEnabled())
                        {
          
                            // Back to where we started, get out.
                           int[] rowCol;
                           if(getComponentOrientation()==ComponentOrientation.LEFT_TO_RIGHT){
                             rowCol=getRowAndColumnForLeftNavigation(row,column,rowCount,getColumnCount());
                           }
                           else{
                               rowCol=getRowAndColumnForRigthNavigation(row,column,rowCount,getColumnCount());
                           }
                           row=rowCol[0];
                           column=rowCol[1];
          
                           if(row!=restoreRow||row<0||row>rowCount){//no active row found
                               //stay where we are
                                int temp=restoreColumn;
                                temp+=getComponentOrientation().isLeftToRight()?1:-1;
                                if(temp >0&&temp<table.getColumnCount()){
                                        restoreColumn=temp;
                                }
                                 row=restoreRow;
                                 column=restoreColumn;
                                 break;
                        }
                            if (row == table.getSelectedRow()
                            && column == table.getSelectedColumn())
                            {
                                break;
                            }
                        }
                       table.changeSelection(row, column, false, false);
          
                    }
                };
                return leftAction;
               }
      //*******************************************************************************
                     private int[] getRowAndColumnForLeftNavigation(int row,int column,int rowCount,int columnCount){
                             int restoredColumn=column;
                             column -= 1;
          
                          if (column == 0 )
                          {
                              column =2;
                              row -=1;
                          }
          
                          if (row == rowCount)
                          {
                              row = row-1;
                          }
                          if(column==0 || column <0){
                              if(getComponentOrientation()==ComponentOrientation.LEFT_TO_RIGHT){
                                 column=restoredColumn-1;
                              }else{
                                      column=restoredColumn+1;
                              }
                          }
                    return new int[] {row,column};
                     }
      // *******************************************************************************
                     private int[] getRowAndColumnForRigthNavigation(int row,int column,int rowCount,int columnCount){
                            column += 1;
          
                         if (column == columnCount || column==0)
                         {
                             column = 0;
                             row +=1;
                         }
          
                         if (row == rowCount)
                         {
                             row = row-1;
                         }
          
                        return new int[]{row,column};
                }
                //*******************************************************************************
               private boolean actionIsInvalid(JTable table){
                      if(table==null)return true;
                      int selectedColumn,selectedRow;
                      selectedRow=table.getSelectedRow();
                      selectedColumn=table.getSelectedColumn();
                      if(selectedRow<0||selectedRow>=getRowCount())
                              return true;
                      if(selectedColumn<0||selectedColumn>=getColumnCount())
                              return true;
                      return false;
                  }
      //*******************************************************************************
                public void setComponentOrientation(ComponentOrientation o) {
                        super.setComponentOrientation(o);
                        initActionManagement();
                }
      //*******************************************************************************
          
          
      }

      Release Regression From : 1.4.2
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

      Release Regression From : 1.4.2
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

            shickeysunw Shannon Hickey (Inactive)
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: