Hide JTable Column Headers
Posted by tech on
March 1, 2010
|
|
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.
Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.tags: column, header, hide, jtable
No Comments
How To Hide JTable Column
Posted by tech on
January 19, 2010
|
|
To hide a JTable column but still retain its column data intact is very easy. Check the line of code below.
1 | jTable1.removeColumn(jTable1.getColumnModel().getColumn(COLUMN_INT_INDEX)); |
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.
1 | jTable1.getModel().getValueAt(jTable1.getSelectedRow(), COLUMN_INT_INDEX); |
tags: column, jtable
No Comments
Auto Resize JTable Columns
Posted by tech on
November 15, 2009
|
|
This class auto resizes columns of a JTable, maximizing any non-used width columns and transferring those extra widths to other columns. This way, a column with a JCheckBox will have a width almost equal to that of a JCheckBox’s width. The class TableColumnResizer contains one static method and you can pass the JTable object as the parameter. The code does the rest.
To use the method, do this:
1 | TableColumnResizer.adjustColumnPreferredWidths(myJTableObject); |
Here is the code of the TableColumnResizer class.
1 2 3 4 5 6 7 8 9 10 | public class TableColumnResizer { public static void adjustColumnPreferredWidths(JTable table) { if (table == null || table.getColumnCount() == 0) return; // strategy - get max width for cells in column and // make that the preferred width TableColumnModel columnModel = table.getColumnModel(); for (int col=0; col |

/rating_on.png)
/rating_half.png)
(3 votes, average: 3.67 out of 5)





