Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a cell in JTable in Editing Mode by default  RSS feed

 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a JTable of 5 columns. 3rd column of my table is alone editable. We have a validation logic for the value entered at the 3rd column. I have put that validation logic in setValueAt() method of my table model.

Once the validation fails(say user was editing 2nd ROW and 3rd COLUMN), I show a warning popup that has an OK button. Till this part it all works fine. Once the user clicks OK button in the warning,The cell which user was editing before (2,3) should be in the editing mode itself and should have the last value that user was editing.

In that setValueAt(Object value, int row, int column) method, I tried
MyTable.editCellAt(row, column). But this inturn calls the setValueAt() again and it end up in a recursive loop.

Any idea on how to go about in doing this?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside the setValueAt() of your model write the validation code. If validation fails, do nothing. If it is successful call super.setValueAt
 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am already doing it. Let me tell two specific problems that I face
1. When the validation fails for (2,3) cell, i am showing a popup. after clicking OK in that popup the user control should go back to (2,3) cell.
2. When the control is taken back to (2,3) cell. The cell should be in the editing mode.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cant say without looking at the code for setValueAt()
 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 2) {
Object oldQnty = getValueAt(rowIndex, columnIndex);
Object oldSCash = getValueAt(rowIndex, 4);
Object oldECash = getValueAt(rowIndex, 5);
double newSCash;
double newECash;
Integer newValue = Integer.valueOf(aValue.toString());
super.setValueAt(newValue, rowIndex, columnIndex);
boolean noOfExceptions;
noOfExceptions = isNoOfExceptions();
if (noOfExceptions) {
// Shows a JOPTIONPANE
}
super.setValueAt(oldQnty, rowIndex, columnIndex);
}
}

Here is the setValueAt(), the problem is not with displaying the new value. The cell (rowIndex, columnIndex) should be in the EDIT MODE
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags in future so that the code is readable.

I dont see any "return" statement after you show the JOptionPane.showXXXDialog.
So the code continues and calls the setValueAt method.
 
Karthick Dharani Vidhya
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even after inserting the return statement it doesnot work. Cell shows the new value but that particular cell is not in editing mode.
 
Cody Westlund
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't see any stickies about resurrecting 7 year old threads so here it goes. I too am trying to get the cell that had invalid input typed in to be in edit mode after the error dialog is closed.
 
Campbell Ritchie
Marshal
Posts: 56195
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can answer or reopen threads of any age. It would be even better if I knew the answer. I don't suppose there is anything useful in the Java® Tutorials, is there?

And welcome to the Ranch
 
Rob Camick
Ranch Foreman
Posts: 2818
15
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The model should not know anything about the state of the table. That is the model is not responsible to putting the table in edit mode.

A JTable editor already does basic edits to make sure you enter data of the proper type into the editor. If you want further editing then you should create a custom editor.

Here is an example that makes sure the text entered is exactly 5 characters:


 
Cody Westlund
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob thanks for taking the time to reply with a working solution for me. Having looked at your code I have now fixed mine. I had to create a new editor that extended DefaultCellEditor and I overrode stopCellEditing() to call textField.selectAll() and textField.requestFocusInWindow() (as you had done). That was enough and works like a charm!
 
Cody Westlund
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I could piggy-back off of this thread I have a related question. My dialog has an OK and Cancel button, so on a Cancel I would rather not have the "super.stopCellEditing()" call be done. This is causing the invalid entry to be validated on the close which in turn causes the error message to show up. How I can get a handle on what was clicked, and if it's the Cancel button to then not call 'super.stopCellEditing'?

 
Rob Camick
Ranch Foreman
Posts: 2818
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My dialog has an OK and Cancel button, so on a Cancel I would rather not have the "super.stopCellEditing()" call be done.


Normally the editor is NOT stopped when you click on a button (or other component), which typically is a problem. See Table Stop Editing for more information on this topic.

I added a button to the example code I posted and I don't have the problem you described, so I don't know how your code is different.

If you still have a problem, then create a proper SSCCE demonstrating the problem and ask a new question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!