YepNepDep
YepNepDep

Reputation: 67

JPanel not showing, tried doing things like invokeLater, still not showing

I've been having a few problems with a JPanel not showing up(Probably because of the way everything is threaded in swing?). I've tried re-ordering where it's added, adding SwingUtilities.invokeLater(...); and still haven't worked. Searching on Google, most of them just say to use the invokeLater function, which doesn't work for me. I'd assume it wouldn't need a layout manager, since the sizes and positions are done by .SetBounds()? This is the code I've got at the moment

public class MenuLogin extends JPanel{

private JTextField txtUsername;
private JPasswordField txtPassword; 

public MenuLogin(){

    setBounds(0, 0, 380, 205);
    setBackground(Color.WHITE);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JLabel lblLogin = new JLabel("Login");
            lblLogin.setBounds(155, 5, 85, 42);
            lblLogin.setFont(new Font("Trebuchet MS", Font.PLAIN, 36));
            lblLogin.setHorizontalAlignment(SwingConstants.CENTER);


            JPanel form = new JPanel(); // The panel that won't show
            form.setBorder(null);
            form.setBounds(10, 74, 390, 195);
            add(form);
            form.setLayout(null);
            form.setVisible(true);

            txtUsername = new JTextField();
            txtUsername.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
            txtUsername.setToolTipText("Username");
            txtUsername.setBounds(10, 30, 370, 30);
            txtUsername.setColumns(16);
            form.add(txtUsername);

            txtPassword = new JPasswordField();
            txtPassword.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
            txtPassword.setToolTipText("Password");
            txtPassword.setColumns(16);
            txtPassword.setBounds(10, 78, 370, 30);
            form.add(txtPassword);

            JButton btnProceed = new JButton("Proceed");
            btnProceed.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
            btnProceed.setBounds(150, 119, 94, 30);
            form.add(btnProceed);

            JButton btnPlayAsGuest = new JButton("Play As Guest");
            btnPlayAsGuest.setFont(new Font("Trebuchet MS", Font.PLAIN, 9));
            btnPlayAsGuest.setBounds(291, 161, 89, 23);
            form.add(btnPlayAsGuest);

            JButton btnSignUp = new JButton("Sign Up");
            btnSignUp.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
            btnSignUp.setBounds(155, 160, 83, 23);
            form.add(btnSignUp);
            add(lblLogin);
        }
      });
}
}

The only thing is, the first JLabel is shown... Only the jlabel shows...

Any suggestions as to why it is still not showing would be nice. Thanks.

Upvotes: 0

Views: 250

Answers (2)

Branislav Lazic
Branislav Lazic

Reputation: 14806

This should work:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MenuLogin extends JPanel {

    private JTextField txtUsername;
    private JPasswordField txtPassword;

    public MenuLogin() {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);

        JPanel form = new JPanel(); // The panel that won't show
        form.setVisible(true);

        JLabel lblLogin = new JLabel("Login");
        lblLogin.setFont(new Font("Trebuchet MS", Font.PLAIN, 36));
        form.add(lblLogin);

        txtUsername = new JTextField();
        txtUsername.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
        txtUsername.setToolTipText("Username");
        txtUsername.setColumns(16);
        form.add(txtUsername);

        txtPassword = new JPasswordField();
        txtPassword.setFont(new Font("Trebuchet MS", Font.PLAIN, 17));
        txtPassword.setToolTipText("Password");
        txtPassword.setColumns(16);
        form.add(txtPassword);

        JButton btnProceed = new JButton("Proceed");
        btnProceed.setFont(new Font("Trebuchet MS", Font.PLAIN, 15));
        form.add(btnProceed);

        JButton btnPlayAsGuest = new JButton("Play As Guest");
        btnPlayAsGuest.setFont(new Font("Trebuchet MS", Font.PLAIN, 9));
        form.add(btnPlayAsGuest);

        JButton btnSignUp = new JButton("Sign Up");
        btnSignUp.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
        form.add(btnSignUp);

        add(form, BorderLayout.CENTER);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.add(new MenuLogin());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

I didn't even try to find which one of setBounds methods caused problem (since I never use them, and you shouldn't too). I just removed setBounds methods and implemented suitable layout manager (FlowLayout - its very primitive).

When it comes to some advanced layouts, I personally prefer MigLayout because of simplicity. Also, have a look at GridBagLayout.

Regards.

Upvotes: 4

Sanjaya Liyanage
Sanjaya Liyanage

Reputation: 4746

you need to add your panel in a frame

JFrame frame = new JFrame();
 frame.add(form);

Upvotes: 0

Related Questions