Why doesn't JScrollPane work when it is added to BorderLayout.NORTH?

96 Views Asked by At

I want to add a JScrollPane to a JFrame, and attach to its top, because if it fits on the frame, the panel in the scrollpane appears centered.The panel in the scrollpane has GridBagLayout, and has multiple panels on it, aligned from top to bottom. This is the code of how I display it:

JFrame frame = new JFrame(item.props.get("name"));
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().setBackground(new Color(0x202020));
JScrollPane scroll = new JScrollPane(new Weapon(item, tags, main));
scroll.setBorder(new CompoundBorder(new LineBorder(new Color(0x202020), 2), new LineBorder(new Color(0x202020), 2)));
scroll.getVerticalScrollBar().setUnitIncrement(24);
frame.getContentPane().add(scroll, BorderLayout.NORTH);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(size.width/2, size.height/2);
frame.setLocation((int) (size.width*0.25), (int) (size.height*0.25));
frame.setVisible(true);

When the scrollpane is added to BorderLayout.NORTH, it looks like the aim has been achieved, but when there's need for the scrollbar, it doesn't apear. If it is added to BorderLayout.CENTER, it works, but it's centered, which I want to avoid.

How could this layout be achieved?

0

There are 0 best solutions below