Java: How to call GUI class from main class (without Swing/JFrame) [duplicate]

45
June 26, 2017, at 01:11 AM

This question already has an answer here:

  • Java: How do I start a standalone application from the current one when both are in the same package? 3 answers

I am new to Java. I would like to build a restaurant application. In the main class all I want to do is to call GUIs from other classes (and later also methods, but that is not the question). This is the code of the main class (comments are in german):

package Restaurant;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Tisch_Auswahl extends Application {
  // Zutaten für die erste (primary) stage / Auswahlseite
  GridPane pane0 = new GridPane();
  Scene scene0 = new Scene(pane0);
  Button button1 = new Button("Tisch 1");
  Button button2 = new Button("Tisch 2");
  Button button3 = new Button("Tisch 3");

  EventHandler<ActionEvent> Tisch1 = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
  Tisch1.show(); // Zweite Bühne wird angezeigt
  // In diesen EventHandler muss eigentlich Befehl für Abruf der Klasse?
}
  };
  EventHandler<ActionEvent> Tisch2 = new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
      Tisch2.show(); // Zweite Bühne wird angezeigt
      // In diesen EventHandler muss eigentlich Befehl für Abruf der Klasse?
    }
      };
  EventHandler<ActionEvent> Tisch3 = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
          Tisch3.show(); // Zweite Bühne wird angezeigt
          // In diesen EventHandler muss eigentlich Befehl für Abruf der Klasse?
        }
      };

  public void start(Stage primaryStage) throws Exception {
// Erste stage vorbereiten
primaryStage.setScene(scene0);
primaryStage.setTitle("Bestellsystem");
pane0.setPadding(new Insets(10));
pane0.setHgap(20);
pane0.setVgap(20);
pane0.addRow(0, button1, button2, button3);


button1.setOnAction(Tisch1);
button2.setOnAction(Tisch2);
button3.setOnAction(Tisch3);
primaryStage.show();
  }
  public static void main(String[] args) {
launch();
  }
}

In the main class there are three EventHandler for "tisch 1 - 3" (which means table 1 - 3). I created a class for each table, each class is the same. This is the code of the class for table 1:

package Restaurant;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Tisch1 extends Application {
  GridPane pane1 = new GridPane(); 
  Scene szene_1 = new Scene(pane1, 600, 650);
  Label l0 = new Label("Menü");
Label l1 = new Label("Vorspeise");
Label l2 = new Label("Hauptspeise");
Label l3 = new Label("Dessert");
Label l4 = new Label("Getränke");
Label l5 = new Label("Suppe");
Label l6 = new Label("Bruschetta");
Label l7 = new Label("Dip");
Label l8 = new Label("Pide");
Label l9 = new Label("Pizza");
Label l10 = new Label("Pasta");
Label l11 = new Label("Rote Grütze");
Label l12 = new Label("Tiramisu");
Label l13 = new Label("Eis");
Label l14 = new Label("Bier");
Label l15 = new Label("Wasser");
Label l16 = new Label("Wein");
ObservableList<String> Menge1 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge1 = new ComboBox<>(Menge1);
ObservableList<String> Menge2 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge2 = new ComboBox<>(Menge2);
ObservableList<String> Menge3 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge3 = new ComboBox<>(Menge3);
ObservableList<String> Menge4 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge4 = new ComboBox<>(Menge4);
ObservableList<String> Menge5 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge5 = new ComboBox<>(Menge5);
ObservableList<String> Menge6 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge6 = new ComboBox<>(Menge6);
ObservableList<String> Menge7 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge7 = new ComboBox<>(Menge7);
ObservableList<String> Menge8 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge8 = new ComboBox<>(Menge8);
ObservableList<String> Menge9 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge9 = new ComboBox<>(Menge9);
ObservableList<String> Menge10 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge10 = new ComboBox<>(Menge10);
ObservableList<String> Menge11 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge11 = new ComboBox<>(Menge11);
ObservableList<String> Menge12 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge12 = new ComboBox<>(Menge12);
Button b1 = new Button("Nächste Bestellung");
Button b2 = new Button("Rechnung drucken");

  public void start (Stage Tisch1) {
  pane1.add(l1, 0, 1);
  pane1.add(l2, 0, 5);
  pane1.add(l3, 0, 9);
  pane1.add(l4, 0, 13);
  pane1.add(l5, 1, 1);
  pane1.add(l6, 1, 2);
  pane1.add(l7, 1, 3);
  pane1.add(l8, 1, 5);
    pane1.add(l9, 1, 6);
    pane1.add(l10, 1, 7);
    pane1.add(l11, 1, 9);
    pane1.add(l12, 1, 10);
    pane1.add(l13, 1, 11);
    pane1.add(l14, 1, 13);
    pane1.add(l15, 1, 14);
    pane1.add(l16, 1, 15);
    pane1.add(menge1, 2, 1);
    pane1.add(menge2, 2, 2);
    pane1.add(menge3, 2, 3);
    pane1.add(menge4, 2, 5);
    pane1.add(menge5, 2, 6);
    pane1.add(menge6, 2, 7);
    pane1.add(menge7, 2, 9);
    pane1.add(menge8, 2, 10);
    pane1.add(menge9, 2, 11);
    pane1.add(menge10, 2, 13);
    pane1.add(menge11, 2, 14);
    pane1.add(menge12, 2, 15);
    pane1.add(b1, 2, 17);
    pane1.add(b2, 3, 17);
    menge1.getSelectionModel().select("0");
    menge2.getSelectionModel().select("0");
    menge3.getSelectionModel().select("0");
    menge4.getSelectionModel().select("0");
    menge5.getSelectionModel().select("0");
    menge6.getSelectionModel().select("0");
    menge7.getSelectionModel().select("0");
    menge8.getSelectionModel().select("0");
    menge9.getSelectionModel().select("0");
    menge10.getSelectionModel().select("0");
    menge11.getSelectionModel().select("0");
    menge12.getSelectionModel().select("0");
    pane1.setPadding(new Insets(10));
    pane1.setHgap(12);
    pane1.setVgap(12);
    EventHandler<ActionEvent> Nächste_Bestellung = new EventHandler<ActionEvent>() {
          public void handle(ActionEvent event) {
              Tisch1.close();
          }
        };
        EventHandler<ActionEvent> Rechnung_erstellen = new EventHandler<ActionEvent>() {
              public void handle(ActionEvent event) {
              }
            };
        b1.setOnAction(Nächste_Bestellung);
        b2.setOnAction(Rechnung_erstellen);
Tisch1.setScene(szene_1);
Tisch1.setTitle("Tisch 1");
Tisch1.show();
  }
  public static void main(String[] args) {
launch(args);
  }
}

