Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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));     } } 
like image 585
sproketboy Avatar asked Jul 26 '10 09:07

sproketboy


People also ask

How do I get rid of JScrollPane?

If you want to remove this whole panel, You need to remove it from the JFrame . So replace this statement with: frame. remove(game1);

How do I make JScrollPane resizable?

By default a JFrame uses a BorderLayout (so there is no need to reset it). All you need to do is add the JScrollPane to the CENTER of the BorderLayout and it will resize automatically. And the basic code would be: JTextArea textArea = new JTextArea(...); JScrollPane scrollPane = new JScrollPane(); frame.

How do I set the size of a JScrollPane?

The JScrollPane is as big as the element it holds. You should make those element(s) wider. Also, there is the setSize() -method. But you'll most likely want to use the setPreferredSize() -method as mentioned by mre.


Video Answer


2 Answers

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); } 
like image 71
sly7_7 Avatar answered Sep 23 '22 00:09

sly7_7


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); 
like image 22
Krishna Gupta Avatar answered Sep 25 '22 00:09

Krishna Gupta