Why is this list in a JScrollPane not left aligning?

Viewed 1142

I can't seem to get this to work. I'm trying to get the list on the right to left align but it's not working and I can't seem to find the fix.

Here's what the code I have is showing:

enter image description here

Here's the code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Example {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(400, 200);
        // main panel
        JPanel pan = new JPanel();
        pan.setLayout(new GridLayout(1, 2));
        pan.setBackground(Color.BLUE);
        jFrame.getContentPane().add(pan, BorderLayout.CENTER);
        jFrame.show();
        // left panel
        JPanel left = getContentPanel();
        left.setBackground(Color.ORANGE);
        pan.add(left);
        // right panel (with scroll pane)
        JPanel right = getContentPanel();
        right.setBackground(Color.YELLOW);
        JScrollPane scr = new JScrollPane(right);
        scr.setBackground(Color.CYAN);
        scr.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        pan.add(scr);
    }

    private static JPanel getContentPanel() {
        JPanel rtn = new JPanel();
        rtn.setLayout(new GridBagLayout());
        GridBagConstraints cs = new GridBagConstraints();
        cs.gridx = 0;
        for (int i = 0; i < 100; i++) {
            JLabel label = new JLabel("Item " + (i + 1));
            label.setBackground(Color.DARK_GRAY);
            cs.gridy = i;
            rtn.add(label, cs);
        }
        rtn.setBackground(Color.GREEN);
        return rtn;
    }

}
1 Answers
Related