Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About KeyEvent?

 
Richard Phen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about KeyEvent: I add a KeyListener to a JTable. To prohibit user modifing the data in JTable, I attempt to capture a KeyEvent and pop up a warning message when an user types keyboard to modify some data in JTable. I hope whatever user type which key the warning message would pop up and stop the modifing. How to do that???
My code was show bellow:
 
Abhinav Anand
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Richard,
Try to find out some posts on AbstractTableModel. I doubt if you are using it. You can define your JTable using the constructor.
JTable table = new JTable(new MyTableModel());
where MyTableModel class extends AbstractTableModel. Define a method named
public boolean isEditable(int row, int col) {
return false;
}
in the MyTableModel class. This will prevent the user from modifying your JTable.
Hope it helps
 
Richard Phen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vishal Sinha,
Thank you for your quick and clear reply! Yes, Your answer is very valuable to me!
I modified my code and achieved the goal.
class MyTableModel extends DefaultTableModel
{
......
public boolean isCellEditable(int row, int column)
{
return false;
}
}
Thank you again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic