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));
    }
}
almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
sproketboy
  • 8,967
  • 18
  • 65
  • 95

6 Answers6

73

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);
}
sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • 1
    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 :) – sly7_7 Jul 26 '10 at 10:08
  • 2
    see krishna's answer below: `sp.setViewportBorder(null);` – Asaf Jul 08 '13 at 14:17
  • 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. – sly7_7 Jul 08 '13 at 14:28
  • 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... – ArtOfWarfare Mar 19 '14 at 12:47
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);
Matt
  • 74,352
  • 26
  • 153
  • 180
Krishna Gupta
  • 1,219
  • 11
  • 12
5

Interestingly the border disappears when you remove this line:

sp.setBorder(null);
Guillaume
  • 14,306
  • 3
  • 43
  • 40
  • 1
    Not for me with windows XP. Maybe it's a platform issue. – sproketboy Jul 26 '10 at 09:52
  • 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.) – ArtOfWarfare Mar 19 '14 at 12:45
1

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

Unihedron
  • 10,902
  • 13
  • 62
  • 72
0

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

RSC
  • 17
  • 1
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);
hms11
  • 54
  • 6