Friday 4 December 2020

JavaFX: TextField widget

TextField class is used to create a TextField widget.

 

Can a TextField has support to multi line input?

No, TextField support single line input. If you want multi line support, you should use TextArea.

 

TextField class provides following constructors to define TextField instance.

 

public TextField()

public TextField(String text)

'text' specifies a string for text content.

 

Find the below working application.

 

textFieldDemo.fxml

 

<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>

<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.TextFieldController"
	fx:id="container" spacing="20" alignment="center"
	style="-fx-background-color:white">

	<HBox fx:id="titleHBox">
		<Label fx:id="title" text="Please enter a number" textFill="blue" 
			wrapText="true" 
		/>
		
		<TextField fx:id="textField" />
		
	</HBox>
	
	<HBox fx:id="buttonHBox">
		<Button fx:id="squareButton" text="square"/> 
		<Button fx:id="cubeButton" text="cube"/> 
	</HBox>
	
	<HBox fx:id="resultHBox">
		<Label fx:id="result" textFill="blue" 
			underline="true"
			wrapText="true" 
		/>
	</HBox>
	
</VBox>

 

TextFieldController.java

package com.sample.app.widgets.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;

public class TextFieldController implements Initializable {

	@FXML
	private VBox container;

	@FXML
	private HBox titleHBox;

	@FXML
	private HBox buttonHBox;

	@FXML
	private HBox resultHBox;
	
	@FXML
	private Button squareButton;
	
	@FXML
	private Button cubeButton;
	
	@FXML
	private TextField textField;
	
	@FXML
	private Label result;

	@FXML
	private Label title;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {

		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);

		container.setEffect(dropShadow);
		
		textField.setMinHeight(25);
		textField.setMaxWidth(100);
		
		titleHBox.setSpacing(15);
		titleHBox.setPadding(new Insets(10, 50, 10, 50));
		resultHBox.setPadding(new Insets(10, 50, 10, 50));
		
		buttonHBox.setSpacing(15);
		buttonHBox.setPadding(new Insets(10, 50, 10, 50));
		
		result.setFont(Font.font("Arial", FontPosture.REGULAR, 20));
		title.setFont(Font.font("Arial", FontPosture.REGULAR, 20));
		squareButton.setStyle("-fx-font-size:20");
		cubeButton.setStyle("-fx-font-size:20");

		squareButton.setOnAction((event) -> {
			String data = textField.getText();

			try {
				int dataNum = Integer.valueOf(data);

				result.setText("Result : " + (dataNum * dataNum));
			} catch (Exception e) {
				result.setText("Please Enter Valid integer");
			}
		});

		cubeButton.setOnAction((event) -> {
			String data = textField.getText();

			try {
				int dataNum = Integer.valueOf(data);

				result.setText("Result : " + (dataNum * dataNum * dataNum));
			} catch (Exception e) {
				result.setText("Please Enter Valid integer");
			}
		});

	}
}

 

TextFieldDemo.java

package com.sample.app.widgets;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TextFieldDemo extends Application {

	private static final String FXML_FILE = "/textFieldDemo.fxml";
	private static final String STAGE_TITLE = "Text Field Demo";

	public static void main(String args[]) {
		launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 400, 400, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();

	}

}

 

Output

 


Enter a number and click on square button to calculate square of the number and click on cube button to calculate cube of the number.

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment