Java 没有标题的 JTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:51:39  来源:igfitidea点击:

JTable without a header

javaswingjtable

提问by saurabh

Can anyone tell me how to create a table without a table header?

谁能告诉我如何创建没有表头的表?

I am making Sudoku puzzle and I want to create a table without the table header in Java. Is it possible?

我正在制作数独谜题,我想在 Java 中创建一个没有表头的表。是否可以?

回答by Peter Lang

I don't think that you want your Sudoku to scroll, so the easiest way should be notto put your table into a JScrollPane, which is responsible for displaying the header:

我不认为您希望您的数独滚动,因此最简单的方法应该是不要将您的表格放入 a JScrollPane,它负责显示标题:

alt text

替代文字



The other way is to call

另一种方式是调用

table.setTableHeader(null);

回答by Ray Hulha

So the trick seems to be to first set a header using the DefaultTableModel constructor but then later to call setTableHeader(null);

所以诀窍似乎是首先使用 DefaultTableModel 构造函数设置标题,然后再调用 setTableHeader(null);

enter image description here

在此处输入图片说明

Here is some demo code:

这是一些演示代码:

package net.raysforge.visual.demo;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableAndGBC {

    private String[] columnNames = {"Source", "Hit", "Last", "Ur_Diff"};
    private JTable table;
    private Object[][] data = {{"Swing Timer", 2.99, 5, 1.01},
        {"Swing Worker", 7.10, 5, 1.010}, {"TableModelListener", 25.05, 5, 1.01}};
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);

    public JTableAndGBC() {
        JPanel panel = new JPanel(new GridBagLayout());
        table = new JTable(model);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        JScrollPane pane = new JScrollPane(table);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setTableHeader(null);
        panel.add(pane, gbc);
        JFrame frame = new JFrame();
        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) throws Exception {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableAndGBC();
            }
        });
    }
}

回答by BudwiseЯ

Anyone having problems with the suggested methods, try the following:

任何对建议的方法有问题的人,请尝试以下操作:

jTable.getTableHeader().setUI(null);

This is tested with Java 8.

这是用 Java 8 测试的。

回答by tharinda

Try These Steps:

尝试这些步骤

JtableProperties > TableHeader > CustomCode > Null

JtableProperties > TableHeader > CustomCode > Null

回答by MoolsBytheway

Dummies like me: do this :]

像我这样的傻瓜:这样做:]

jTable.setTableHeader(null);