user3895525
user3895525

Reputation: 1

JTable into JScrollPane, without Header

Why there comes tableHeader automatically when I add a JTable into a JScrollPane. I want to add table into a scrollPane without header. How to do? Pls

Upvotes: 0

Views: 533

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347194

Short answer is, you can't (do it automatically).

What you can do is call JScrollPane#setColumnHeaderView and pass it null after you've set the JTable to the viewport...

The column headers are applied to the JScrollPane by the JTable when the tables addNotify method is called. This is called in response to a container, which contains the table, been added to a displayable container (like a visible frame).

This then calls configureEnclosingScrollPane

Updated

The above code assumes that the JScrollPane and JTable have already been added to a container that is already displayable, this may not always be the case. You could, alternatively, override the JTable#configureEnclosingScrollPane method and configure the headers when you want them to be...

@Override
protected void configureEnclosingScrollPane() {
    if (showHeaders) {
        super.configureEnclosingScrollPane();
    }
}

Now personally, I would create my own custom JTable which had a showColumnHeaders property and which could used to toggle the headers on and off based on my needs...

Upvotes: 4

alex2410
alex2410

Reputation: 10994

Try next code:

    JTable t = new JTable(3,3);
    JScrollPane jScrollPane = new JScrollPane(t);
    t.setTableHeader(null);
    jScrollPane.setColumnHeaderView(null);

or just setting tableHeader to null works for me t.setTableHeader(null);

Upvotes: 3

Related Questions