0

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);
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user1406416
  • 181
  • 1
  • 1
  • 7
  • 1. Please post some code so we can better understand the problem, and 2. are you using `JScrollPane.setViewportView` or `JScrollPane.add` to place the inner panel? You should almost definitely be using the first one. – wchargin May 20 '12 at 17:42
  • 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); – user1406416 May 20 '12 at 17:50
  • jscp.setPreferredSize(new Dimension(width-100,height-100)); newPanel.setMinimumSize(new Dimension(width,height)); newPanel.setMaximumSize(new Dimension(width,height)); mainPanel.add(myStandardPanel, panelIdentifier); – user1406416 May 20 '12 at 17:51
  • I did it like this because I have a main Panel with a card layout. I then have each of my screens as cards. I want to be able to switch cards. However, not all of the cards are the same size. For some reason it is doing something really annoying in centering the cards instead of left aligning them. My code was working to left align until I tried adding a JScrollPanel because one of the cards was too big to display on a normal computer screen. I am using setViewportView. Thanks! – user1406416 May 20 '12 at 17:52
  • It's too difficult to read in this form - code in comments... Please edit your post and paste there your code sample using `Ctrl+K` – Xeon May 20 '12 at 18:43
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) such as [this one](http://stackoverflow.com/a/6432291/230513). – trashgod May 20 '12 at 19:20
  • I figured it out. I padded all my panels to be the same dimensions. Thanks – user1406416 May 25 '12 at 16:13

0 Answers0