1

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 :)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
A.Belle
  • 19
  • 1
  • You must follow Java naming convention and start variable and field names with lower-case letters. Since you don't do that yet, there's at least one compile error in your code at this moment: the names of the event handler instances clash with the class names, which makes invocations of show() methods impossible. – Ivan Gammel Jun 25 '17 at 13:01

1 Answers1

0

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(); 
   } 
}

`

Ivan Gammel
  • 652
  • 7
  • 17