1

我是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;
    }

}
4

1 回答 1

2
public static final String[] colNames = { " Name ", " Type ", " Value " };

您不能只定义包含列名的变量。

VarGroupVariableModel必须正确实现 getColumnNames() 方法才能显示表头名称。所以上面的代码不知何故需要成为你模型的一部分。

有关工作示例和更多信息,请参阅有关如何使用表格的 Swing 教程。

于 2013-05-27T16:34:39.247 回答