-1

i'm a newbie programmer and im kinda stuck with this issue for 2 hours now. So my issue is that whenever i click the Register button, only a title bar (no contents at all) pops up in the upper left of my screen. How can I fix this? Thank you in advance

btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AccountCreationGUI window = new AccountCreationGUI();
window.setVisible(true);
frame.dispose();
                
            }
        });
btnRegister.setBounds(199, 170, 89, 23);
frame.getContentPane().add(btnRegister);

AccountCreationGUI (I didnt add the other method to make this short)

package main;
import java.sql.*;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import java.awt.Window.Type;

public class AccountCreationGUI extends JFrame {

    private JFrame frmToolInventory;
    private JTextField textfield_firstname;
    private JTextField textfield_lastname;
    private JTextField textfield_age;
    private JTextField textfield_username;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;
    JFrame frame;
    Connection connection = null;
    
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AccountCreationGUI window = new AccountCreationGUI();
                    window.frmToolInventory.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public AccountCreationGUI() {
        initialize();
    }

public void initialize() {
        frmToolInventory = new JFrame();
        frmToolInventory.setResizable(false);
        frmToolInventory.setTitle("Tool Inventory");
        frmToolInventory.getContentPane().setBackground(new Color(0, 51, 51));
        frmToolInventory.setBounds(100, 100, 534, 346);
        frmToolInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmToolInventory.getContentPane().setLayout(null);
        
        JLabel label_firstname = new JLabel("First Name");
        label_firstname.setForeground(new Color(255, 255, 255));
        label_firstname.setBounds(88, 67, 68, 14);
        frmToolInventory.getContentPane().add(label_firstname);
        
        JLabel label_lastname = new JLabel("Last Name");
        label_lastname.setForeground(Color.WHITE);
        label_lastname.setBounds(88, 92, 68, 14);
        frmToolInventory.getContentPane().add(label_lastname);
        
        JLabel label_username = new JLabel("Username");
        label_username.setForeground(Color.WHITE);
        label_username.setBounds(88, 142, 68, 14);
        frmToolInventory.getContentPane().add(label_username);
        
        JLabel label_password = new JLabel("Password");
        label_password.setForeground(Color.WHITE);
        label_password.setBounds(88, 167, 68, 14);
        frmToolInventory.getContentPane().add(label_password);
        
        JLabel label_reconfirmpassword = new JLabel("Re-confirm Password");
        label_reconfirmpassword.setForeground(Color.WHITE);
        label_reconfirmpassword.setBounds(88, 192, 101, 14);
        frmToolInventory.getContentPane().add(label_reconfirmpassword);
        
        JLabel label_age = new JLabel("Age");
        label_age.setForeground(Color.WHITE);
        label_age.setBounds(88, 117, 68, 14);
        frmToolInventory.getContentPane().add(label_age);
        
        textfield_firstname = new JTextField();
        textfield_firstname.setBounds(224, 64, 144, 20);
        frmToolInventory.getContentPane().add(textfield_firstname);
        textfield_firstname.setColumns(10);
        
        textfield_lastname = new JTextField();
        textfield_lastname.setColumns(10);
        textfield_lastname.setBounds(224, 89, 144, 20);
        frmToolInventory.getContentPane().add(textfield_lastname);
        
        textfield_age = new JTextField();
        textfield_age.setColumns(10);
        textfield_age.setBounds(224, 114, 144, 20);
        frmToolInventory.getContentPane().add(textfield_age);
        
        textfield_username = new JTextField();      
        textfield_username.setColumns(10);
        textfield_username.setBounds(224, 139, 144, 20);
        frmToolInventory.getContentPane().add(textfield_username);
        
        passwordField = new JPasswordField();
        passwordField.setBounds(224, 164, 144, 20);
        frmToolInventory.getContentPane().add(passwordField);
        
        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(224, 189, 144, 20);
        frmToolInventory.getContentPane().add(passwordField_1);
        
        JButton button_CreateAccount = new JButton("Create");
        button_CreateAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createAccount();    
            }
        });
        button_CreateAccount.setBounds(224, 220, 68, 23);
        frmToolInventory.getContentPane().add(button_CreateAccount);
        
        JButton btnClear = new JButton("Clear");
        btnClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });
        btnClear.setBounds(302, 220, 68, 23);
        frmToolInventory.getContentPane().add(btnClear);
    }
    }
    
