我是java新手,想在jtable中设置表头,我已经采取了数组,但无法设置头,请指导我。我试过设置通过采取实例的JTable,但没有帮助。也试过table.setmodel。由于
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class XmlGui extends JFrame {
public XmlGui() {
}
public static final String[] colNames = { " Name ", " Type ", " Value " };
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XmlBack xmlBack = new XmlBack();
saxParser.parse(new File(
"File path"), xmlBack);
new XmlGui().drawFrame(xmlBack.varGroupVariables);
}
public void drawFrame(List<VarGroupVariable> varGroupVariables) {
setTitle("The Xml Parser");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(800, 500);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jplInnerPanel1 = createInnerPanel();
jplInnerPanel1.setLayout(new BorderLayout());
//Vector<String> columnNames = new Vector<String>();
JTable table = new JTable();
table.setModel(new VarGroupVariableModel(varGroupVariables));
JScrollPane tableContainer = new JScrollPane(table);
jplInnerPanel1.add(tableContainer, BorderLayout.CENTER);
tabbedPane.addTab(" TABBEDPANE DEMO ", jplInnerPanel1);
getContentPane().add(tabbedPane);
setVisible(true);
}
protected JPanel createInnerPanel() {
JPanel jplPanel = new JPanel();
JLabel jlbDisplay = new JLabel();
jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
jplPanel.setLayout(new GridLayout(1, 1));
jplPanel.add(jlbDisplay);
return jplPanel;
}
}
public static final String[] colNames = { " Name ", " Type ", " Value " };
你不能只定义一个包含列名的变量。
您的VarGroupVariableModel
必须正确实现getColumnNames()方法才能显示表头名称。所以上面的代码需要成为你的模型的一部分。
请参阅Swing教程关于如何使用表的工作示例和更多信息。