FULL PRODUCT VERSION :
Happens with jdk1.5.0_7, jdk1.5.0_9, jdk1.5.0_10 and jdk1.6.0-b105.
Didn't try others.
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
EXTRA RELEVANT SYSTEM CONFIGURATION :
Nothing with relevance.
A DESCRIPTION OF THE PROBLEM :
[Most of this bugreport refers to the behaviour in jdk 1.5. Specifics of the behaviour in jdk 1.6 are described at the end of the bug report.]
When setting a new JTableHeader to a JTable, every Property I set on this header is ignored.
For example with the following code snippet:
[code]
this.tblGleise.setTableHeader(new JTableHeader());
this.tblGleise.getTableHeader().setReorderingAllowed(false);
[/code]
the Table Columns can still be reordered. The same goes for any other property, like changing the background or forground color, setting a tooltip, etc...
However, comment out the first line of the code snippet (so that the TableHeader isn't set to a new object) and it works quite well as expected.
I noticed another (not so disturbing) difference. The default font for the TableHeader is a thin shaped font. When setting the JTableHeader with setTableHeader(), the Font is a bold font.
With JDK 6, the behaviour is a bit different. The font difference is there, too. The background color setting is still ignored. But the behaviour of setting reorderingAllowed() is different. The header column itself can be dragged, but it doesn't allow to "drop" the column to a different position. When dragging the header column to an amount that is larger than the width of the column, it jumps back to the original position.
When not setting a new JTableHeader, it behaves like expected, properties like the background color can be changed and when reordering is disallowed, the column headers cannot be moved.
First I thought that this could be Bug #4776303 (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4776303), but since the workaround described there doesn't work, I think these must be two distinctive bugs.
PS: While writing the test case for this bug I noticed another oddity: The above described behaviour happens with netbeans 5.5 generated code. When writing the code by hand, setting the tableHeader with setTableHeader() leads to no visible header, like when I would have called setTableHeader(null).
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
* Create a JFrame or JDialog with a JTable in a ScrollPane.
* Set a new JTableHeader to the JTable
* Set some properties of the JTableHeader, like the bg color or reorderingAllowed
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Correct behaviour would be that the properties get correctly applied.
The Background of the TableHeader should be the new Color and no reordering of the columns should be possible.
(This behaviour can be achieved by not setting the JTableHeader.)
ACTUAL -
No setting to the TableHeader is applied. The background color is still the same and the columns can be reordered (except for the little specialty in JDK 1.6)
However, when asking for the properties with JTable.getTableHeader().getReorderingAllowed(), it tells me the value I gave.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
Netbeans generated code:
[code]
public class T2 extends javax.swing.JFrame {
/** Creates new form T2 */
public T2() {
initComponents();
this.jTable1.setTableHeader(new javax.swing.table.JTableHeader());
this.jTable1.getTableHeader().setBackground(java.awt.Color.RED);
this.jTable1.getTableHeader().setReorderingAllowed(false);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(14, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new T2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
[/code]
Handwritten code.
[code]
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class TableTest extends JFrame{
JTable table= new JTable();
JScrollPane sp= new JScrollPane(table);
public static void main(String[] args){
new TableTest().setVisible(true);
}
public TableTest(){
table.setModel(new CustomTableModel());
table.setTableHeader(new JTableHeader());
table.getTableHeader().setBackground(java.awt.Color.RED);
table.getTableHeader().setReorderingAllowed(false);
this.getContentPane().add(sp);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static class CustomTableModel extends AbstractTableModel{
private String[][] content= new String[][]{{"one", "two", "three"},
{"four", "five", "six"}};
public String getColumnName(final int col){
return String.valueOf("col"+col);
}
public int getRowCount() {
return this.content.length;
}
public int getColumnCount() {
return 3;
}
public Object getValueAt(int row, int col) {
return this.content[row][col];
}
}
}
[/code]
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The only workaround is not setting the JTableHeader.