1

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.

  • PersonalInfo is a class not a function – dahui Jan 11 '17 at 15:38
  • 4
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 11 '17 at 15:44
  • 2
    4) *"..sorry if this is a duplicate.."* No need for apologies, there is a search box on the upper right of the page, and when typing a question, possible duplicates are shown beneath the message posting text box. 5) **It seems probable that `PersonalInfo` extends `JFrame`, and that is the major part of this problem, given that the `initialize()` method creates and configures a frame that is never set visible. I'm not sure if that is the answer to the question given there is no MCVE, ..and no question.** – Andrew Thompson Jan 11 '17 at 15:46
  • Continuing from Point (3) above.. As an alternative to the `null` layout (which will cause no end of problems), consider using a proper layout. [This example](https://i.stack.imgur.com/NRRU7.png) uses `GridBagLayout` - which is good for columns and rows of components of unequal size (among other things). To simplify it, it sets the 'header label' as a titled border for the entire container. – Andrew Thompson Jan 12 '17 at 02:31

1 Answers1

2

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);
       }
     }
TiyebM
  • 2,684
  • 3
  • 40
  • 66