Search code examples
javaswingsystem.out

java swing it doesn't without system.out.println


Hello so i started learning java a week back and i basically started making a gui just to see how things work and i found a weird "bug" or i don't exactly understand how things work and it's not even a bug

i have a class called startPanel that makes a panel that is visible from the start and it asks you as to what you wish to log in admin,user or a guest this is startPanel:

package library;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

/*
 * this panel is responsible for the first opening panel and redirects you to your panel
 * 
 * 
 */
import javax.swing.*;
public class startPanel extends JFrame {


boolean adminState=false;
boolean userState=false;
boolean guestState=false;

JButton adminBut,userBut,guestBut ;

//start of constructor
public startPanel(){
    //frame size,close when pressing x,title,and spawn at middle of the screen
    this.setSize(500,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Welcome guest");
    this.setLocationRelativeTo(null);
    //making the panel
    JPanel panel1 = new JPanel();
    //making a label to fill things up it doesn't really do anything
    JLabel startLabel = new JLabel("you wan't to log in as...");
    //3 buttons for the user to click 1 only and the according frame will show up
    adminBut = new JButton("Admin");
    userBut = new JButton("User");
    guestBut = new JButton("Guest");
    //making an event handler for admin only so far just for test purposes
     ListenForButton lForButton = new ListenForButton();
     adminBut.addActionListener(lForButton);

     //adding comps to the panel
    panel1.add(startLabel);
    panel1.add(adminBut);
    panel1.add(userBut);
    panel1.add(guestBut);

    //adding the panel to the frame
    this.add(panel1);

} // end of startPanel constructor

private class ListenForButton implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        /*probably not the correct way to do what i want to but just figured this might work
         *it only works for admin button if the user presses the adminBut
         *it will change the states and with a getter we can change each state 
         *from main accordingly
         */
        if (event.getSource() == adminBut ){
            adminState=true;
            guestState=false;
            userState= false;

        }
    }

} // end of Listen for button

//all getters for the states
public boolean getAdminState(){
    return adminState;
}
public boolean getUserState(){
    return guestState;
}
public boolean getGuestState(){
    return userState;
}

}

this is main :

package library;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class mainLibrary {
public static void main(String[] args) {


    adminPanel adminP = new adminPanel();
    userPanel userP = new userPanel();
    startPanel gui = new startPanel();
    gui.setVisible(true);

    while(true){
        System.out.println(gui.getAdminState());
        if (gui.getAdminState() == true) {
            gui.setVisible(false);
            userP.setVisible(true);

        }
    }

the problem now is that if i remove System.out.println(gui.getAdminState()); this does not work it doesn't even get in the if at all if it's false at start if i don't remove it works correctly :/ so what is going on

this is adminPanel for the adminPanel if it matters

package library;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class adminPanel extends JFrame {

    //start of adminPanel constructor
public adminPanel(){ 
    //frame size,close when pressing x,title,and spawn at middle of the screen
    this.setSize(500,500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Admin panel area");
    this.setLocationRelativeTo(null);

    JPanel panel1 = new JPanel();

    this.add(panel1);


    } //end of admin constructor

}

Solution

  • This is not a good GUI design. Never use such active loop.

    Remarks: use standard naming convention (uppercase/lowercase), and never call a frame a panel (this is too confusing).

    A better design would be to have references to Panels to be activated in the startPanel and setting appropriate property reacting to button actions. Something like:

    class StartFrame extends JFrame implements ActionListener {
        private JFrame adminFrame;
        private JFrame userFrame;
        ...
    
        // add construtor to initialize adminFrame and userFrame appropriately
        ...
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == adminBut) {
                this.setVisible(false);
                adminFrame.setVisible(true);
            }
            if (e.getSource() == userBut) {
                this.setVisible(false);
                userFrame.setVisible(true);
            }
        }
    }