Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rewrite of Chapter 16 ButtonDemo Program
package chapter16;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class ButtonDemo3 extends Application {
private Text text = new Text(50, 50, "JavaFX Programming");
@Override
public void start(Stage primaryStage) {
BorderPane appPane = new BorderPane();
appPane.setBottom(new ButtonPane());
appPane.setCenter(new TextPane());
appPane.setLeft(new RadioButtonPane());
appPane.setRight(new CheckBoxPane());
appPane.setTop(new TextSetterPane());
Scene scene = new Scene(appPane, 450, 200);
primaryStage.setTitle("Button Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private class ButtonPane extends HBox {
public ButtonPane() {
this.setSpacing(20);
this.setAlignment(Pos.CENTER);
this.setStyle("-fx-border-color: green");
Button btLeft = new Button("Left", new ImageView("image/left.gif"));
Button btRight = new Button("Right", new ImageView("image/right.gif"));
btRight.setContentDisplay(ContentDisplay.RIGHT);
btLeft.setOnAction(e -> text.setX(text.getX() - 10));
btRight.setOnAction(e -> text.setX(text.getX() + 10));
this.getChildren().addAll(btLeft, btRight);
}
}
private class TextPane extends Pane {
public TextPane() {
this.getChildren().add(text);
}
}
private class RadioButtonPane extends VBox {
public RadioButtonPane() {
this.setSpacing(20);
this.setPadding(new Insets(5, 5, 5, 5));
this.setStyle("-fx-border-width: 2px; -fx-border-color: green");
RadioButton rbRed = new RadioButton("Red");
RadioButton rbGreen = new RadioButton("Green");
RadioButton rbBlue = new RadioButton("Blue");
this.getChildren().addAll(rbRed, rbGreen, rbBlue);
ToggleGroup group = new ToggleGroup();
rbRed.setToggleGroup(group);
rbGreen.setToggleGroup(group);
rbBlue.setToggleGroup(group);
rbRed.setOnAction(e -> {
if (rbRed.isSelected()) {
text.setFill(Color.RED);
}
});
rbGreen.setOnAction(e -> {
if (rbGreen.isSelected()) {
text.setFill(Color.GREEN);
}
});
rbBlue.setOnAction(e -> {
if (rbBlue.isSelected()) {
text.setFill(Color.BLUE);
}
});
}
}
private class CheckBoxPane extends VBox {
public CheckBoxPane() {
this.setSpacing(20);
this.setPadding(new Insets(5, 5, 5, 5));
this.setStyle("-fx-border-color: green");
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);
CheckBox chkBold = new CheckBox("Bold");
CheckBox chkItalic = new CheckBox("Italic");
this.getChildren().addAll(chkBold, chkItalic);
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);
}
}
class TextSetterPane extends BorderPane {
public TextSetterPane() {
this.setPadding(new Insets(5, 5, 5, 5));
this.setStyle("-fx-border-color: green");
this.setLeft(new Label("Enter a new message: "));
TextField tf = new TextField();
tf.setAlignment(Pos.CENTER_LEFT);
this.setCenter(tf);
tf.setOnAction(e -> text.setText(tf.getText()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.