0

So I have a Java GUI exercise from college to work on, about making a registration form.

I've [mistakenly] coded everything from beginning to end without testing, and found out that nothing actually appears on my GUI. The program was just a plain JFrame without anything inside, like in the picture above.

I've tried to find out why this happened, however I haven't found any clue why. So your help is appreciated.

So far, I haven't found any duplicate of this question. I found that my issue is somewhat different from other users. So, I'd like to apologize as if there's one duplicate or so.

Below is the code: (you may want to take a keen look at this)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class RegistrationForm extends JFrame {

    private JPanel panel;    
    private JPanel northPanel;
    private JPanel eastPanel;
    private JPanel westPanel;
    private JPanel southPanel;
    private JPanel centerPanel;

    private JPanel formPanel;
    private JPanel basicFormPanel;
    private JPanel addressPanel;
    private JPanel typePanel;
    private JPanel cityPanel;
    private JPanel agreementPanel;
    private JPanel buttonPanel;

    private JLabel formTitle;
    private JLabel lblName;
    private JLabel lblEmail;
    private JLabel lblPhone;
    private JLabel lblAddress;
    private JLabel lblType;
    private JLabel lblCity;

    private JTextField name;
    private JTextField email;
    private JTextField phone;
    private JScrollPane addressScroll;
    private JTextArea address;
    private ButtonGroup type;
    private JRadioButton silver;
    private JRadioButton gold;
    private JRadioButton platinum;
    private JComboBox<String> city;
    private JCheckBox agreement;

    private JButton submit;
    private JButton reset;

    public RegistrationForm() {
        initElements();
        initView();
        initFormPanels();
    }

    public void initElements() {
        panel = new JPanel();
        northPanel = new JPanel();
        eastPanel = new JPanel();
        westPanel = new JPanel();
        southPanel = new JPanel();
        centerPanel = new JPanel();

        formPanel = new JPanel();
        basicFormPanel = new JPanel();
        addressPanel = new JPanel();
        typePanel = new JPanel();
        cityPanel = new JPanel();
        agreementPanel = new JPanel();
        buttonPanel = new JPanel();

        formTitle = new JLabel("Registration Form");
        formTitle.setFont(new Font("Arial", Font.PLAIN, 32));

        lblName = new JLabel("Name");
        lblEmail = new JLabel("Email");
        lblPhone = new JLabel("Phone");
        lblAddress = new JLabel("Address");
        lblType = new JLabel("Type");
        lblCity = new JLabel("City");

        name = new JTextField();
        email = new JTextField();
        phone = new JTextField();
        address = new JTextArea();
        addressScroll = new JScrollPane(address);
        type = new ButtonGroup();
        silver = new JRadioButton("Silver");
        gold = new JRadioButton("Gold");
        platinum = new JRadioButton("Platinum");
        String[] cities = {"Jakarta", "Bandung", "Bogor"};
        city = new JComboBox<String>(cities);
        agreement = new JCheckBox("I agree with the terms and conditions");

        submit = new JButton("Submit");
        reset = new JButton("Reset");
    }

    public void initView() {
        panel.setLayout(new BorderLayout());
        panel.add(northPanel, BorderLayout.NORTH);
        panel.add(eastPanel, BorderLayout.EAST);
        panel.add(westPanel, BorderLayout.WEST);
        panel.add(southPanel, BorderLayout.SOUTH);
        panel.add(centerPanel, BorderLayout.CENTER);

        northPanel.setBackground(Color.black);
        northPanel.setPreferredSize(new Dimension(50, 50));
        formTitle.setForeground(Color.white);

        eastPanel.setBackground(Color.black);
        westPanel.setBackground(Color.black);
        southPanel.setBackground(Color.black);      
        centerPanel.setBackground(Color.gray);

        formPanel.setBackground(Color.gray);
        basicFormPanel.setBackground(Color.gray);
        addressPanel.setBackground(Color.gray);
        typePanel.setBackground(Color.gray);
        cityPanel.setBackground(Color.gray);
        agreementPanel.setBackground(Color.gray);

        formPanel.add(basicFormPanel);
        formPanel.add(addressPanel);
        formPanel.add(typePanel);
        formPanel.add(cityPanel);
        formPanel.add(agreementPanel);
        formPanel.add(buttonPanel);
        centerPanel.add(formPanel);

        buttonPanel.setBackground(Color.black);
        buttonPanel.add(submit);
        buttonPanel.add(reset);
        southPanel.add(buttonPanel);
    }

    public void initFormPanels() {
        // basic form panel
        basicFormPanel.setLayout(new GridLayout(3, 3, 5, 5));

        basicFormPanel.add(lblName);
        basicFormPanel.add(name);

        basicFormPanel.add(lblEmail);
        basicFormPanel.add(email);

        basicFormPanel.add(lblPhone);
        basicFormPanel.add(phone);

        // address panel
        addressPanel.setLayout(new GridLayout(1, 2, 5, 5));

        addressPanel.add(lblAddress);
        addressPanel.add(addressScroll);

        // type panel
        typePanel.setLayout(new GridLayout(1, 2, 5, 5));

        typePanel.add(lblType);

        type.add(silver);
        type.add(gold);
        type.add(platinum);

        buttonPanel.add(silver);
        buttonPanel.add(gold);
        buttonPanel.add(platinum);

        typePanel.add(buttonPanel);

        // city panel
        cityPanel.setLayout(new GridLayout(1, 2, 5, 5));

        cityPanel.add(lblCity);
        cityPanel.add(city);

        // agreement checkbox panel
        agreementPanel.setLayout(new FlowLayout());
        agreementPanel.add(agreement);

        // button panel
        buttonPanel.add(submit);
        buttonPanel.add(reset);

        // set preferred sizes
        basicFormPanel.setPreferredSize(new Dimension(400, 100));
        addressPanel.setPreferredSize(new Dimension(400, 90));
        cityPanel.setPreferredSize(new Dimension(400, 30));
        typePanel.setPreferredSize(new Dimension(400, 50));
        agreementPanel.setPreferredSize(new Dimension(400, 30));
        buttonPanel.setPreferredSize(new Dimension(400, 50));
    }

    public static void main(String[] args) {
        RegistrationForm gui = new RegistrationForm();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(450, 450);
        gui.setTitle("Registration");
        gui.setLocationRelativeTo(null);
        gui.setVisible(true);
    }
}

Thanks for helping out. I hope I'll get this done very soon.

  • 1
    You got to 213 lines of code before you realized that nothing appears? A good case for compile (and run) often. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jan 22 '15 at 15:15
  • 1
    1) Where in that code is *anything* added to the frame? 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jan 22 '15 at 15:18

3 Answers3

1

Change:

    panel.add(centerPanel, BorderLayout.CENTER);

To this to see something:

    panel.add(centerPanel, BorderLayout.CENTER);
    setContentPane(panel);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Looks like you need to add your root component you made (panel) to this.

public RegistrationForm() {
    initElements();
    initView();
    initFormPanels();
    this.add(panel);
}
NESPowerGlove
  • 5,496
  • 17
  • 28
1

add this.add(panel) in RegistrationForm constructor.It worked for me.

Prinz Km
  • 323
  • 1
  • 12