Search code examples
javaswingjframejpaneljscrollpane

Why isn't my JScrollPane working?


I'm trying to make my JPanel scrollable but it's not working. I made a JPanel, added components to it, then added my JPanel to a JScrollPane. This is what you're supposed to do, right? What am I doing wrong?

import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingConstants;


public class RegisterPane extends JPanel {
    private JTextField txtJohnDoe;
    private JTextField txtExampledomaincom;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;

    /**
     * Create the panel.
     */
    public RegisterPane() {
        setLayout(null);

        JPanel panel = new JPanel();
        panel.setBounds(6, 36, 300, 450);
        panel.setLayout(null);

        JLabel lblFirstName = new JLabel("First Name");
        lblFirstName.setBounds(28, 6, 92, 16);
        panel.add(lblFirstName);

        txtJohnDoe = new JTextField();
        txtJohnDoe.setText("John Doe");
        txtJohnDoe.setBounds(124, 0, 251, 28);
        panel.add(txtJohnDoe);
        txtJohnDoe.setColumns(10);

        txtExampledomaincom = new JTextField();
        txtExampledomaincom.setText("[email protected]");
        txtExampledomaincom.setColumns(10);
        txtExampledomaincom.setBounds(124, 40, 251, 28);
        panel.add(txtExampledomaincom);

        JLabel lblEmail = new JLabel("Email");
        lblEmail.setBounds(28, 46, 92, 16);
        panel.add(lblEmail);

        JLabel lblGender = new JLabel("Gender");
        lblGender.setBounds(28, 89, 55, 16);
        panel.add(lblGender);

        JRadioButton rdbtnMale = new JRadioButton("Male");
        rdbtnMale.setBounds(124, 85, 92, 23);
        panel.add(rdbtnMale);

        JRadioButton rdbtnFemale = new JRadioButton("Female");
        rdbtnFemale.setBounds(283, 85, 92, 23);
        panel.add(rdbtnFemale);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(28, 157, 55, 16);
        panel.add(lblPassword);

        JLabel passDirections = new JLabel();
        passDirections.setHorizontalAlignment(SwingConstants.CENTER);
        passDirections.setFont(new Font("Lucida Grande", Font.PLAIN, 10));
        passDirections.setBackground(SystemColor.window);
        passDirections.setText("Password's Must be at Least 6 characters long and contain 1  non letter character");
        passDirections.setBounds(29, 117, 346, 28);
        panel.add(passDirections);

        passwordField = new JPasswordField();
        passwordField.setBounds(124, 151, 251, 28);
        panel.add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(124, 195, 251, 28);
        panel.add(passwordField_1);

        JLabel lblRetypePassword = new JLabel("Password Again");
        lblRetypePassword.setBounds(28, 201, 92, 16);
        panel.add(lblRetypePassword);

        JCheckBox chckbxSubscribeToNews = new JCheckBox("Subscribe to News Letter");
        chckbxSubscribeToNews.setSelected(true);
        chckbxSubscribeToNews.setHorizontalAlignment(SwingConstants.CENTER);
        chckbxSubscribeToNews.setBounds(29, 244, 346, 23);
        panel.add(chckbxSubscribeToNews);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(28, 280, 55, 16);
        panel.add(lblNewLabel);

        JScrollPane scroll = new JScrollPane(panel);
        scroll.setSize(new Dimension(450,300));
        panel.setAutoscrolls(true);
        add(scroll);
    }
}

Here's my JFrame class

import java.awt.BorderLayout;


public class MainFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        setResizable(false);
        setContentPane(new RegisterPane());

            //RegisterPane isn't scrolling ^

    }

}

Thanks in Advance for your help!


Solution

  • I'm seeing setLayout(null) alot in your code.

    The JViewport uses the component's/view's preferred size as a bases for determining if the view expands beyond the visible bounds of the JScrollPane, because you've seen fit to ignore this feature, the components have begun to break down.

    Swing is designed to work layout managers and it makes it much easier to develop complex user interfaces that can work across a multitude of platforms and environments