Hiding all column headers of a JTable is pretty easy. You just need to set to null the method setTableHeader(null).

A very common problem is that once you call this method, nothing happens. The column headers still get displayed. While other workarounds involve setting column header labels to blank, you would still need column names to get access to the column object through their column names (I prefer this way compared to accessing it via a column’s index number directly).

The JTable method setTableHeader() when set to null is only good if there is no JScrollPane on it. Chances are there is one attached to your JTable object because it is needed when you have many rows to consider. You need to call a second method from the JScrollPane in order for the column headers to be hidden. Call the setColumnHeaderView() method and set it to null.

That should do the trick.

By default, pressing the TAB key in a JTextArea will insert a tab character instead of transferring focus to the next focusable component. But if your JTextArea is a cell editor within a JTable, and you want the TAB key to transfer to the next cell on an on, what I did was add a KeyListener to the JTextArea and capture the TAB key event when pressed, then calling the JTable changeSelection() method to transfer focus to the next cell.

If the focus was on the last column of a row, it will check if there is another row next to it. If there is, focus will be transferred to the next row on the first column and so on and so forth. Here is the source code for my TextAreaCellEditor class.

If you want the TextAreaCellEditor class to implement trapping the TAB key event, just supply a boolean parameter true to the constructor.

Focus traversal from cell to cell in a JTable seems to be a very interesting topic. I scoured the internet looking for a solution and it was not easy. People suggested about focus traversal among others which did not work. One person in a forum mentioned overriding the changeSelection method of the JTable that made it work.

That’s it. Whenever you press the TAB key if the JTable has focus, the focus will transfer to the next cell. If your cell has an editor associated to it and if the editor returned is a container like a JPanel, you can modify code in your cell editor class and add a FocusListener to the panel object while adding code in the focusGained() method and call requestFocusInWindow() to the Swing component that you want to receive focus.

Related Posts Plugin for WordPress, Blogger...