-2

I'm new to swing, and I'm trying to implement a simple GUI with one form (HospitalGUI.form). When I run the associated Java file (HospitalGUI.java) as main, the form is visible.

I want to be able to launch it from my controller so that either the GUI or a console interface can be selected. When my controller instantiates it, the code in the constructor is executed, but the form doesn't open.

Is there anything special or additional that needs to be done for a form to be opened by another Object?

I would greatly appreciate any help understanding this.

My GUI's main is

public static void main(String[] args) {

    final int FRAME_WIDTH = 300;
    final int FRAME_HEIGHT = 400;

    JFrame frame = new JFrame("Hospital System");
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setContentPane(new HospitalGUI().rootPanel);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

and the call in the controller is

HospitalGUI hospitalInterface = new HospitalGUI();
  • 2
    Possibly add: `hospitalInterface.setVisible(true);` but how can we truly answer this question without guessing? Please let's prevent this -- give us a decent [mcve] with your question, and please read the [ask]. – Hovercraft Full Of Eels Jun 23 '18 at 02:21

1 Answers1

1

When I run the associated Java file (HospitalGUI.java) as main, the form is visible.

That is because you main() method creates the HospitalGUI class and a frame for the panel. Then you add the panel to the frame and make the frame visible.

and the call in the controller is

HospitalGUI hospitalInterface = new HospitalGUI();

When you just use the above statement that means the constructor of your class still essentilly needs to invoke all the code from the main() method to create a frame and add the panel to the frame and make the frame visible.

Since you didn't post all that code we can't guess what you are missing.

So in reality you need to change your design. You need two classes:

  1. one class to create the panel you want to add to the frame. So you can add the panel to the frame created in the main() method.
  2. the controller class. This class will be responsible for creating the frame and adding the panel to the frame.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • That makes sense, though I'm not sure how to create the frame and add the panel to it from within the controller class. As a first attempt, I tried copying the code from my GUI's main to my controller and making the GUI's panel public. The code executed fine, but it still didn't produce anything. – Allan McPherson Jun 23 '18 at 12:23
  • Or have I misunderstood? Are you saying I should have a controller class and a class that creates the panel in addition to the HospitalGUI class that I currently have? – Allan McPherson Jun 23 '18 at 12:23
  • From what I can see you have a single class "HospitalGUI". Then you have a variable `rootPanel`, which is an instance variable of this class. So yes I am suggestion you need two classes. The first would be "HospitalPanel". So in your main() method you create an instance of this class and add it the frame you create. The second HospitalGUI is just a wrapper class that creates a JFrame and then creates an instance of the HospitalPanel class to add to the frame. Whatever you do you need to 1) create a JFrame and 2) create the panel you add to the JFrame. – camickr Jun 23 '18 at 14:00
  • Thank you. I had thought that I had done that in my previous attempts, but I just tried again, and now it works. – Allan McPherson Jun 24 '18 at 00:37