0

My question is how to display more than one user interface screen per program. I'm sure this question has been asked before, but I haven't found a solution that works for me (or should I say that I understand). There isn't anything exotic about the scenarios I'm talking about. The first is simply validating inputs from a screen and re-displaying the same screen in the case of errors.

I'll pose the question in terms of the second more complicated scenario: displaying an input data screen, processing the inputs; and then displaying the outputs. This complicated somewhat by the fact that the first, a simple screen with 5 text boxes and a command button, uses an FXML file, whereas the second, a multi-select list box does not. The flow is:

1. Main program calls

2. A loader program which loads the FXML and somehow or another calls

3. A controller which receives the inputs and processes them to produce output.

The final step is to display the output in the form of a multi-select list box. Note that the first GUI employs a controller, which is a separate file, to process the inputs, whereas the second uses an event handler, which is in the same file as the screen definition, to make the selection(s) when the user clicks a command button.

Various SO posts have said that the way to go is to not shut down the application once the first GUI has completed via but Keep the JavaFX run time going in the background with

Platform.setImplicitExit(false);

and to define each GUI and simply switch scenes to the one you want to display. But where, given the scenario I described do you put the code? The second GUI has three pieces: screen definition, event handler(s), and scene switching code. Where do you put each? #2 or #3. If you put some in #2 and some in #3, how does #3 know what you did in #2?

The code for #2 the FMXL loader:

    public class inputData extends Application { 
    public static void load() {       
        launch();
    }
    @Override
    public void start(Stage stage) throws Exception {
                    
        GridPane inpRoot = FXMLLoader.load(getClass().getResource("inputData.fxml"));
        Scene inpScene = new Scene(inpRoot, 300, 275);
        
        stage.setTitle("Amsnag 2.1 - Query Input");
        stage.setScene(inpScene);
        stage.show();
    }   
}

Code for #3, the list box definition and handlers, which worked fine running separately. It's only when I tried to incorporate it with the rest of the program that it failed.

public class multiList extends Application { 
    public static void load() {       
        launch();  
    }
    public static final ObservableList options = FXCollections.observableArrayList();
    
    @Override
        public void start(final Stage stage) {    
        final ListView<String> listView = new ListView<>();         
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
// load list from DB
        Connection conn = sql.connect();
        try {
    // initialize option table
            ResultSet rs = sql.select(conn, 
                "select distinct connDesc,accom from option order by connDEsc,accom");
            while (rs.next()) {
                String opt = rs.getString("connDesc") + ": " + rs.getString("accom");
                listView.getItems().add(opt);
            }                                                                    
            conn.close();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage()+ " from init");
        }
// button to display fares
        final Button displayButton = new Button("Display Fares");
// handle button click
        displayButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent event) {
            Platform.exit();   // close list box
            ObservableList selectedIndices = listView.getSelectionModel().getSelectedItems();
// lcreate temp table with selected options
            Connection conn = sql.connect();
            try {
// initialize option table
                ResultSet rs = sql.select(conn, 
                    "create temporary table selected (connDesc varchar(200),accom varchar(50))");
                for(Object o : selectedIndices){
                   String option = o.toString();
// extract connDesc+accom from displayed option
                   msg.g(option);
                }        
                conn.close();
            }
            catch (SQLException e) {
               System.out.println(e.getMessage()+ " from init");
            }         
        }
    }    );   // end of display handler
