I have started using JavaFX to create a window for user interaction, to be used in another, non-JavaFX, program.
My main program is called the Abc
class, having a main method. This is a non-JavaFX program, but plain vanilla Java. This program runs some activities, then asks the user to select a String
from a list of possible Strings
. This user interaction is done with a JavaFX program called MenuSelector
. The user selects one String
, which will then be returned to Abc
for further processing.
MenuSelector
is made with Netbeans and Scene Builder, which uses three files: FXMLDocument.fxml
, FXMLDocumentController.java
, and MenuSelector.java
. Handling of the selection process is done in FXMLDocumentController.java
. The MenuSelector.java
class only defines the Stage
and Scene
.
What I can not find online is instructions for how Abc
would start MenuSelector
. And can not find instructions how the resulting String
in FXMLDocumentController
can be passed back to Abc
. What steps should I take to get this up and running?
Edit:
I was asked about the reason for having things this way. It was suggested that I am having a design problem.
My current implementation of the MenuSelector
is with using javax.swing
. This is hand-coded in one java class called MenuSelector
, having a top level method which can be called from other programs (e.g. Abc
, Def
, ..). This MenuSelector
is a supporting piece of software which can be used by multiple programs. Each program can submit a list of Strings to the menu, out of which the user can select one String
, which will then be returned to the program which called this MenuSelector
.
The MenuSelector
therefore does not have a main method, only a top-level method which can be called by others. MenuSelector
is a supporting program, and is not supposed to be the highest level.
My attempt is to replace the hand-coded javax.swing
version of MenuSelector
by a JavaFX version. But I can only do this if I can input a list of Strings as input to the menu, and return a single String as result.
Edit, to explain the JavaFX structure as generated by Netbeans:
When generating a new Netbeans JavaFX project it comes with three files: FXMLDocument.fxml
, FXMLDocumentController.java
, and MenuSelectorFX.java
. After building the GUI with Scene Builder and creating the control software the contents of these three are:
FXMLDocument.fxml
(not manually modified by me):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="menuselector.FXMLDocumentController">
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="320.0" spacing="20.0">
<children>
<ChoiceBox fx:id="InstrumentChoiceBox" prefWidth="150.0" />
<Button mnemonicParsing="false" onAction="#onChoiceMade" text="This One" />
<Label fx:id="SelectionLabel" text="Label" />
</children>
</VBox>
</children>
</AnchorPane>
FXMLDocumentController.java
(code added by me):
package menuselector;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML private ChoiceBox InstrumentChoiceBox;
@FXML private Label SelectionLabel;
private String SelectedInstrument;
public String getSelectedInstrument(){
return SelectedInstrument;
}
public void onChoiceMade(){
SelectedInstrument = InstrumentChoiceBox.getSelectionModel().getSelectedItem().toString();
SelectionLabel.setText("Instrument selected: "+SelectedInstrument);
}
private String[] determineCandidates(){
//method to determine the list of candidates, abbreviated
//Reads in a number of Strings from file and converts to String[]
return Result;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
String[] Candidates = determineCandidates();
SelectedInstrument = "";
for(int i = 0; i < Candidates.length;i++) InstrumentChoiceBox.getItems().add(Candidates[i]);
InstrumentChoiceBox.setValue(Candidates[0]);
}
}
and finally MenuSelectorFX
(not manually modified by me):
package menuselector;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MenuSelectorFX extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Until now have I been able to call the MenuSelector from my non-JavaFX class Abc
(or Def
) by using the lines:
MenuSelectorFX msfx = new MenuSelectorFX();
msfx.main(new String[0]);
This starts the GUI with the choice box, and when I select an option the chosen option is displayed in the GUI. What I have not yet been able to achieve is the return of parameter SelectedInstrument
in FXMLDocumentController
to Abc
.