java - JTable 不显示列标题

标签 java swing jtable jframe miglayout

这是我第一次使用 GUI,所以我不确定是什么导致了这个问题。我有来自大学的作业。该项目是为各种目的制作一种“产品管理”程序。除了 GUI 之外,整个事情都完成了,这就是我不明白为什么这个 JTable 不显示列标题的地方。这是代码(顺便说一句,使用 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;
    }
}

最佳答案

将表添加到 JScrollPane

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.

关于java - JTable 不显示列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9769396/

相关文章:

java - 比较 util Date 的时差(以小时为单位)

java - Swing 中的 JTable 没有表头

java - 无法在 HTC P3400i 上打开与 J2ME 应用程序的套接字连接

java - 从编辑文本框Android中删除默认文本

java - 重写 setPreferredSize() 和 getPreferredSize()

java - 分享我的 Swing 应用程序中的一个公共(public)变量

java - JButton 操作不在 JTable 内部执行

java - 包裹在 JPanel 内的 JScrollPane 内的 JTable 无法捕获 KeyEvent

java - 使用什么来代替简单的 JdbcTestUtils?

java - 如何将默认表模型与表头同步?