0

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();
    }

}
mohsen kamrani
  • 6,935
  • 5
  • 38
  • 64
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Apr 21 '14 at 12:36
  • `name.setPreferredSize(new Dimension(400,35)); //Sets the size of the box.` A) No it doesn't do what the comment claims. B) You should not do it anyway. -- Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 21 '14 at 12:38
  • Thank you very much guys; I'm completely new to this site and suppose I should have read up a bit more before posting. Also that makes a lot of sense Andrew, thank you! Explains it a lot better than other mentions of not using setPreferredSize I've read. Will get that changed right away. – user3556490 Apr 21 '14 at 13:16
  • `I have SpringLayout set for my JPanel; why isn't this doing the job?` SpringLayout is one of the more complicated layout managers. If the constraints are not set properly, the preferred size may not be calculated properly. The scrollbars appear automatically when the preferred size of the panel is greater than the size of the scroll pane. I don'tuse SpringLayout so I can't tell if you constraints are correct or not. You should be able to do a quick test by using panel.getPreferredSize() after the GUI is visible to see if it is returning a reasonable value. – camickr Apr 21 '14 at 14:55
  • Mmm it's returning 0,0 so I think that's where the problem lies. How would I go about fixing this? I read somewhere about overriding the getPreferredSize method? – user3556490 Apr 21 '14 at 17:29

0 Answers0