我是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;
}
}
答案 0 :(得分:2)
public static final String[] colNames = { " Name ", " Type ", " Value " };
您不能只定义包含列名的变量。
您的VarGroupVariableModel
必须正确实现getColumnNames()方法才能显示表头名称。所以上面的代码需要成为模型的一部分。
有关工作示例和更多信息,请参阅How to Use Tables上的Swing教程。