1

I have a main jFrame HomePage.java and many other classes which extend javax.swing.JInternalFrame. One of the classes (JInternalFrame) is Login.java. Now I check whether the password is correct in Login.java and if the password is correct, I load success.java. Now the problem arises when I have to load the success.java page.

I have a function in the HomePage.java whose purpose is to remove all internal frames and load success.java class. The function is as follows :-

public void LogIn(){
    jDesktopPane1.removeAll();
    System.out.println("Pos 2");
    success frame = new success();
    frame.setVisible(true);
    jDesktopPane1.add(frame);
    setContentPane(jDesktopPane1);
    try {
        frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {
    }
}

I call the function in the Login.java page as follows :-

System.out.println("Pos 1 ");
(new HomePage()).LogIn();

The logic does not work. But, I receive the output as

Pos 1
Pos 2

which shows that the program flow was correct. I do not get any error also. Another weird factor is that when I calthe same LogIn() function from the menu in the jFrame itself, I get the desired output. Please solve my dilemma. Any help will surely be appreciated!

1 Answer 1

2

This code:

System.out.println("Pos 1 ");
(new HomePage()).LogIn();

Creates a new HomePage object and calls a method on it. Oh you are certainly calling the correct method, and it's going a HomePage instance, but it's not the correct HomePage instance, not the one that's displayed, because you've created a new one.

Short term solution: get a valid reference to the displayed HomePage instance and call your method on it. You could get it by passing it into the class that needs it as a constructor or method parameter, or you could use SwingUtilities.getWindowAncestor(...).

i.e.,

Window window = SwingUtilities.getWindowAncestor(Login.this);
if (window instanceof HomePage) {
  HomePage validHomePageRef = (HomePage) window;
  validHomePageRef.logIn();
}

Long term solution: don't create spaghetti code with GUI objects changing behaviors of other GUI objects, but instead refactor your code a la MVC or one of its variants, so the GUI can change the model and the other GUI objects can be notified of these changes via listeners and respond if they want.

3
  • +1 for SwingUtilities. But how would one "validly refer" to the HomePage instance
    – h2O
    Jan 31, 2014 at 17:40
  • @h2O: His HomePage object appears to be the JFrame that holds all the other parts of his GUI. If he gets that window, casts it to a HomePage variable, he has access. Jan 31, 2014 at 17:41
  • @puZZlr: you're welcome, but I have to say, it's a bad design. Also you really need to learn and follow Java naming conventions including giving methods and variable names that start with lower-case letters and classes names that start with upper case letters. Jan 31, 2014 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.