1

My issue currently is i have hide my jframe1 by using this.setVisible(false) and call for jframe2. but how do i go to the same jframe1 again? i can set jframe1.setVisible(true) but this will call a new jframe1.

The previous jframe1 i hide have mysql data i pull from login form. Now you see, my issue is if i set jframe1 to setVisible(true) from jframe2 it will actually run new jframe1 and all previous data it have wih login form before will lost.

for your information my jframe1 have 2 override class, jframe1() and jframe1(String getUsername, String getPassword). i am using jframe1(String getUsername, String getPassword) to call jframe1 from login form.

Example Situation from jframe1(String getUsername, String getPassword)

//button to move from jframe1 to jframe2   (at jframe1)
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         

jframe2 frame2 = new jframe2();
this.setVisible(false); // i hide jframe1(String , String) and call jframe2
                        // i use this.setVissible(false) because i dont know how to put
                        // jframe1(2 parameter) with setVisible().

frame2.setVisible(true);// call jframe2

} 

unhide and call jframe1(String, String) again from jframe2

 private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          

     jframe1 frame1 = new jframe1(); //i have no idea how create jframe1 instance 
                                     //with 2 parameter, perhap this is the reason
                                     // i failed to call previous jframe1

     mf.setVisible(true);  // unhide and call jframe1, unfortunately it will
                           // create new jframe1 form, i have no idea how to 
                           // call/unhide the previous jframe1

     this.setVisible(false);  //hide jframe2
Ridzuan Adris
  • 1,192
  • 2
  • 13
  • 32
  • 1
    if this post is confusing, my actual question is how do you call setVisible(true) jframe class with 2 parameter? – Ridzuan Adris Apr 02 '14 at 06:30
  • 1
    By the sounds of it you want some sort of WindowManager class that contains each of the JFrame instances and can be called to hide and unhide them. Alternatively, have the JFrame instances as variables within the class contain the actions, that way, you'll be able to get hold of the instances to hide and unhide them – Dan Temple Apr 02 '14 at 06:41
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Apr 02 '14 at 06:48

1 Answers1

2

Im not sure if that is, what you are looking for. It sounds like, you want to be able to restore a hidden (frame1.setVisible(false);) window from within another class.

To do that, you have to provide access to a reference to your frame1. Then you are able to restore the the window as is.

Your main window:

public class Window1 extends JFrame {             
    private final String username;
    private final String password;


    public Window1 (final String username, final String password) {
        this.username = username;
        this.password = password;

        // do initial stuff for you frame here

        this.setVisible(true);
    }

    // have to implement actual button action listener here
    private onButtonClick() {
        final Window2 = new Window2(this);

        this.setVisible(false);           
    }

}

Your second window (maybe a dialog or something?):

public class Window2 extends JFrame {
    private final JFrame firstWindow;

    public Window2(final JFrame firstWindow) {
        if (firstWindow == null)
            throw new IllegalArgumentException("No main window specified");

        this.firstWindow = firstWindow;

        // do initial stuff for your temp window here

        this.setVisible(true);
    }


    // have to implement actual button action listener here
    private onButtonClick() {
        firstWindow.setVisible(true);
        this.dispose();
    }
}
ifloop
  • 8,079
  • 2
  • 26
  • 35
  • 1
    yes you are right, i am calling jframe2 from another class. thank you for a quick reply. currently i am analyzing your code. ill update to you if i success or having question.. – Ridzuan Adris Apr 02 '14 at 07:02
  • 1
    when i put private final JFrame firstWindow as window2 variable, it display "variable firstWindow might not be initialized", what is actually my mistake? – Ridzuan Adris Apr 02 '14 at 07:17
  • 1
    In what line does this warning occur? At `this.firstWindow = firstWindow`? – ifloop Apr 02 '14 at 07:19
  • when i declare private final JFrame firstWindow, the error occur on public Window2(final JFrame firstWindow); – Ridzuan Adris Apr 02 '14 at 07:22
  • 2
    Have you any other errors/warnings in your code, that my cause this to happen? Actually it should be good the way I wrote it. Anyway, I added a check agains `null` in the constructor. Edited my post above. This may fix the warning. – ifloop Apr 02 '14 at 07:26
  • actually i cannot made firstWindow into JFrame variable, because if i do that, public window2(parameter) will display error "variable firstWindow might not be initialized" will occur, anyway i suspect this is because window2 class cannot detect window1 jframe at the first place. – Ridzuan Adris Apr 02 '14 at 07:48
  • 1
    Yes you are good guy @ifLoop, Thanks you for your help. I am now able to return to the previous frame. – Ridzuan Adris Apr 02 '14 at 08:16