Question

I am newbie in java and wanted to set table header in jtable i have taken array but not able to set header please guide me. I have tried setting through taking instance of JTable but didnt help. also tried table.setmodel. thanks

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

}
Was it helpful?

Solution

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

You can't just define a variable containing the column names.

Your VarGroupVariableModel must implement the getColumnNames() method correctly for the table header names to appear. So the above code somehow needs to be a part of your model.

See the Swing tutorial on How to Use Tables for a working example and more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top