So how can I call the GUI for table 1 in my main class? I found answers using Swing, but I don't want to mix it up. Pleas help me :)

Answer 1

Typical operations with GUI in the code are:

  1. Create and show application window (root container)
  2. Compose a GUI component and add to some container
  3. Subcribe/unsubscribe to GUI events
  4. Change state of GUI component

The first one is usually done within a main application class, probably, the one that contains main method. For the rest it's good idea to follow MVC pattern, in which V stands for View (an instance of JavaFX or Swing class, responsible for rendering and receiving user input), M is your data model (e.g. list of the menu items in restaurant), and C is the controller class, responsible for composing the view, subscribing and handling view events and updating the model. So your code could look like this (in pseudocode, since I'm not familiar with javafx API):

interface Controller {
     Node getView();
}
class TableController {
   ...
}
class MainScreenController implements Controller, ReservationListener {
    private Restaurant model;
    private StackPane view;
    private List<TableController> children;
    public MainScreenController(Restaurant restaurant) {
        view = new StackPane();
        // compose this view from table views for each table
        for (Table table : model.getTables()) {
             TableController controller = new TableController(table);
             view.getChildren().add(controller.getView());
             // when Reserve button is clicked in TableController, we want
             // to receive reservation events here and update restaurant
             // status
             controller.addReservationListener(this);
        }
        // add more components here and subscriptions on events
    }
    // receives reservation events from tables
    public void tableReserved(Table table) { 
        ...
    }
}
class MyApplication extends Application {
   // a model that could be loaded from file or database
   private Restaurant restaurant = new Restaurant(); 
   // a controller for the main view
   private RestaurantController controller = new RestaurantController(restaurant);
   public void start(Stage stage) {
      // here you just initialize the stage with the view
      // from your main controller
      stage.setScene(new Scene(main.getView(), 400, 300));
      // you only need to show the root, the rest is already part of it
      stage.show(); 
   } 
}

`

READ ALSO
suggesting the correct approach with respect to multiple conditions

suggesting the correct approach with respect to multiple conditions

I was advising one of my friend to not to add logger with single if conditionIf there were multiple lines after the if clause then It will treated as sonar critical issue

83
Why wrapper class in Java doesn&#39;t behave like a reference type?

Why wrapper class in Java doesn't behave like a reference type?

I have a huge problem to understand why wrapper class in Java doesn't behave like a reference typeExample:

61
Passing Cucumber Tags/values for JUnit Runner in maven command

Passing Cucumber Tags/values for JUnit Runner in maven command

I am trying to set the cucumber tags from maven command(through command line maven command)I am using Junit runner and cucumber options to specify the run and specify cucumber options(format,features,

98