JavaFX TextField set column-size properties dynamically
import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.input.KeyCode; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class Main extends Application { protected TextField tfTextField = new TextField(); protected TextField tfColumnSize = new TextField(); @Override// www .ja v a 2 s.c o m public void start(Stage primaryStage) { // Set properties for text fields tfTextField.setText("JavaFX"); tfTextField.setAlignment(Pos.BOTTOM_CENTER); tfColumnSize.setAlignment(Pos.BOTTOM_RIGHT); tfColumnSize.setPrefColumnCount(3); tfTextField.setPrefColumnCount(12); tfColumnSize.setText("12"); // Create three radio buttons RadioButton rbLeft = new RadioButton("Left"); RadioButton rbCenter = new RadioButton("Center"); RadioButton rbRight = new RadioButton("Right"); rbCenter.setSelected(true); // Create a toggle group ToggleGroup group = new ToggleGroup(); rbLeft.setToggleGroup(group); rbCenter.setToggleGroup(group); rbRight.setToggleGroup(group); // Create four hbox HBox paneForRadioButtons = new HBox(5); paneForRadioButtons.getChildren().addAll(rbLeft, rbCenter, rbRight); paneForRadioButtons.setAlignment(Pos.BOTTOM_LEFT); HBox paneForColumnSize = new HBox(5); paneForColumnSize.getChildren().addAll( new Label("Colum Size"), tfColumnSize); HBox paneForTextField = new HBox(5); paneForTextField.setAlignment(Pos.CENTER); paneForTextField.getChildren().addAll( new Label("Text Field"), tfTextField); HBox hbox = new HBox(10); hbox.getChildren().addAll(paneForRadioButtons, paneForColumnSize); // Create a vBox and place nodes in it VBox pane = new VBox(10); pane.setPadding(new Insets(5, 5, 5, 5)); pane.getChildren().addAll(paneForTextField, hbox); // Create and register the handlers rbLeft.setOnAction(e -> { if (rbLeft.isSelected()) { tfTextField.setAlignment(Pos.BOTTOM_LEFT); } }); rbCenter.setOnAction(e -> { if (rbCenter.isSelected()) { tfTextField.setAlignment(Pos.BOTTOM_CENTER); } }); rbRight.setOnAction(e -> { if (rbRight.isSelected()) { tfTextField.setAlignment(Pos.BOTTOM_RIGHT); } }); tfColumnSize.setOnKeyPressed(e -> { if (e.getCode() == KeyCode.ENTER) { tfTextField.setPrefColumnCount(Integer.parseInt( tfColumnSize.getText())); } }); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("java2s.com"); primaryStage.setScene(scene); primaryStage.show(); } }