How to get rid of the border with a JTable / JScrollPane
Asked Answered
I

6

40

If you run the small sample below you'll see a border around the center region. I'm not sure why this border is showing.

It happens when a JTable is in a JScrollPane. I tried various things to remove it but so far no luck. A JTable without the JScrollPane shows no border.

See sample below. TIA.

public class TestScrollPane extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new TestScrollPane();
        JPanel panel = new JPanel();
        JTable table = new JTable();

        panel.setLayout(new BorderLayout());
        panel.add(new JLabel("NORTH"), BorderLayout.NORTH);
        panel.add(new JLabel("SOUTH"), BorderLayout.SOUTH);

        JScrollPane sp = new JScrollPane(table);
        // None of these have any effect
        sp.setBorder(null);
        sp.getInsets().set(0, 0, 0, 0);
        sp.setViewportBorder(null);
        sp.getViewport().setBorder(null);
        sp.getViewport().getInsets().set(0, 0, 0, 0);
        sp.getViewport().setOpaque(true);

        panel.add(sp, BorderLayout.CENTER);
        // Adding the table alone shows no border
        // panel.add(table, BorderLayout.CENTER);
        frame.add(panel);

        frame.setVisible(true);
    }

    public TestScrollPane() throws HeadlessException {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(100, 100));
    }
}
Ingenerate answered 26/7, 2010 at 9:30 Comment(2)
Instead of setting null border, have you try BorderFactory.createEmptyBorder() ?Nock
Also, don't neglect to pack() the frame.Metathesis
N
74

Use BorderFactory.createEmptyBorder() instead of null...

by using:

sp.setBorder(createEmptyBorder());

it works.

Your main method becomes:

public static void main(String[] args) {
    JFrame frame = new TestScrollPane();
    JPanel panel = new JPanel();
    JTable table = new JTable();

    panel.setLayout(new BorderLayout());
    panel.add(new JLabel("NORTH"), BorderLayout.NORTH);
    panel.add(new JLabel("SOUTH"), BorderLayout.SOUTH);

    JScrollPane sp = new JScrollPane(table);
    sp.setBorder(BorderFactory.createEmptyBorder());
    panel.add(sp, BorderLayout.CENTER);
    frame.add(panel);

    frame.setVisible(true);
}
Nock answered 26/7, 2010 at 9:40 Comment(4)
Cool :). I think (but not sure) the default behavior of swing components is to have line border. So the BorderFactory class is your friend to customize them :)Nock
see krishna's answer below: sp.setViewportBorder(null);Darlinedarling
When the author wrote this, he used the setViewPortBorder(null), but it did'nt seem to work. Perhaps it was just a bug, which was fixed since 2010. I have left java development, so perhaps @krishna-gupta answer is the right one now.Nock
This answer worked for me in Java 6 on OS X Lion. I'll have to try it on my other machine (running Java 8 on OS X Mavericks) to see if maybe I'll need to write some version specific branch here or something...Originally
L
5

Interestingly the border disappears when you remove this line:

sp.setBorder(null);
Liriodendron answered 26/7, 2010 at 9:38 Comment(2)
Not for me with windows XP. Maybe it's a platform issue.Ingenerate
This is definitely dependent on your L&F. On OS X, you get a very small border that I think is supposed to look like a shadow or something (not quite sure - I placed the component in a nonstandard place where the border just didn't look right, thus my desire to remove it.)Originally
I
5

I was looking for the answer for the same question but above answers could not do... so I found a better answer:

JScrollPane jsp = new JScrollPane();

//ur other codes

jsp.setViewportBorder(null);
Impossibility answered 1/6, 2013 at 13:18 Comment(0)
E
1

For JTable table.setIntercellSpacing(new Dimension(0, 0)) works.

Equiponderate answered 24/9, 2015 at 7:57 Comment(0)
I
0

I think the proper fix is to set the border on the viewportView to 'null'.

Impala answered 19/8, 2010 at 18:36 Comment(0)
W
0

To remove the border from all parts of the JScrollPane including the vertical and horizontal bar the following code works

JScrollPane jsp = new JScrollPane();
jsp.getVerticalScrollBar().setBorder(null);
jsp.getHorizontalScrollBar().setBorder(null);
jsp.setBorder(null);
Waterford answered 5/8, 2020 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.