Hello all,
I have a JTable that has a custom TableModel. I want to update the tables' header values after initialization. However, I'm not sure how to do that with the custom renderer. Here are the two classes that demonstrate what I'm talking about:
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.TableColumn;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.Box;
import java.awt.Dimension;
public class TestTableWindow {
JFrame _frame;
JTable _table;
TestTableModel _tableModel;
public TestTableWindow(){
//create the top level container
_frame = new JFrame("Test Table");
Container pane = _frame.getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS));
_table = new JTable(1, 8);
_table.setFillsViewportHeight(true);
_tableModel = new TestTableModel();
((JLabel) _table.getTableHeader().getDefaultRenderer())
.setPreferredSize(new Dimension(0, 25));
_table.setRowHeight(25);
_table.getTableHeader().setReorderingAllowed(false);
_table.getTableHeader().setResizingAllowed(false);
_table.setRowSelectionAllowed(false);
JScrollPane scrollPane = new JScrollPane(_table, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS));
tablePanel.add(scrollPane);
tablePanel.add(Box.createVerticalGlue());
pane.add(tablePanel);
//the table model should be set before the header values are updated
_table.setModel(_tableModel);
String[] names = {"cats", "dogs", "kittens", "puppies", "birds", "fish", "snails", "lizards"};
setNames(names);
//but the updating only seems to work if it comes before setting the model
//_table.setModel(_tableModel);
pane.setPreferredSize(new Dimension(500, 50));
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.pack();
_frame.setResizable(false);
_frame.setVisible(true);
}
public void setNames(String [] names){
_tableModel.setColumnNames(names);
_table.getTableHeader().repaint();
}
public static void main(String [] args){
new TestTableWindow();
}
}
And here is the custom TableModel. It doesn't do much now, but I do need it in the "real" program.
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
public class TestTableModel extends AbstractTableModel {
private int _columnCount = 8;
String[] _tableData = new String[_columnCount];
String[] _headerValues = new String[_columnCount];
public int getColumnCount() {
return _columnCount;
}
public int getRowCount() {
return 1;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(rowIndex == 1){
if(columnIndex >= 0 && columnIndex < _columnCount){
return _tableData[columnIndex];
}
}
return null;
}
public String getColumnName(int column){
if(column >= 0 && column < _columnCount){
return _headerValues[column];
}
return null;
}
public void setColumnNames(String[] names){
_headerValues = names;
}
}
Does anybody see what I'm doing wrong with updating the header values? Every example I can find on the web does basically this same thing, what's off about mine? The header values do get set just fine, as long as they are set before the model is set to the table. However, since my goal is to update the headers after everything has been initialized, that's not going to work. I appreciate any help you can offer!