Search code examples
jpaneljscrollbar

How to add a scrollbar to JPanel


Hello guys i am having some trouble adding a scroll-bar to an application i wrote. As i have a 17 inch monitor i have no need for it but the other people have tried on smaller screens and couldn't access some items on the GUI. I thought about adding a scroll-bar but i am having difficulties. How would i add a scroll-bar to a JPanel i currently have. What it should do: If people re-size the window then it has they have to be able to scroll to the bottom, here is what i have but it doesn't work :s

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 753, 852);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    scrollPane= new JScrollPane(contentPane);
    contentPane.setPreferredSize(new Dimension(contentPane.getWidth(),contentPane.getHeight()));
    getContentPane().add( scrollPane, BorderLayout.CENTER );
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

    then i got alot of contantPane.add()...

Any help is appreciated. Thanks!


Solution

  • I think your error is how you are adding all of the components into the contentPane after you have already added it to the getContentPane() pane. Try adding all of the components to the contentPane before you dad the contentPane to the main content pane, if that makes sense.. I think that may be your problem.