setting a JTable header

I posted this question as apart of another question but in the post script, so people aren't really addressing it, but...

Is there a way to set the header of a Jtable? I see a lot of implementations with setting a tablemodel class but all of those implementations require actual data in the table as well. I have no data in it quite yet, that will be implemented in real-time, but I want to set the header to be something. How can this be done? I was thinking about setting null as the data parameter of the TableModel, but that felt really hacky (not even sure if it would work). Does anybody know how to set the header in the JTable?

[649 byte] By [deadseasquirrelsa] at [2007-10-3 8:54:23]
# 1

are you just after an empty table?

import javax.swing.*;

import java.awt.*;

import javax.swing.table.*;

class Testing

{

public void buildGUI()

{

String colNames[] = {"Col 0", "Col 1","Col 2", "Col 3"};

DefaultTableModel dtm = new DefaultTableModel(null,colNames);

dtm.setRowCount(10);

JTable table = new JTable(dtm);

JScrollPane sp = new JScrollPane(table);

sp.setPreferredSize(new Dimension(300,100));

JFrame f = new JFrame();

f.getContentPane().add(sp);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-15 4:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
yeah I was, I guess setting the content of the table to null in the Tablemodel class is the way to go. I always second guess myself, thinking it's a hack. Thanks for the tip.
deadseasquirrelsa at 2007-7-15 4:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
Are your column names already decided?
hiwaa at 2007-7-15 4:04:22 > top of Java-index,Desktop,Core GUI APIs...
# 4
Am I missing something? Here are two constructors for the API. Set the row count to 0DefaultTableModel(Object[] columnNames, int rowCount) DefaultTableModel(Vector columnNames, int rowCount)
camickra at 2007-7-15 4:04:22 > top of Java-index,Desktop,Core GUI APIs...