JAVA_HA16.0: UI Controls Demo JavaFX provides many UI controls for developing a
ID: 3912706 • Letter: J
Question
JAVA_HA16.0: UI Controls Demo
JavaFX provides many UI controls for developing a comprehensive user interface. Write a program (UIControlsDemo.java) that uses a variety of JavaFX UI controls including button, checkbox, radio button, and text field. The code is combined and modified of the following source code files from the required textbook (Liang, 2015, 10thedition):
LISTING 16.2, ButtonDemo.java, pages 633-634
LISTING 16.3, CheckBoxDemo.java, pages 635-636
LISTING 16.4, RadioButton.java, pages 638-639
LISTING 16.5, TextFieldDemo.java, pages 639-640
When the code is ran a box will pop up with three buttons on the left side to choose Red Green or Blue. Two check boxs on the right Bold and Italic. It will have and "Enter a new message:" and a place to put the message on the top. In the middle it will display the message that was typed. On the bottom it will have a Left and Right Button that are also Gifs that were place in the a image folder that was made under the src folder.
Explanation / Answer
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
public class UIControlsDemo {
public class ButtonDemo extends Application {
protected Text text = new Text(50, 50, "JavaFX Programming");
protected BorderPane getPane() {
HBox paneForButtons = new HBox(20);
Button btLeft = new Button("Left",
new ImageView("image/left.gif"));
Button btRight = new Button("Right",
new ImageView("image/right.gif"));
paneForButtons.getChildren().addAll(btLeft, btRight);
paneForButtons.setAlignment(Pos.CENTER);
paneForButtons.setStyle("-fx-border-color: green");
BorderPane pane = new BorderPane();
pane.setBottom(paneForButtons);
Pane paneForText = new Pane();
paneForText.getChildren().add(text);
pane.setCenter(paneForText);
btLeft.setOnAction(e -> text.setX(text.getX() - 10));
btRight.setOnAction(e -> text.setX(text.getX() + 10));
return pane;
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a scene and place it in the stage
Scene scene = new Scene(getPane(), 450, 200);
primaryStage.setTitle("ButtonDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public class CheckBoxDemo extends ButtonDemo {
@Override // Override the getPane() method in the super class
protected BorderPane getPane() {
BorderPane pane = super.getPane();
Font fontBoldItalic = Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.ITALIC, 20);
Font fontBold = Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.REGULAR, 20);
Font fontItalic = Font.font("Times New Roman",
FontWeight.NORMAL, FontPosture.ITALIC, 20);
Font fontNormal = Font.font("Times New Roman",
FontWeight.NORMAL, FontPosture.REGULAR, 20);
text.setFont(fontNormal);
VBox paneForCheckBoxes = new VBox(20);
paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5));
paneForCheckBoxes.setStyle("-fx-border-color: green");
CheckBox chkBold = new CheckBox("Bold");
CheckBox chkItalic = new CheckBox("Italic");
paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic);
pane.setRight(paneForCheckBoxes);
EventHandler<ActionEvent> handler = e -> {
if (chkBold.isSelected() && chkItalic.isSelected()) {
text.setFont(fontBoldItalic); // Both check boxes checked
}
else if (chkBold.isSelected()) {
text.setFont(fontBold); // The Bold check box checked
}
else if (chkItalic.isSelected()) {
text.setFont(fontItalic); // The Italic check box checked
}
else {
text.setFont(fontNormal); // Both check boxes unchecked
}
};
chkBold.setOnAction(handler);
chkItalic.setOnAction(handler);
return pane; // Return a new pane
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.