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)
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: