1 Reply Latest reply: Oct 7, 2004 6:50 PM by 794342 RSS

    calling a frame from another frame

    807587
      Background: : I have two frame/classes. A login screen and a "display results" screen. A login screen has a login button which takes you to display results screen.
      Display eesults screen as a logout button however...

      Problem: when you click on the logout button, it generates two instances of the login screen. In other words, two login screens appear one on top of the other on the left hand corner NOT in the center like the first time the program launches.

      Behavior I want: once I click logout on "display results" screen, I go right away to the login screen. Exactly the way it previously was: centered and with only one instance running not two as it is right now.

      This is on the login screen.
       void loginbtn_actionPerformed(ActionEvent e)
        {
      
      //if login successful, you will get the results screen
         Display results = new Display();
         results.setVisible(true);
         results.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setVisible(false);
      
        }
      this is on the results screen
        void loginOutbtn_actionPerformed(ActionEvent e)
        {
         //go back to login screen.
          Welcome loginScreen = new Welcome();
          loginScreen.setVisible(true);
          loginScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setVisible(false);
      
        }
      thanks in advance....
        • 1. Re: calling a frame from another frame
          794342
          you are creating a new instance

          void loginOutbtn_actionPerformed(ActionEvent e)
          {
          //go back to login screen.
          Welcome loginScreen = new Welcome();//<-----------------------------------
          loginScreen.setVisible(true);
          loginScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setVisible(false);
          }

          you need to pass a reference to the original 'Welcome'

          something like this

          Display results = new Display(this);

          and in you Display class
          class Display
          {
            Welcome originalScreen;
            public Display(Welcome w)
            {
              originalScreen = w;
              ...
            }
            
            void loginOutbtn_actionPerformed(ActionEvent e)
            {
              //go back to login screen.
              originalScreen.setVisible(true);
              this.setVisible(false);//probably should be this.dispose(), otherwise same problem if login again
            }
          }