0

I'm using the Java GUI for the first time through Netbeans. I'm trying to design some basic hotel software, and for that I'm trying to implement multiple screens through multiple JFrames.

What I don't know is how to get the jFrame I'm using to go invisible and another jFrame to go visible when a button in my current jFrame is clicked.

I have a class, Hotel with this code:

public static void main(String[] args) {
    // TODO code application logic here
    HotelMain h = new HotelMain();
    HotelBooking b = new HotelBooking();
    h.setVisible(true);
}

HotelMain and HotelBooking are 2 separate jFrame files. I'm trying to get HotelMain to disappear and HotelBooking to appear when a button is clicked on the HotelMain screen.

And this is some code from the main function in HotelMain. Should this even be there? Also, how do I use this to get the jFrame to disappear?

java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HotelMain().setVisible(true);
            }
        }
splungebob
  • 5,357
  • 2
  • 22
  • 45
  • 1
    Don't do this as it's a bad design decision. How may professional gui's do you see that throw windows at users? Not many as it is very annoying for the user. Instead swap views using a CardLayout. – Hovercraft Full Of Eels Sep 16 '14 at 13:50
  • @HovercraftFullOfEels How do I do that? Also, this isn't professional at all, haha, I'm just trying to get used to this, never used it before. – Rohil Verma Sep 16 '14 at 13:52
  • 1
    Please read [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice), and even if it's not professional, you don't want to annoy your instructor, or mark your program as one created by an obvious newbie. Please google the Swing CardLayout tutorial as it will explain all. – Hovercraft Full Of Eels Sep 16 '14 at 13:53
  • @HovercraftFullOfEels Thanks, I will. But I'm trying to make my title screen disappear forever, so how would I do that? – Rohil Verma Sep 16 '14 at 13:57
  • Simply call `setVisible(false)` on the original window and `setVisible(true)` on the new one. Nothing to it. – Hovercraft Full Of Eels Sep 16 '14 at 13:57
  • @HovercraftFullOfEels Yup, I figured as much. The problem with that is that I create the instance of this jFrame in my main class Hotel. As far as I can see, the only way to react to button presses is through methods in the HotelMain file, and so I have no idea how to access the object from its own class. – Rohil Verma Sep 16 '14 at 14:02
  • You access the object of the current class through either `this`, or if you're trying to access it from within an anonymous inner class, then you would pre-pend `this` with the class name, `Hotel.this`. – Hovercraft Full Of Eels Sep 16 '14 at 15:21

0 Answers0