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

Local problem of DefaultCellEditor in combination with JFormattedTextField

XMLWordPrintable

      FULL PRODUCT VERSION :
      Microsoft Windows XP [Version 5.1.2600]
      (C) Copyright 1985-2001 Microsoft Corp.

      C:\>java -version
      java version "1.6.0_07"
      Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
      Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows with German Locale

      A DESCRIPTION OF THE PROBLEM :
      The problem/bug is in class javax.swing.DefaultCellEditor in constructor:

          public DefaultCellEditor(final JTextField textField) {
              editorComponent = textField;
      this.clickCountToStart = 2;
              delegate = new EditorDelegate() {
                  public void setValue(Object value) {
      textField.setText((value != null) ? value.toString() : "");
                  }

      public Object getCellEditorValue() {
      return textField.getText();
      }
              };
      textField.addActionListener(delegate);
          }

      when using this constructor with a JFormattedTextField, the line:
      textField.setText((value != null) ? value.toString() : "");

      Sets the text to the JFormattedTextField without respect of the Locale. This is a problem for example when using Double values with decimal seperators other than '.'



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - start vm with german locale where decimal seperator symbol is ','
      - create table with column to edit double values in table by using DefaultCellEditor in combination with new JFormattedTextField(new DecimalFormat("0.000"))

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When trying to edit value in table cell, the value should be displayed in the locale of the vm argument or the default local of the operating system. For German Local With ',' as decimal seperator the value should be displayed in JFormattedTextField component like 1,234
      ACTUAL -
      The value is displayed in JFormattedTextField as 1.234

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      Derive class from DefaultCellEditor and provide an additional constructor

      import javax.swing.JCheckBox;
      import javax.swing.JComboBox;
      import javax.swing.JFormattedTextField;
      import javax.swing.JTextField;

      /**
       * DefaultCellEditor that handles correctly JFormattedTextFields
       *
       *
       * @version $Revision: 16095 $
       * @author haasbern 18.09.2008
       *
       * <p>
       * Copyright © 2008 Biocrates AG, All rights reserved.
       * </p>
       */
      @SuppressWarnings("serial")
      public class DefaultCellEditor extends javax.swing.DefaultCellEditor {

      /**
      * @param checkBox
      */
      public DefaultCellEditor(final JCheckBox checkBox) {
      super(checkBox);
      }

      /**
      * @param comboBox
      */
      public DefaultCellEditor(final JComboBox comboBox) {
      super(comboBox);
      }

          /**
           * Constructs a <code>DefaultCellEditor</code> that uses a text field.
           *
           * @param textField a <code>JTextField</code> object
           */
          public DefaultCellEditor(final JTextField textField) {
           super(textField);
          }

          /**
           * Constructs a <code>DefaultCellEditor</code> that uses a text field.
           *
           * @param textField a <code>JTextField</code> object
           */
      public DefaultCellEditor(final JFormattedTextField textField) {
           super(textField);
           // remove old delegate
           textField.removeActionListener(delegate);
           // create new delegate
              delegate = new EditorDelegate() {
                  public void setValue(Object value) {
                   ((JFormattedTextField)textField).setValue(value);
                  }

      public Object getCellEditorValue() {
      return ((JFormattedTextField)textField).getValue();
      }
              };
              // add new delegate as action listener
              textField.addActionListener(delegate);
          }

      }

            Unassigned Unassigned
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: