Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove borders inside JScrollPane

I'm trying to put multiple JLists in a JPanel and the JPanel in a JScrollPane. The problem is that when I add the JScrollPane to the equation there is a space between the JList and outer border, how do I get rid of this border so it's fills horizontally?

Here is a small sample code that demonstrates this problem with a JLabel instead of JList.

/edit: It's seems to be windows thing, it runs fine on Mac OS.

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame
{  
    public static void main(String[] args) 
    {
        Test test = new Test();
    }

    public Test()
    {
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());        
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        
        this.setContentPane(contentPane);        
        this.setMinimumSize(new Dimension(400, 300));

        JScrollPane sp = new JScrollPane(createLeftPane());
        sp.setBorder(BorderFactory.createLineBorder(Color.yellow));
        this.add(sp, BorderLayout.WEST);

        LookAndFeel.installBorder(sp, "BorderFactory.createEmptyBorder()");        

        StackTraceElement[] st = Thread.currentThread().getStackTrace();
        for(int i = 0; i < st.length; i++) {
            System.out.println(st[i]);
        }

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    private JPanel createLeftPane()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        JLabel label = new JLabel("TEST LABEL");
        label.setBorder(BorderFactory.createLineBorder(Color.blue));
        panel.add(label, c);
        panel.setBorder(BorderFactory.createLineBorder(Color.red));

        return panel;
    }        
}

Here's a screenshot (first one is without the scrollpane) JScrollPane Problems

Stack Trace:

java.lang.Thread.getStackTrace(Thread.java:1479) 
test.Test.<init>(Test.java:27) 
test.Test.main(Test.java:10)

/edit: After some trial and error I found out that when I add a c.weightx = 0.5 to the contrains it does fill Horizontally, but when the scrollPane becomes larger than it's content it makes itself wider, which is weird. See the screenshots below:

enter image description here

like image 217
Jesse Avatar asked Aug 19 '26 17:08

Jesse


2 Answers

The problem isn't because of the scrollpane. Its because you add the label to a JPanel. By default a panel uses a FlowLayot which has has a vertical/horizontal gap of 5 pixels around each component.

Change the FlowLayout to use a 0 gap, or use a different layout. Maybe a BoxLayout.

like image 155
camickr Avatar answered Aug 22 '26 06:08

camickr


component.setBorder(null) removes any border which has been set on the component

like image 41
Romantic Electron Avatar answered Aug 22 '26 07:08

Romantic Electron