-1

I've got this big multiple class application, and i've got alogin system. So after a user logs in the text "Welcome, Guest" should update do "Welcome, username". My method "logininfo" is the one handling it and it uses the method setText(). You can see the method here:

public void loginInfo(String userName) {
    lblWelcomeGuest.setText("Welcome, "+userName);
    System.out.println(lblWelcomeGuest.getText());
}

The GUI shows "Welcome, Guest", while my debug-message prints "Welcome, Fillefixsweden". I have no idea what the problem is, maybe I need to refresh? Please help!

Requested by comments, whole code (without imports):

public class MainGUI extends JFrame{


    private static final long serialVersionUID = 1L;

    LogIn login= new LogIn();
    private JLabel lblWelcomeGuest = new JLabel("Welcome, Guest");
    public String uName;
    private JLabel lblLogIn = new JLabel("Log in");
    LoginUI loginui = new LoginUI();


public void initialize() {

    lblLogIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblLogIn.setForeground(Color.WHITE);
    lblLogIn.setBounds(842, 118, 128, 14);
    lblWelcomeGuest.setBounds(10, 116, 133, 16);
    lblWelcomeGuest.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lblWelcomeGuest.setForeground(Color.WHITE);
    getContentPane().add(lblWelcomeGuest);
    getContentPane().add(lblLogIn);


}

public void loginInfo(String userName) {
    lblWelcomeGuest.setText("Welcome, "+userName);
    lblLogIn.setText("Log out");
    System.out.println(lblWelcomeGuest.getText());


    }


}

Here is the complete Main class:

public class Main {
    MainGUI gui;
    public String username;


public Main() {
    System.out.println("Main constructor");
    gui = new MainGUI();
    gui.initialize();
    gui.setVisible(true);

}

public static void main(String[] args) {
    System.out.println("Main");
    new Main();

}

}

And here is the method that calls loginInfo() (another class):

public void cred(String name, String password){
    if(new SQL().validate(name, password)){
        System.out.println("Logged in");
        CloseFrame();
        new MainGUI().loginInfo(name);
    }else{
        System.out.println("Credentials does not match!");
        System.out.println(name+" "+password);

    }
}
hildred
  • 453
  • 11
  • 29
  • 3
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Apr 11 '15 at 15:38
  • Yes, the lblLogIn is another label. I will delete it from the code. – Fillefixsweden Apr 11 '15 at 15:40
  • 1
    If label.setText() doesn't change what is displayed on the screen, and System.out.println(label.getText()) displays something that is different from what the label displays on the screen, the most probable reason is that the JLabel displayed on the screen is not the same one as the one on which you're calling setText() and getText(). But since you chose not to show us your code, we can't help further. – JB Nizet Apr 11 '15 at 15:41
  • 1
    You never show how loginInfo is being called. Likely you're calling it on a non-displayed MainGUI object. Also, you really want to avoid null layouts and setbounds. – Hovercraft Full Of Eels Apr 11 '15 at 15:48
  • 3
    There's no main method, and the only method changing the text of lblWelcomeGuest is never called in the code you posted. Remove all the irrelevant stuff, and post a complete, minimal, executable example reproducing the problem. – JB Nizet Apr 11 '15 at 15:48

2 Answers2

2

Every time you call new MainGUI() you create a new and distinct instance (object) one that is completely unique from all others. You call this once to create a visible GUI, and then later create another to call your loginInfo() method just as we suspected. Solution -- only create one MainGUI instance and call all pertinent methods off of it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Nevermind, solved it by myself.

I created a new instance of the GUI in another class that covered my affected GUI. Solved by not creating a new instance but using the old one.