jDave1984
jDave1984

Reputation: 972

JScrollPane won't scroll - Java Desktop Application

So I'm developing an application. I have a huge problem, and I know I'm probably overlooking something stupid, but my scollpanes aren't scrolling. Could someone please checkout the following code and tell me what I did wrong?

rotationPanel = new JPanel();
rotationPanel.setLayout(null);
rotationLabels = new JLabel[countStarters(team)];
resetXY(5,5);
for(int i = 0; i < countStarters(team); i++){
    rotationLabels[i] = new JLabel(team.rotation.get(i).getName());
    rotationLabels[i].setForeground(Color.BLACK);
    addComp(rotationLabels[i], rotationPanel, labelX, labelY, labelSize);
    labelY += 25;
}

//Other Code in between

rotationBar = new JScrollPane(rotationPanel);
rotationBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
rotationBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
rotationBar.setPreferredSize(new Dimension(520, 150));
addComp(rotationBar, this, 15, 75, new Dimension(520, 150));

//addComp method:
public void addComp(JComponent comp, JComponent panel, int xPos, int yPos, Dimension size){
    comp.setLocation(xPos,yPos);
    comp.setSize(size);
    panel.add(comp);
}

The resetXY() method just sets the x and y position for the components

Any help would be appreciated

Cheers, Dave

Upvotes: 0

Views: 527

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347332

JScrollPane uses either the components preferredSize or if implemented Scrollable#getPreferredScrollableViewportSize to determine what size the scroll pane and its view port can be. When the viewport is larger the the scroll pane, the scroll bars will appear

The Swing API has being designed around the use of the layout manager, choosing to do without the will cause you no end of problems and additional work.

Layout managers help you over come the difference between systems, including font rendering, DPI, screen sizes and rendering pipelines to mention a few.

Upvotes: 1

Aargonian
Aargonian

Reputation: 223

I think your JPanel needs to have its size set, otherwise it will shrink to fit the JScrollPane.

Upvotes: 0

Related Questions