In this lab, we learn how to create GUI components for applications using JavaFX.
To create an application using JavaFX, we will need a number of pieces. There will often be a Model, View, Controller, and Application. Through these three applications, you will see why all these are separated and how it is all connected.
For our first application we will create a tip calculator. We will include all of the details with multiple places where you copy and paste to create your application.
TipApp
package tip; import javafx.application.Application; import javafx.stage.Stage; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; public class TipApp extends Application { @Override public void start(Stage primaryStage) { try { FXMLLoader loader = new FXMLLoader(); BorderPane root = (BorderPane)loader.load(getClass().getResource("TipGUI.fxml").openStream()); primaryStage.setScene(new Scene(root)); primaryStage.show(); root.requestFocus(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public static void main(String[] args) { launch(args); } }
Now, we need to add in the View, describing how the GUI will look on the screen and the components available to the program.
TipGUI
Editing the raw XML document can be extremely tedious. Luckily, there is a extension called SceneBuilder that you can use to graphically add components.
TipGUI.fxml
AnchorPane
BorderPane
VBox
HBox
Label
TextField
ChoiceBox
Your GUI should look like this
Now, save the GUI and close SceneBuilder.
We would like to tip based on three perceived levels of service. This is an excellent opportunity for an Enum. Create a new Enum called Service within the tip package. Add the following code:
Service
tip
package tip; public enum Service { POOR { @Override public double tip() { return 0.1; } }, GOOD { @Override public double tip() { return 0.15; } }, EXCELLENT { @Override public double tip() { return 0.2; } }; abstract public double tip(); public double computeTotal(double bill) { return (1.0 + tip()) * bill; } }
Now create a new class called TipController. This gives us a way to respond to the clicks and keys pressed when the program is running.
TipController
The initialize method in a Controller is similar to a constructor in other classes. It will be the place where objects are created and initial state is set up.
initialize
Also notice the calculate method, which will compute the tip amount based on the selected options in the app.
calculate
Add the following code:
package tip; import javafx.fxml.FXML; import javafx.scene.control.ChoiceBox; import javafx.scene.control.TextField; public class TipController { @FXML private TextField bill; @FXML private ChoiceBox<Service> tips; @FXML private TextField total; @FXML public void initialize() { bill.setEditable(true); total.setEditable(false); for (Service s: Service.values()) { tips.getItems().add(s); } tips.getSelectionModel().select(Service.GOOD); } @FXML public void calculate() { double billEntered = Double.parseDouble(bill.getText()); Service level = tips.getSelectionModel().getSelectedItem(); double totalAmount = level.computeTotal(billEntered); total.setText(String.format("%7.2f", totalAmount)); } }
Finally, we need a button we can click that will calculate the tip according to our choices. In this step we are setting up the wiring between the View and Controller portions of the app. Open up TipGUI.fxml again in SceneBuilder.
Button
On the left side, go to Document → Controller. Select tip.TipController in the drop-down menu.
tip.TipController
Save the GUI in SceneBuilder and close SceneBuilder.
Run Tip and test the program.
Now, edit the program and interface as follows:
Now, start from scratch and use the information above to test out your new JavaFX skills. Create a new project using JavaFX to make a program that records how many times a button has been clicked.
Create a new Java project and package for your application. Copy/paste the template Application code above and alter it for your new project in a class called ClickerApp.
ClickerApp
Create a FXML document. In your GUI, your project should include at a minimum a BorderPane with a Button and a Label, created through SceneBuilder. You might need some HBox or VBox pieces to organize your GUI.
SceneBuilder
You GUI could look like this for example:
Create a Controller class. The controller should
int
Run the GUI and test your program.
The previous two apps introduced the Controller, View, and Application, but noticeably there was no Model. For this third project, we will build an application around our TootsiePop class from earlier in the semester.
TootsiePop
Create a new Java project and package for your application. Copy/paste the template Application code above and alter it for your new project in a class called TootsiePopApp.
TootsiePopApp
Create a FXML document. In your GUI, use SceneBuilder to include at a minimum a BorderPane with two Buttons and a visual representation of a TootsiePop created with a Circle and a Rectangle. You might need some HBox or VBox pieces to organize your GUI, and an AnchorPane could be helpful for designing the TootsiePop visuals.
The two Buttons should be labeled New and Bite.
Buttons
New
Bite
Create a Flavor Enum. You should define at least five flavors, each of which has a different Color accessible through a getColor() method.
Flavor
getColor()
Revise your TootsiePop code from HW1 in the following ways to prepare it for use in the GUI application.
String
getLicks
getFlavor
The remaining methods should stay the same.
onTap()
Circle
onNew()
onBite()
updateView()
setFill
Color