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

editCellAt fails to get focus for the cell to be edited.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.1.7
    • client-libs



      Name: dbT83986 Date: 03/25/99


      The first time I call editCellAt(row, column) in a JTable, there is no indication on screen that the cell editor has been invoked.
      If I begin typing the editor then shows itself but if I click on the cell instead, editing is immediately stopped. When I have finished
      editing and press return, nothing happens. Only after I have clicked in the editor, and the caret has appeared there, does the
      return key work to stop editing.

      The second and subsequent calls to editCellAt(row, column) result in the editor being visible but it does not appear to get the
      focus. As above, no caret appears. If I begin typing characters are accepted but return still doesn't stop editing until I click in
      the text and get the caret to appear there.

      Notes:
      I am using a custom cell renderer in the table.
      I have called setCellSelectionEnabled(true) for this table.
      This bug is not fixed as claimed in 4139059 and 4133261.

      ==============================
      REVIEW NOTE 3/25/99 - Relevant information from user on incident 54069

      Please find attached an example which reproduces this problem (and the one I
      reported with review ID: 54015 but which has not yet been addressed). To
      reproduce the problem(s) compile and run TableTest.java. This will bring up
      a window listing the contents of your home directory. Note that any changes
      you make will not affect the contents of your filing system - nothing is
      written back to disk. Adjust the width of the first column of the table so
      that some of the items displayed there are truncated (have ellipsis). Click
      on a cell in another column to select a row in the table and press F2. This
      should initiate cell editing on the item in the first column but that cell
      doesn't get the focus so there is no way to edit without risking loss of
      data. Press return to complete the invisible edit. Now press F2 after
      clicking on a cell in the first column. This should work as intended. Now
      repeat the first test (clicking on a cell in another column). The cell
      editor will appear in the first column but the repaint will be broken.

      I am running Swing 1.1 release with JDK 1.1.7 on Windows NT SP4.

      Please write back if you need further details.


      import java.awt.*;
      import java.awt.event.*;
      import java.io.File;
      import java.text.DateFormat;
      import java.util.Date;

      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.filechooser.*;
      import javax.swing.table.*;

      public class TableTest extends JFrame
      {
      public static void main(String args[])
      {
      TableTest test = new TableTest();
      test.addWindowListener(new WindowAdapter()
      {
      public void windowClosed(final WindowEvent event)
      {
      System.exit(0);
      }
      });
      test.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      test.show();
      test.setDirectory(System.getProperty("user.home"));
      test.setSize(400, 300);
      }

      private String[] contents;
      private String directory;
      private JTable table;
      private JScrollPane scrollPane;

      public TableTest()
      {
      this.table = new JTable(new TableTestModel(this));
      this.table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);//NEXT_COLUMN);//_OFF);
      this.table.setShowGrid(false);
      this.table.registerKeyboardAction(
      new AbstractAction()
      {
      public void actionPerformed(final ActionEvent event)
      {
      editSelection();
      }
      },
      KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0),
      JComponent.WHEN_FOCUSED
      );

      setBackground(this.table.getBackground());

      this.scrollPane = new JScrollPane(this.table);
      getContentPane().add(this.scrollPane, BorderLayout.CENTER);
      }

      public void setDirectory(final String directory)
      {
      if (this.table.isEditing())
      {
      this.table.getCellEditor().cancelCellEditing();
      }
      this.directory = directory;
      this.contents = new File(this.directory).list();
      ((AbstractTableModel)this.table.getModel()).fireTableStructureChanged();
      // this.table.sizeColumnsToFit(-1);
      }

      public String getDirectory()
      {
      return this.directory;
      }

      public String[] getContents()
      {
      return this.contents;
      }

      public void editSelection()
      {
      if (getDirectory() != null && this.table.getSelectedRowCount() == 1)
      {
      int row = this.table.getSelectedRow();
      TableTestModel model =
      (TableTestModel)this.table.getModel();
      model.setEditableRow(row);
      if (this.table.editCellAt(row, 0))
      {
      this.table.getEditorComponent().requestFocus();
      this.table.getCellEditor().addCellEditorListener(model);
      }
      else
      {
      System.out.println("editCellAt() returned false");
      }
      }
      }
      }

      class TableTestModel
      extends AbstractTableModel
      implements CellEditorListener
      {
      private static final String[] columnName =
      {
      "Name", "Access", "Modified", "Size"
      };

      private static final Class[] columnClass =
      {
      String.class, String.class, String.class, Long.class
      };

      //////////////////////////////////////////////////////////////////

      private int editableRow = -1;
      private final int editableColumn = 0;

      private TableTest viewer;

      private final static DateFormat dateFormat =
      DateFormat.getDateTimeInstance();

      public TableTestModel(final TableTest viewer)
      {
      this.viewer = viewer;
      }

      public Class getColumnClass(final int columnIndex)
      {
      return columnClass[columnIndex];
      }

      public int getColumnCount()
      {
      return columnName.length;
      }

      public String getColumnName(final int columnIndex)
      {
      return columnName[columnIndex];
      }

      public int getRowCount()
      {
      String[] entries = viewer.getContents();
      return entries != null ? entries.length : 0;
      }

      public Object getValueAt(final int rowIndex, final int columnIndex)
      {
      Object value = null;

      String[] entries = viewer.getContents();

      if (entries != null)
      {
      File entry = new File(viewer.getDirectory(), entries[rowIndex]);

      if (viewer.getDirectory() != null)
      {
      switch (columnIndex)
      {
      case 0:
      //"Name"
      value = entry.getName();//new IconText(fileView.getIcon(entry), entry.getName());
      break;

      case 1:
      // "Access"
      StringBuffer mode = new StringBuffer();
      if (entry.canRead())
      {
      mode.append('R');
      }
      else
      {
      mode.append(' ');
      }
      if (entry.canWrite())
      {
      mode.append('W');
      }
      value = mode.toString();
      break;

      case 2:
      // "Modified"
      value = this.dateFormat.format(
      new Date(entry.lastModified())
      );
      break;

      case 3:
      // "Size"
      if (!entry.isDirectory())
      {
      value = new Long(entry.length());
      }
      break;

      default:
      break;
      }
      }
      }
      return value;
      }

      public void setValueAt(
      final Object value, final int rowIndex, final int columnIndex
      )
      {
      this.editableRow = -1;
      }

      public void setEditableRow(final int row)
      {
      this.editableRow = row;
      }

      public boolean isCellEditable(final int row, final int column)
      {
      return row == this.editableRow && column == this.editableColumn;
      }

      public void editingCanceled(final ChangeEvent event)
      {
      this.editableRow = -1;
      ((TableCellEditor)event.getSource()).removeCellEditorListener(this);
      }

      public void editingStopped(final ChangeEvent event)
      {
      ((TableCellEditor)event.getSource()).removeCellEditorListener(this);
      }
      }
      (Review ID: 54015)
      ======================================================================

            pmilnesunw Philip Milne (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: