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)); } }
If you want to remove this whole panel, You need to remove it from the JFrame . So replace this statement with: frame. remove(game1);
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.
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.
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); }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With