I've been trying to find an answer to how to get my JScrollPane to scroll, but really don't understand how to fix it. Was hoping someone could point me in the right direction.
My MainArea is a JPanel that adds a JScrollPane to it. This in turns adds a JPanel as the viewport, and this JPanel has some JLabels added to it, based on each players info in an ArrayList. The problem I'm having is that the JScrollPane is only displaying the first 8 or so names, and then it goes "off the page" without allowing scrolling.
I've been searching around and found out this was perhaps something to do with my container (the viewport JPanel) not having the right dimensions; if this is the case how do I go about fixing that? In searching for answers I found that using setPreferredDimension was a no-go (woops - might have to change some code there), but I literally have no clue on how to make it the right dimensions. Someone also mentioned using the right layout manager should solve it - I have SpringLayout set for my JPanel; why isn't this doing the job?
Been working on this for a bit so maybe I'm just being stupid but my mind has gone blank. Cheers for your help.
Code below:
public class PlayerManagerMain extends MainArea {
private JScrollPane scroll;
private SpringLayout layout;
private JPanel panel;
public PlayerManagerMain(ArrayList<Player> players) {
super();
scroll = new JScrollPane();
layout = new SpringLayout();
panel = new JPanel();
panel.setLayout(layout);
scroll.setPreferredSize(new Dimension(700,300));
//panel.setBackground(null);
//panel.setOpaque(false);
this.add(scroll);
/*
* Sort out the scroll bar.
*/
scroll.setViewportView(panel);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
for(int i=0; i<players.size();i++) {
/*
* Creates the name box and sets visual aspects.
*/
JLabel name = new JLabel(players.get(i).getName()); //Creates a new name JLabel.
name.setFont(new Font("verdana",Font.PLAIN,15)); //Sets the font.
name.setPreferredSize(new Dimension(400,35)); //Sets the size of the box.
name.setBorder(BorderFactory.createLineBorder(new Color(0x222222))); //Creates the border for the JLabel.
name.setHorizontalAlignment(SwingConstants.CENTER); //Centers the text.
/*
* Creates the status box and sets visual aspects.
*/
JLabel status = new JLabel(); //Creates a new name JLabel.
status.setFont(new Font("verdana",Font.PLAIN,15)); //Sets the font.
status.setPreferredSize(new Dimension(200,35)); //Sets the size of the box.
status.setBorder(BorderFactory.createLineBorder(new Color(0x222222))); //Creates the border for the JLabel.
status.setHorizontalAlignment(SwingConstants.CENTER); //Centers the text.
if(players.get(i).getActive()==true) {
status.setText("ACTIVE");
name.setForeground(new Color(0xFF9900));
status.setForeground(new Color(0xFF9900));
}
else {
status.setText("INACTIVE");
name.setForeground(new Color(0x990000));
status.setForeground(new Color(0x990000));
}
panel.add(name);
panel.add(status);
layout.putConstraint(SpringLayout.WEST, name, 20, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, name, 20+40*i, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, status, 5, SpringLayout.EAST, name);
layout.putConstraint(SpringLayout.NORTH, status, 0, SpringLayout.NORTH, name);
}
panel.revalidate();
}
}