Nothing appears in the Java GUI

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.

Answers


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

public RegistrationForm() {
    initElements();
    initView();
    initFormPanels();
    this.add(panel);
}

Change:

    panel.add(centerPanel, BorderLayout.CENTER);

To this to see something:

    panel.add(centerPanel, BorderLayout.CENTER);
    setContentPane(panel);

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


Need Your Help

Comparing each of the <option>s in a <select> element using jQuery

javascript jquery html

I am writing a plugin for WordPress and am confused as to how to compare the user-input (got from an &lt;input&gt; element) to a set of &lt;option&gt; elements in a &lt;select&gt; element.

mySQL Restrict Cascade No Action settings

mysql cascade restrict

What is the best constraint to use on a forum table where users leave comments?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.