// quit  button
    final Button resetButton = new Button("Quit");
    resetButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
        public void handle(ActionEvent event) {
            Platform.exit();
        }
    });
    final HBox controls = new HBox(10);
    controls.setAlignment(Pos.CENTER);
    controls.getChildren().addAll(displayButton, resetButton);
 
    final VBox layout = new VBox(10);
    layout.setAlignment(Pos.CENTER);
    layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
    layout.getChildren().setAll(listView, controls);
    layout.setPrefWidth(320);``enter code here
    
    Scene scene = new Scene(layout);
 //   stage.setScene(new Scene(layout));
    stage.setScene(scene);
    stage.setTitle("Select one or more options");
    stage.show();
  }  
  public static void main(String[] args) { launch(args); }
}
3
  • 1
    You should only have one Application subclass in your application. It's not really clear what you're trying to do with two such classes. Maybe stackoverflow.com/questions/32464698/… helps?
    – James_D
    Jun 26, 2017 at 16:07
  • Right. I didn't make it clear that the second set of code, the multiList class, was a stand alone version that worked. What I'm trying to do is display two GUI's.
    – Paul M
    Jun 27, 2017 at 5:05
  • You can't reuse an Application subclass in another application. Factor the part you want to reuse into a separate class. This is identical to the question I linked above.
    – James_D
    Jun 27, 2017 at 10:35

1 Answer 1

0

You can't reuse an Application subclass in a different application.

The Application class represents an entire application, or perhaps more specifically its lifecycle. So it has methods such as init(), start(), and stop() which are invoked by the FX Application Toolkit at the appropriate moments in the lifecycle of the application.

The layout for your multiList (aside: please use proper naming conventions) class is performed in the start() method, so it can only happen at the start of the application. By putting the layout code here, you make it impossible to reuse so that it is performed at a later point in a different application.

So move the layout for MultiList to a separate class:

public class MultiList  { 

    public static final ObservableList options = FXCollections.observableArrayList();

    private final VBox view ;

    public MultiList() {    
        final ListView<String> listView = new ListView<>();         
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        // load list from DB
        Connection conn = sql.connect();
        try {
            // initialize option table
            ResultSet rs = sql.select(conn, 
                "select distinct connDesc,accom from option order by connDEsc,accom");
            while (rs.next()) {
                String opt = rs.getString("connDesc") + ": " + rs.getString("accom");
                listView.getItems().add(opt);
            }                                                                    
            conn.close();
        }
        catch (SQLException e) {
            System.out.println(e.getMessage()+ " from init");
        }
        // button to display fares
        final Button displayButton = new Button("Display Fares");
            // handle button click
            displayButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent event) {
                Platform.exit();   // close list box
                ObservableList selectedIndices = listView.getSelectionModel().getSelectedItems();
                // create temp table with selected options
                Connection conn = sql.connect();
                try {
                    // initialize option table
                    ResultSet rs = sql.select(conn, 
                        "create temporary table selected (connDesc varchar(200),accom varchar(50))");
                    for(Object o : selectedIndices){
                       String option = o.toString();
    // extract connDesc+accom from displayed option
                       msg.g(option);
                    }        
                    conn.close();
                } catch (SQLException e) {
                   System.out.println(e.getMessage()+ " from init");
                }         
            }
        });   // end of display handler
        // quit  button
        final Button resetButton = new Button("Quit");
        resetButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
            public void handle(ActionEvent event) {
                Platform.exit();
            }
        });
        final HBox controls = new HBox(10);
        controls.setAlignment(Pos.CENTER);
        controls.getChildren().addAll(displayButton, resetButton);

        view = new VBox(10);
        view.setAlignment(Pos.CENTER);
        view.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
        view.getChildren().setAll(listView, controls);
        view.setPrefWidth(320);


    }  

    public Parent getView() {
        return view ;
    }

}

Now if you want to test this out on its own, you can write an application for it:

public class MultiListApp extends Application {

    @Override
    public void start(Stage primaryStage) {
        MultiList multiList = new MultiList() ;
        Scene scene = new Scene(multiList.getView());
        primaryStage.setScene(scene);
        primarStage.setTitle("Select one or more options");
        primaryStage.show();
    }
}

Or in the controller class for InputData.fxml, you can do the same thing:

public class InputDataController {

    @FXML
    private void someEventHandler() {
        MultiList multiList = new MultiList() ;
        Scene scene = new Scene(multiList.getView());
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("Select one or more options");
        stage.show();
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.