Minelava
Minelava

Reputation: 221

How to remove border inside JScrollPane

Hello are there ways to remove the border header inside JScrollPane?

Here is the picture of my JScrollPane header border Remove the ugly inside header border

I tried many ways to remove the header border like including setting border to null, but no success.

Here is the code that set the JScrollPane border...

JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.getVerticalScrollBar().setUI(new CustomJScrollBar());
    scrollPane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
    scrollPane.setBounds(105, 127, 1120, 540);
    scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    scrollPane.getViewport().setBackground(Color.WHITE);

    add(scrollPane);

Thank you so much!

Upvotes: 1

Views: 222

Answers (1)

BarrySW19
BarrySW19

Reputation: 3809

You would probably need to augment the default cell header renderer, e.g.:

public class MyRenderer implements TableCellRenderer {
    private TableCellRenderer parent;
    private Border emptyBorder = BorderFactory.createEmptyBorder();

    private MyRenderer(TableCellRenderer parent) {
        this.parent = parent;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel headerLabel = (JLabel) parent.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        headerLabel.setBorder(emptyBorder);
        return headerLabel;
    }
}

Then, you would set this as the renderer to use with:

JTable jt = <your table>
JTableHeader tableHeader = jt.getTableHeader();
tableHeader.setDefaultRenderer(new MyRenderer(tableHeader.getDefaultRenderer()));

Upvotes: 2

Related Questions