简体   繁体   English

JTable没有显示列标题

[英]JTable not showing column titles

This is my first time working with GUI, so I'm not exactly sure what is causing the problem. 这是我第一次使用GUI,所以我不确定导致问题的原因。 I have an assignment from my Uni. 我的Uni任务。 The project is to make a sort of "Product management" program for various purposes. 该项目旨在为各种目的制定一种“产品管理”计划。 The whole thing is done except the GUI, and this is where I just don't get it why this JTable won't display column titles. 除了GUI之外,整个过程都已完成,这就是我不知道为什么这个JTable不会显示列标题的原因。 Here's the code (btw, using MIGLayout) 这是代码(顺便说一句,使用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;
    }
}

Add the table to a JScrollPane 将表添加到JScrollPane

The docs for JTable state: JTable状态的文档:

.. Note that if you wish to use a JTable in a standalone view (outside of a JScrollPane ) and want the header displayed, you can get it using getTableHeader() and display it separately. ..请注意,如果您希望在独立视图中使用JTable (在JScrollPane之外)并希望显示标题,您可以使用getTableHeader()获取它并单独显示它。

At first add your table to a JScrollPane: 首先将表添加到JScrollPane:

JScrollPane scrollpane = new JScrollPane(table);

then add the scrollpane to your layout : 然后将滚动窗格添加到您的布局:

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

Secondly, add all your components to the frame and make it visible at the end of your method: 其次,将所有组件添加到框架中,并在方法结束时将其显示为:

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

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

For miglayout I use this: 对于miglayout我使用这个:

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? 要删除垂直间隙,请阅读如何删除MigLayout中两个单元格之间的垂直间隙?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM