I have using following code over jdk 6.0 but after first round it doesnt work ....

if(table.getRowCount() -1 == table.getSelectedRow())
table.editCellValues(0,columnno);
else 
table.editCellValues(table.getSelectedRow(),columnno);

These lines are in mouse clicked method... Problem is when it reaches to end, and when fw button is pressed it jumps to another column.... Any Suggestions;.....

Dani AI

Generated

Short diagnosis: the odd jump and the exception you saw are classic re‑entrancy problems. Calling editCellAt from a TableModelListener (or trying to control editing from a KeyListener) runs into model-update callbacks while the editor is still committing — that fires more TableModelEvents and can change selection/focus unexpectedly. was right to check syntax, and was right to ask for a runnable example — the fix is a change of approach rather than more conditionals.

Recommended pattern (reliable and simple)

  • Commit the current editor (stopCellEditing()), then schedule the move-to-next-cell logic using SwingUtilities.invokeLater(...). Deferring avoids re-entrant TableModel events.
  • Do the selection change and start editing the target cell inside that deferred Runnable; then explicitly request focus on the editor component.
  • Prefer a CellEditorListener (listen for editingStopped) or bind Enter via the table InputMap/ActionMap instead of a TableModelListener.

Example (attach a CellEditorListener to your editor type)

DefaultCellEditor stringEd = (DefaultCellEditor)table.getDefaultEditor(String.class);
stringEd.addCellEditorListener(new CellEditorListener() {
  public void editingStopped(ChangeEvent e) {
    final int row = table.getSelectedRow();
    final int rows = table.getRowCount();
    final int nextRow = (row == rows - 1) ? 0 : row + 1; // wrap if desired
    final int nextCol = 0; // choose target column

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        table.changeSelection(nextRow, nextCol, false, false);
        if (table.editCellAt(nextRow, nextCol)) {
          Component ed = table.getEditorComponent();
          if (ed != null) ed.requestFocusInWindow();
        }
      }
    });
  }
  public void editingCanceled(ChangeEvent e) {}
});

Notes: add the listener to each editor type you use (String/default, combo, etc.). Remove the TableModelListener that calls editCellAt — it’s the source of the loop/column-jump. If problems persist when wrapping, check whether the editor component itself consumes Enter (bind Enter at the table/editor level to ensure consistent behavior).

Recommended Answers

All 9 Replies

Try something like this

if((table.getRowCount() - 1) == table.getSelectedRow())

Sorry, i forgot to mention the brackets.... Still the same problem....

Could you provide us with a little more info? What do you want to do? Show us some more code

Well i want to edit a cell and when a user press enter next cell automatically selected in edit mode.... Its just a name entering form , when data is entered in one cell cursor moves to next.

there any Bug with TableCellEditor, some your mistake, post a runnable code that demonstrated your issue, because your code works in my hands as I expected

if(table.getRowCount() >0)
if(table.getSelectedRow() == (table.getRowCount() - 1))	
table.editCellAt(0,3);
else
table.editCellAt(table.getSelectedRow()+1,3);

Well its the code in public void keyReleased(KeyEvent ke)

And its not working, i will post a video to make it more clear... Problem is it select next cell for edit but when it reaches to end it select the first row and after VK_ENTER it move from column 3 to 4, Thats my problem...

I just found table model listener.... And it solved a great deal of my problem. But Still i cant select a new cell for updation, each cellEditAt call tablemodelevent function call which iterate and generate a error... Any Help Regarding to this...

if(table.getSelectedColumn() == 3 && table.getSelectedRow() != -1 && tme.getType() == TableModelEvent.UPDATE)
table.editCellAt(table.getSelectedRow() + 1, 0);			{

Sorry, but i thought no answers from here so i posted some where to get my answer....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.