Search code examples
javauser-interfacewindowframepane

Java GUI window does not have any output


Greetings The Fellowship of the Ring,

The following quest is set upon thee.

Thy Lord's program does not output a result on the window to my wishes.

The frame pops up but the panel does not work and therefore none of the buttons and controls show up.

Here is thy mission, should you choose to accept it:

import javax.swing.*;
import java.awt.*;        
import java.awt.Container.*;  

public class newAccount
{    

static JFrame bankFrame;
JButton buttonCreate;

// Panel Variable

static JPanel panelObject;

// Label Variables

JLabel labelName;
JLabel labelDOB;
JLabel labelGender;
JLabel labelAge;
JLabel labelPartner;
JLabel labelCountry;
JLabel labelCity;
JLabel labelAddress;
JLabel labelZipPostal;

// Data Entry Control Variables

JTextField textName;
JTextField textDOB;
JComboBox comboGender;
JTextField textAge;
JTextField textPartner;
JComboBox comboCountry;
JTextField textCity;
JTextField textAddress;
JTextField textZipPostal;

// Layout Variables

GridBagLayout gbObject;
GridBagConstraints gbc;

public void newAccount()
{
    // Initializing Laoyout Variables

    gbObject = new GridBagLayout();
    gbc = new GridBagConstraints();
    panelObject = new JPanel();
    panelObject.setLayout(gbObject);


    // Initializing Label Controls

    labelName = new JLabel ("Name");
    labelDOB = new JLabel ("Date of Birth");
    labelGender = new JLabel ("Gender");
    labelAge = new JLabel ("Age");
    labelPartner = new JLabel ("Partner");
    labelCountry = new JLabel ("Country");
    labelCity = new JLabel ("City");
    labelAddress = new JLabel ("Address");
    labelZipPostal = new JLabel ("Zip/Postal Code");

    // Data Entry Controls

    textName = new JTextField(50);
    textDOB = new JTextField(9);

    String packages[] = {"Male", "Female", "Other"};
    comboGender = new JComboBox (packages);

    textAge = new JTextField(3);
    textPartner = new JTextField(50);

    String packages1[] = {"Oman", "India", "United Arab Emirates", "United States of America", "United Kingdon"};
    comboCountry = new JComboBox (packages1);

    textCity = new JTextField(50);
    textAddress = new JTextField(100);
    textZipPostal = new JTextField(10);

    // Controls for Name

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 10;
    gbObject.setConstraints (labelName, gbc);
    panelObject.add (labelName);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 10;
    gbObject.setConstraints (textName, gbc);
    panelObject.add (textName);

    // Controls for Date of Birth

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 30;
    gbObject.setConstraints (labelDOB, gbc);
    panelObject.add (labelDOB);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 30;
    gbObject.setConstraints (textDOB, gbc);
    panelObject.add (textDOB);

    // Controls for Gender

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 50;
    gbObject.setConstraints (labelGender, gbc);
    panelObject.add (labelGender);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 50;
    gbObject.setConstraints (comboGender, gbc);
    panelObject.add (comboGender);

    // Controls for Age

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 70;
    gbObject.setConstraints (labelAge, gbc);
    panelObject.add (labelAge);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 70;
    gbObject.setConstraints (textAge, gbc);
    panelObject.add (textAge);

    // Controls for Partner

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 90;
    gbObject.setConstraints (labelPartner, gbc);
    panelObject.add (labelPartner);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 90;
    gbObject.setConstraints (textPartner, gbc);
    panelObject.add (textPartner);

    // Controls for Country

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 110;
    gbObject.setConstraints (labelCountry, gbc);
    panelObject.add (labelCountry);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 110;
    gbObject.setConstraints (comboCountry, gbc);
    panelObject.add (comboCountry);

    // Controls for City

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 130;
    gbObject.setConstraints (labelCity, gbc);
    panelObject.add (labelCity);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 130;
    gbObject.setConstraints (textCity, gbc);
    panelObject.add (textCity);

    // Controls for Address

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 150;
    gbObject.setConstraints (labelAddress, gbc);
    panelObject.add (labelAddress);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 150;
    gbObject.setConstraints (textAddress, gbc);
    panelObject.add (textAddress);

    // Controls for Zip or Postal Code

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 170;
    gbObject.setConstraints (labelZipPostal, gbc);
    panelObject.add (labelZipPostal);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 170;
    gbObject.setConstraints (textZipPostal, gbc);
    panelObject.add(textZipPostal);
}

public static void main (String args[])
{

    newAccount customerObj;
    customerObj = new newAccount();
    bankFrame = new JFrame("Bank account");

    bankFrame.setSize(300, 300);
    bankFrame.setVisible(true);

    bankFrame.getContentPane().add(panelObject);

    customerObj.newAccount();
}
}

Thy Lord's problem appears to be here:

public static void main (String args[])
{

newAccount customerObj;
customerObj = new newAccount();
bankFrame = new JFrame("Bank account");

bankFrame.setSize(300, 300);
bankFrame.setVisible(true);

bankFrame.getContentPane().add(panelObject); <--- Here

customerObj.newAccount();
}
}

Make thy Lord proud


Solution

  • My Lord,

    It appears that you have unintentionally swapped two lines of code.

    Instead of

    bankFrame.getContentPane().add(panelObject);
    customerObj.newAccount();
    

    try putting them in reverse order like this:

    customerObj.newAccount();
    bankFrame.getContentPane().add(panelObject);
    

    The reason for exception is that panelObject variable is static, and is not assigned until you call customerObj.newAccount() - this means that in original code - since panelObject was null - you have been trying to add nothing contentPane, and that's the reason for failing. When we first call customerObj.newAccount we are initialising panelObject and swing will not complain.

    Also please note that you should not interact with swing components from Main thread, but only from event dispatch thread. In order to do so just replace original main method with following:

        public static void main (String args[]) throws InvocationTargetException, InterruptedException {
    
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                newAccount customerObj;
                customerObj = new newAccount();
                bankFrame = new JFrame("Bank account");
    
                bankFrame.setSize(300, 300);
                bankFrame.setVisible(true);
    
                customerObj.newAccount();
                bankFrame.getContentPane().add(panelObject);
            }
        });
    
    }
    

    invokeAndWait will pass runnable to event dispatch thread and wait until swing executes that code before returning.

    Running original code with replaced main method - on my machine gives this:

    screenshoot