If you use a JComboBox as a cell editor in a JTable and you get this error message “component must be showing on the screen to determine its location”, chances are your cell editor is inherited from DefaultCellEditor. Thomas Bang saved us all the trouble in trying to figure out how to make a workaround for this problem by creating a ComboBoxCellEditor class inheriting from AbstractCellEditor.

Pretty cool. This class actually did the trick.

To hide a JTable column but still retain its column data intact is very easy. Check the line of code below.

This scenario is a good and quick hack if you want your table entries to have a reference to its id but you do not want the id’s column to be displayed. To access the column that contains the ids, what you need to do is to access the JTable’s table model.

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.

Related Posts Plugin for WordPress, Blogger...