如何删除JScrollPane中的边框

时间:2015-01-30 11:06:10

标签: java jscrollpane

您好有办法删除JScrollPane中的边框标题吗?

这是我的JScrollPane标题边框的图片 Remove the ugly inside header border

我尝试了多种方法来删除标题边框,例如将border设置为null,但没有成功。

以下是设置JScrollPane边框的代码...

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

非常感谢你!

1 个答案:

答案 0 :(得分:2)

您可能需要扩充默认的单元标题渲染器,例如:

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

然后,您可以将其设置为要使用的渲染器:

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