Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about JTable

 
Adithya Rayaprolu
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When booking flight operation is successful, I would like to change the number of available seats in the JTable. How can I do it? I am using a table model which extends from AbstractTableModel. If I use setValueAt method, it is not getting reflected in the table.
Thanks for any suggestion.
 
Adithya Rayaprolu
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another doubt regarding JTextField. I want user to input only digits in a text field. I wrote the following code in keyTyped event handler:
if ( char < '0' | | char > '9' )
event.consume();
when I press Backspace, the digits are not getting erased. can somebody help me?
Thanks in advance.
 
Ravikiran Choppalli
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your First Question, I too had some difficulty, so I changed my implementation to Default Table Model. It was quite easy using that. Just needs to update the vector using setDataVector, and then call fireTableDataChanged() method.
For the second question, I feel that the BackSpace keyevent is also getting consumed since it does not fall between '0' and '9'. May be you should not let backspace event to get consumed.
Thanks,
Ravi.
 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I extended default table model and in the setValueAt(object, int, int) I change the appropiate cell and call fireTableCellUpdated(row, col). This is more effecient than updating the whole table for one column change.
For the Second problem why don't you create your own Document from PlainDocument and override the insertString method. Here you can test the character pressed to see if it is a number or not. I had problems doing the same as you with the KeyEvent listeners, but this solution worked fine for me
Trevor
 
Devu Shah
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adithya Rayaprolu:
I have another doubt regarding JTextField. I want user to input only digits in a text field. I wrote the following code in keyTyped event handler:
if ( char < '0' | | char > '9' )
event.consume();
when I press Backspace, the digits are not getting erased. can somebody help me?
Thanks in advance.

I have employed the following strategy for the above.I have used the Integer.parseInt(txtfeild.getText()) method to get the no of seats. and if user inputs either characters or leaves it empty, then the above method throws the "NumberFormatException"
which i catch and there i give the user the message to enter only integer values.and then i have made the textfeild value
as textfeild.settext("");and then txtfeild.grabFocus so that
focus remains on txtfeild.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic