I am trying to include a JPanel
inside a JScrollPane
. When I set the preferred size of the JScrollPane
, it automatically centers the panel inside of it. I want to have it left aligned. I noticed that the actual inner panel is the size of the JScrollPanel
, but for some reason, the contents are now centered. If I use the JScrollPane
methods of setMinimum
and setMaximum
instead of setPreferredSize
.
Edit: Code migrated from comments:
JPanel myStandardPanel = new JPanel();
myStandardPanel.setMinimumSize(new Dimension(width, height));
myStandardPanel.setMaximumSize(new Dimension(width, height));
myStandardPanel.setLayout(new BorderLayout());
JPanel alignHorizontalPanel = new JPanel();
alignHorizontalPanel.setMinimumSize(new Dimension(width, height));
alignHorizontalPanel.setMaximumSize(new Dimension(width, height));
alignHorizontalPanel.setLayout(new BorderLayout());
JScrollPane jscp = new JScrollPane(newPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jscp.setPreferredSize(new Dimension(width - 100, height - 100));
newPanel.setMinimumSize(new Dimension(width, height));
newPanel.setMaximumSize(new Dimension(width, height));
mainPanel.add(myStandardPanel, panelIdentifier);