JTable not displaying column headers

This is my first experience with a GUI, so I'm not entirely sure what is causing the problem. I have an assignment from my Uni. The project should make a kind of "Product Management" program for various purposes. All of this is done except for the GUI and this is where I just don't understand why this JTable

one won't display the column headers. Here's the code (btw using MIGLayout)

package sepm.s2012.e0727422.gui;

import sepm.s2012.e0727422.service.*;

import java.awt.*;
import javax.swing.*;

import net.miginfocom.swing.MigLayout;


public class MainFrame {

    /** Start all services **/
//  IProductService ps = new ProductService();
//  IInvoiceService is = new InvoiceService();
//  IOrderService os = new OrderService();

    /** Frame **/
    private JFrame frame;
    private JTabbedPane tab;

    public static void main(String[] args) {
        new MainFrame();
    }


    public MainFrame() {

        frame = new JFrame("Product control and management system");
        frame.setVisible(true);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /** TABBED PANE options and parameters **/
        tab = new JTabbedPane();
        ImageIcon icon = null; // TODO
        tab.addTab("Products", icon, tabProducts(), "Add/Update/Delete products");
        tab.addTab("Invoices", icon, tabInvoices(), "Overview of invoices");
        tab.addTab("Cart", icon, tabCart(), "Order new products");
        tab.setSelectedIndex(0);

        frame.add(tab, BorderLayout.CENTER);

    }       
    /**
     * Products Panel tab
     * @return panel
     */
    public JPanel tabProducts() {
        JPanel panel = new JPanel(new MigLayout("","20 [] 20", "10 [] 10 [] 10 [] 10"));

        JLabel label = new JLabel("List of all available products");
        JButton add = new JButton("Add product");
        JButton update = new JButton("Update product");
        // Below is a test table, will be replace by products in DB
        String[] tableTitle = new String[] {"ID", "Name", "Type", "Price", "In stock"};
        String[][] tableData = new String[][] {{"1", "Item 1", "Type 1", "0.00", "0"}, {"2", "Item 2", "Type 2", "0.00", "0"},
                                                {"3", "Item 3", "Type 3", "0.00", "0"}, {"4", "Item 4", "Type 4", "0.00", "0"}};
        JTable table = new JTable(tableData, tableTitle);

        panel.add(label, "wrap, span");
        panel.add(table, "wrap, span");
        panel.add(add);
        panel.add(update);

        return panel;
    }

    public JPanel tabInvoices() {
        JPanel panel = new JPanel(new MigLayout());
        return panel;
    }

    public JPanel tabCart() {
        JPanel panel = new JPanel(new MigLayout());
        return panel;
    }
}

      

+3


source to share


3 answers


First add the table to the JScrollPane:

JScrollPane scrollpane = new JScrollPane(table);

      

then add scroll to layout:



panel.add(scrollpane, "wrap, span");

      

Second, add all the components to the frame and make them visible at the end of your method:

//...
frame.add(tab, BorderLayout.CENTER);

frame.validate();
frame.setVisible(true);

      

+4


source


Add the table to JScrollPane

Docs for JTable

state:



.. Note that if you want to use JTable

offline (out of JScrollPane

) view and want the displayed title to appear, you can retrieve it with getTableHeader()

and display it separately.

+8


source


For miglayout, I use this:

JPanel panel = new JPanel(new MigLayout("wrap 1, gapy 0"));
table = CreateParametersTable();
panel.add(table.getTableHeader(), "width 100%");
panel.add(table, "width 100%");

      

For removing vertical gaps read this How to remove vertical gap between two cells in MigLayout?

0


source







All Articles