Let’s say you have a JTable component and a save JButton in a form. When you make changes to the data in the JTable, the cell editors would have to save the new changes before you will be able to get the new values. One thing you probably did was to click on some other cell so that the previous cell would lose focus and the new changes will take effect.

But what if you want to click on the save JButton in order to save all the data in the JTable? It is ugly to have to click on some other cell so that changes will take effect and then clicking on the save JButton.

There is a way to go around this and it only involves one line of code.

Place that line of code inside your JButton‘s actionPerformed() method before doing any save. That way, you will be able to get the new values in your JTable.

I am guessing this is quite a common problem that programmers encounter when they have JTextField objects inside a JTable. When you click on a cell, it would not select all the text in it. You have to double click it first. Even the DefaultCellEditor’s setClickCountToStart(1) method does not help. The workaround for this is to override the shouldSelectCell() method.

Related Posts Plugin for WordPress, Blogger...