Newb9
  • 1
  • 1
  • 3
    1) We can't tell what `AccountCreationGUI` looks like, and there's the error probably. If you're not adding components to that `JFrame` then, nothing's gonna show up, or maybe you forgot to call `pack()`. Who knows? We can't tell by the code shown here 2) `.setBounds` means that you're usign `null-layout` which I can [demonstrate](https://stackoverflow.com/a/42521097/2180785) it's a really bad idea, as Swing has to deal with different PLAFs, OS, screen sizes and resolutions, so use layout managers instead – Frakcool Apr 23 '21 at 19:04
  • 2
    3) Also it's a bad UX to have multiple windows popping up, instead use Dialogs, see [The use of multiple JFrames, good / bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (bad). 4) If you need / want better answers to your question, please provide a proper [mre] that demonstrates your issue – Frakcool Apr 23 '21 at 19:05
  • Hello, I've updated the post, i really dont know whats the issue here. tyia – Newb9 Apr 24 '21 at 02:04
  • Please see my answer below @Newb9 – Frakcool Apr 26 '21 at 17:09

3 Answers3

2

There are multiple improvements we can make in your code:

  1. setBounds(...) and .setLayout(null) are your worst enemies, Swing has to deal with different OS, PLAFs, screen sizes and resolutions, see: Why is it frowned upon to use a null layout in Swing? for more information. Pixel perfect apps are a nightmare when you present your UI in a different computer than yours, here's an example of what it looks like if you don't follow this advice. Learn the different Layout Managers and use them, you can combine them as well if needed.

  2. Learn how to indent your code correctly, use your IDE for this, Google something like this [Your IDE (IntelliJ / Eclipse / Netbeans / etc)] format code and follow the steps, try to memorize the command so that your code is always readable by anyone.

  3. textfield_firstname, don't use snake_case to name your variables, in Java the naming convention is camelCase:

    • FirstWordUpperCasedClass
    • firstWordLowerCasedVariable
    • firstWordLowerCasedMethod
    • ALL_WORDS_UPPER_CASED_CONSTANT
  4. Using multiple JFrames creates a terrible UX, because each JFrame instance will create its own window in the task bar, and will not prevent you to interact with the other frames, causing confusion to the user as well as opening multiple windows with different sizes isn't a good experience IMO. See The Use of Multiple JFrames: Good or Bad Practice? (bad) for a more in-depth explanation.

  5. Your problem is that in AccountCreationGUI you're extending JFrame for no reason, (let's call this JFrame the frame1), then you create an instance of JFrame in here: JFrame frmToolInventory; and then another one JFrame frame;, so you have 3 JFrames (see the confusion part here? You have to keep track of all the different JFrames), you're adding all the components to frmTollInventory but you're making frame1 visible:

    AccountCreationGUI window = new AccountCreationGUI();
    window.setVisible(true);
    frame.dispose();
    

For this particular case, you don't need to call extends JFrame, and here's a simple program that you can use as a base for your program later on:

import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MultiWindowGUI {
    private JFrame frame;
    private JPanel pane;
    private JButton dialogGUIButton;
    private JButton multipleJFrameButton;
    private ActionListener listener;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MultiWindowGUI()::createAndShowGUI);
    }
    
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName());
        pane = new JPanel();
        dialogGUIButton = new JButton("Dialog (recommended)");
        multipleJFrameButton = new JButton("JFrame (not recommended)");
        
        listener = e -> {
            if (e.getSource().equals(dialogGUIButton)) {
                openDialogGUI();
            } else if (e.getSource().equals(multipleJFrameButton)) {
                openJFrameGUI();
            }
        };
        
        dialogGUIButton.addActionListener(listener);
        multipleJFrameButton.addActionListener(listener);
        
        pane.add(dialogGUIButton);
        pane.add(multipleJFrameButton);
        
        frame.add(pane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    
    private void openDialogGUI() {
        JOptionPane.showMessageDialog(frame, new DialogGUI());
    }
    
    private void openJFrameGUI() {
        new MultiFrameGUI();
    }
}

class DialogGUI extends JPanel {
    private JLabel label;
    
    public DialogGUI() {
        label = new JLabel("A label inside a dialog window");
        
        add(label);
    }
}

class MultiFrameGUI {
    private JFrame frame;
    private JLabel label;
    
    public MultiFrameGUI() {
        frame = new JFrame(getClass().getSimpleName());
        label = new JLabel("A label inside a JFrame window");
        
        frame.add(label);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

I suggest you to click 3-5 times on the second button, then switch to another window and then try to find which JFrame you were using last. See what happens if you switch to another window if you press the first button and come back to your JFrame? See the difference?

Another way to switch views is by using CardLayout, here's an example of how to make use of it.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

For the position, you need to specify a location; or you can use a system default:

window.setLocationByPlatform(true);

https://stackoverflow.com/a/7778050/145976

As for the size, you'll need to set a size, and assuming you're using a layout manager, pack it.

window.setPreferredSize(new Dimension(400, 300));
window.pack();
Charlie
  • 8,530
  • 2
  • 55
  • 53
  • 2
    *"assuming you're using a layout manager"* That code is not, but should be. Then, setting a preferred size is counter productive. It's a guess, whereas `pack()` results in precisely the size needed to display the components. – Andrew Thompson Apr 24 '21 at 02:30
0

I think the button which you created is good check out AccountCreationGUI maybe there is your problem.