Bkmm3
Bkmm3

Reputation: 78

Why are my components invisible?

This is my code for creating a window using swing.

I can see the window of defined size but none of the components are present in the window.

Why are the components not visible?

I have separate methods are for creating, initializing and adding the components. These methods are invoked from the constructor. The window with title and defined size is visible in the output. What am I missing?

package swing_basics;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MySwingDemo extends JFrame {

    JLabel lblName, lblPassword; //Declaration of variables
    JTextField txtfName;
    JPasswordField pwdfPassword;
    JButton btnSubmit, btnCancel, btnReset;

    public void createComponents(){  //method to initialise the components

        lblName = new JLabel();
        lblPassword = new JLabel();
        txtfName = new JTextField();
        pwdfPassword = new JPasswordField();
        btnSubmit = new JButton();
        btnCancel = new JButton();
        btnReset = new JButton();
    }

    public void setComponents(){  //method to set the components
        setVisible(true);
        setSize(400, 400);
        setTitle("My Swing Demo");
        setLayout(new FlowLayout());

        lblName.setText("Name");
        lblPassword.setText("Password");

        txtfName.setText("Name");// try

        pwdfPassword.setText("Password");

        btnSubmit.setText("Submit");
        btnCancel.setText("Cancel");
        btnReset.setText("Reset");
    }

    public void addComponents(JFrame frame){  //method to add the components
        frame.add(lblName);
        frame.add(txtfName);

        frame.add(lblPassword);
        frame.add(pwdfPassword);

        frame.add(btnSubmit);
        frame.add(btnCancel);
        frame.add(btnReset);
    }

    public static void main(String[] args) {
        new MySwingDemo();
    }

    public MySwingDemo() {  //Constructor
        createComponents();
        setComponents();
        addComponents(this);
    }

}

Upvotes: 1

Views: 144

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

It's the order of operations, you're setting the frame visible before you add (and setup) your components. Instead move setVisible(true); to after you have setup your components. And, make sure you call addComponents(this); before you call setComponents();.

Like

public MySwingDemo() { // Constructor
    createComponents();
    addComponents(this);
    setComponents();
}

and I would also add a default frame close operation

public void setComponents() { // method to set the components
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("My Swing Demo");
    setLayout(new FlowLayout());

    lblName.setText("Name");
    lblPassword.setText("Password");

    txtfName.setText("Name");// try

    pwdfPassword.setText("Password");

    btnSubmit.setText("Submit");
    btnCancel.setText("Cancel");
    btnReset.setText("Reset");
    setVisible(true);
}

Upvotes: 3

Related Questions