Laura Cîrstea
Laura Cîrstea

Reputation: 147

Java Swing open existing form

I'm trying to call a method that opens a new existing frame, but all I get is an empty frame without content.

This is the code that calls the function when a button is preesed:

JButton btnPersonalInfo = new JButton("Personal Info");
btnPersonalInfo.setBounds(10, 5, 120, 23);
btnPersonalInfo.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFrame PersonalInfo = new JFrame("Personal Info"); 
        PersonalInfo content = new PersonalInfo();
        PersonalInfo.setContentPane(content);
        PersonalInfo.setSize(700,700);
        PersonalInfo.setLocation(100,15);
        PersonalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
        PersonalInfo.setResizable(false);
        PersonalInfo.setVisible(true);
    }
});
panel_1.add(btnPersonalInfo);

This is how I initialize the PersonalInfo function:

    public PersonalInfo() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        JLabel lblPersonalInfo = new JLabel("Personal Information");
        lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
        lblPersonalInfo.setBounds(110, 11, 185, 14);
        panel.add(lblPersonalInfo);

        JLabel lblFullName = new JLabel("Full Name");
        lblFullName.setBounds(10, 36, 58, 14);
        panel.add(lblFullName);

        JLabel lblNationality = new JLabel("Nationality");
        lblNationality.setBounds(10, 61, 78, 14);
        panel.add(lblNationality);

        JLabel lblDateBirth = new JLabel("Date of Birth");
        lblDateBirth.setBounds(10, 86, 78, 14);
        panel.add(lblDateBirth);

        JLabel lblGender = new JLabel("Gender");
        lblGender.setBounds(10, 111, 46, 14);
        panel.add(lblGender);

        JLabel lblAddress = new JLabel("Address");
        lblAddress.setBounds(10, 164, 58, 14);
        panel.add(lblAddress);

        JLabel lblMobile = new JLabel("Mobile");
        lblMobile.setBounds(10, 189, 46, 14);
        panel.add(lblMobile);

        JLabel lblEmail = new JLabel("E-mail");
        lblEmail.setBounds(10, 214, 46, 14);
        panel.add(lblEmail);

        JRadioButton rdbtnM_2 = new JRadioButton("M");
        rdbtnM_2.setBounds(74, 133, 109, 23);
        panel.add(rdbtnM_2);

        JRadioButton rdbtnF = new JRadioButton("F");
        rdbtnF.setBounds(74, 107, 109, 23);
        panel.add(rdbtnF);
    }
}

Basically I'm expecting to view the frame from the PersonalInfo method when I press the btnPersonalInfo button and right now I only get an empty frame.

Thank you and sorry if this is a duplicate, but it's my first question here.

Upvotes: 1

Views: 628

Answers (1)

user3408531
user3408531

Reputation:

Remove frame = new JFrame(); from your initialize()

Simply:

    JPanel panel = new JPanel();
    add(panel, BorderLayout.CENTER);//It will be added to the ContentPane by default.
    panel.setLayout(null);
    ...

Then when calling, remove the following block:

    JFrame PersonalInfo = new JFrame("Personal Info"); 
    PersonalInfo content = new PersonalInfo();
    PersonalInfo.setContentPane(content);

Replace it by:

PersonalInfo personalInfo = new PersonalInfo(); 
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
....

So this should be your method call:

   public void actionPerformed(ActionEvent e) {
            PersonalInfo personalInfo = new PersonalInfo(); 
             personalInfo.setSize(700,700);
            personalInfo.setLocation(100,15);
            personalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
            personalInfo.setResizable(false);
            personalInfo.setVisible(true);
        }

And this should be your Class:

    public class PersonalInfo extends JFrame
{
    public PersonalInfo() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        JPanel panel = new JPanel();
        add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        JLabel lblPersonalInfo = new JLabel("Personal Information");
        lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
        lblPersonalInfo.setBounds(110, 11, 185, 14);
        panel.add(lblPersonalInfo);

        JLabel lblFullName = new JLabel("Full Name");
        lblFullName.setBounds(10, 36, 58, 14);
        panel.add(lblFullName);

        JLabel lblNationality = new JLabel("Nationality");
        lblNationality.setBounds(10, 61, 78, 14);
        panel.add(lblNationality);

        JLabel lblDateBirth = new JLabel("Date of Birth");
        lblDateBirth.setBounds(10, 86, 78, 14);
        panel.add(lblDateBirth);

        JLabel lblGender = new JLabel("Gender");
        lblGender.setBounds(10, 111, 46, 14);
        panel.add(lblGender);

        JLabel lblAddress = new JLabel("Address");
        lblAddress.setBounds(10, 164, 58, 14);
        panel.add(lblAddress);

        JLabel lblMobile = new JLabel("Mobile");
        lblMobile.setBounds(10, 189, 46, 14);
        panel.add(lblMobile);

        JLabel lblEmail = new JLabel("E-mail");
        lblEmail.setBounds(10, 214, 46, 14);
        panel.add(lblEmail);

        JRadioButton rdbtnM_2 = new JRadioButton("M");
        rdbtnM_2.setBounds(74, 133, 109, 23);
        panel.add(rdbtnM_2);

        JRadioButton rdbtnF = new JRadioButton("F");
        rdbtnF.setBounds(74, 107, 109, 23);
        panel.add(rdbtnF);
       }
     }

Upvotes: 2

Related Questions