Javafx fetching string from a datepicker.





import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class FetchingFromDatePicker extends Application
{
    public static void main(String[]args)
    {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage)
    {
        BorderPane bp = new BorderPane();
        Scene scene = new Scene(bp,600,400);
        
        //creating the datepicker
        DatePicker dateP = new DatePicker();
        dateP.setPrefWidth(200);//creating the width of the datepicker
        dateP.setPrefHeight(20);//creating the height of the datepicker
        
        //creating button
        Button btn = new Button("print");
        //creating the flowpane
        FlowPane fp = new FlowPane();
        fp.setHgap(10);//setting the horizontal gap between elements in the flowpane
        fp.getChildren().addAll(dateP,btn);//ading the datepicker and button to flowpane
        fp.setAlignment(Pos.CENTER);
        bp.setCenter(fp);
        
        //on button click trigger an action.In this case echo the input from the datepicker
        btn.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent t)
            {
                String date = ((TextField)dateP.getEditor()).getText();//fetching input from date picker and assigning it to string date.
                System.out.println(date);
            }
        });
        primaryStage.setTitle("Date Picker");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Comments

Popular posts from this blog

Javafx adding icons to menu and menuitem.

Javafx displaying image from database.

Javafx inserting uploaded image into database.