没有标题的 JTable

发布于 2024-08-26 09:13:26 字数 68 浏览 4 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白日梦 2024-09-02 09:13:26

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

alt text


另一种方法是调用

table.setTableHeader(null);

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

alt text


The other way is to call

table.setTableHeader(null);
深爱不及久伴 2024-09-02 09:13:26

如果有人对建议的方法有疑问,请尝试以下操作:

jTable.getTableHeader().setUI(null);

这是使用 Java 8 进行测试的。

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

jTable.getTableHeader().setUI(null);

This is tested with Java 8.

秋日私语 2024-09-02 09:13:26

因此,技巧似乎是首先使用 DefaultTableModel 构造函数设置标头,然后调用 setTableHeader(null);

输入图片此处描述

这是一些演示代码:

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

So the trick seems to be to first set a header using the DefaultTableModel constructor but then later to call 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();
            }
        });
    }
}
断念 2024-09-02 09:13:26

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

jTable.setTableHeader(null);

Dummies like me: do this :]

jTable.setTableHeader(null);
梦晓ヶ微光ヅ倾城 2024-09-02 09:13:26

尝试以下步骤

JtableProperties >表头>自定义代码>空

Try These Steps:

JtableProperties > TableHeader > CustomCode > Null

甜心 2024-09-02 09:13:26

(if) L&F 更改后,jTable.getTableHeader().setUI(null) 将不起作用。

对我有用的是重置滚动窗格列标题的可见性(在我的情况下分配给变量scrollPaneSummary),以这种方式包装 JTable:

scrollPaneSummary = new javax.swing.JScrollPane();
scrollPaneSummary.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPaneSummary.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPaneSummary.setViewportView(  summaryJtable  );
scrollPaneSummary.setColumnHeaderView( summaryJtable.getTableHeader());

// make JTable headers invisible
scrollPaneSummary.getColumnHeader().setVisible(false);
scrollPaneSummary.revalidate();


  

jTable.getTableHeader().setUI(null) will not work after (if) L&F is changed.

What worked for me was to reset the visibility for the column headers of the scrollpane (in my cases assigned to variable scrollPaneSummary) that wraps the JTable this way:

scrollPaneSummary = new javax.swing.JScrollPane();
scrollPaneSummary.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPaneSummary.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPaneSummary.setViewportView(  summaryJtable  );
scrollPaneSummary.setColumnHeaderView( summaryJtable.getTableHeader());

// make JTable headers invisible
scrollPaneSummary.getColumnHeader().setVisible(false);
scrollPaneSummary.revalidate();


  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文