开发者

JTable without a header

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?


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:

JTable without a header


The other way is to call

table.setTableHeader(null);


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

jTable.getTableHeader().setUI(null);

This is tested with Java 8.


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

JTable without a header

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


Dummies like me: do this :]

jTable.setTableHeader(null);


Try These Steps:

JtableProperties > TableHeader > CustomCode > Null


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


  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