Datepicker in Processing

Hello my friends,
I need to include in my project a Datepicker, but it seems that no exist nothing about this.
I found an exemple in java:

import java.util.Locale;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
 
public class DatePickerSample extends Application {
 
    private Stage stage;
    private DatePicker checkInDatePicker;
    public static void main(String[] args) {
        Locale.setDefault(Locale.US);
        launch(args);
    }
 
    @Override
    public void start(Stage stage) {
        this.stage = stage;
        stage.setTitle("DatePickerSample ");
        initUI();
        stage.show();
    }
 
    private void initUI() {
        VBox vbox = new VBox(20);
        vbox.setStyle("-fx-padding: 10;");
        Scene scene = new Scene(vbox, 400, 400);
        stage.setScene(scene);

        checkInDatePicker = new DatePicker();

        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        Label checkInlabel = new Label("Check-In Date:");
        gridPane.add(checkInlabel, 0, 0);

        GridPane.setHalignment(checkInlabel, HPos.LEFT);
        gridPane.add(checkInDatePicker, 0, 1);
        vbox.getChildren().add(gridPane);
    }
}

Is it possible to adapt it for processing?
Thanks,
Santy

Hello, I have tried next:

import java.util.Locale;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
private Stage stage;
private DatePicker checkInDatePicker;
 
void setup(){
  Locale.setDefault(Locale.US);
  launch(args);  
}
 
void start(Stage stage) {
    this.stage = stage;
    stage.setTitle("DatePickerSample ");
    initUI();
    stage.show();
}

void initUI() {
   VBox vbox = new VBox(20);
   vbox.setStyle("-fx-padding: 10;");
   Scene scene = new Scene(vbox, 400, 400);
   stage.setScene(scene);
   checkInDatePicker = new DatePicker();
   GridPane gridPane = new GridPane();
   gridPane.setHgap(10);
   gridPane.setVgap(10);
   Label checkInlabel = new Label("Check-In Date:");
   gridPane.add(checkInlabel, 0, 0);
   GridPane.setHalignment(checkInlabel, HPos.LEFT);
   gridPane.add(checkInDatePicker, 0, 1);
   vbox.getChildren().add(gridPane);
 }

but say: NullPointerException and remark this line: launch(args);
and no more messages. I don´t know.

This will not run in Processing unfortunately as Processing is managing the display for you aka. Processing is doing lots of the heavy job in the background. There are no hooks to attach this code to the sketch. What I am going to show next is running your code using plain java.

For your first code that has a main, follow the next steps:

  1. Create an empty folder and name it anything you want.

  2. Then create a file called DatePickerSample.java and place the above code there.

  3. Open a cmd window and run these two commands:

        javac DatePickerSample.java
        java DatePickerSample

Kf

datepicker0


datepicker1

Thanks very much for your reply, kfrajer. I’ll have to find another solution.
Best regards,
Santy.

Is it immoral to show dangerous code? :slight_smile:

I could not resist the temptation of doing this, even if it’s very wrong. I never used FX2D before.

import java.util.Locale;
import java.lang.reflect.*;
import javafx.scene.layout.StackPane;
import javafx.scene.canvas.*;
import javafx.scene.control.DatePicker;
import javafx.stage.Stage;
import processing.javafx.*;

Stage stage;
DatePicker datePicker;

//http://www.java2s.com/Tutorials/Java/JavaFX/0540__JavaFX_DatePicker.htm

void setup() {
  size(400, 400, FX2D); // only works with FX2D
  Locale.setDefault(Locale.US);

  try {
    // Make private property accessible (bad)
    // https://stackoverflow.com/questions/1555658/
    // We need access to stage to mix JavaFX components with P5
    Field field = PSurfaceFX.class.getDeclaredField("stage");
    field.setAccessible(true);
    stage = (Stage)field.get(surface);
    
    // I know root is a StackPane from looking at PSurfaceFX.java
    StackPane stackPane  = (StackPane)stage.getScene().getRoot();
    
    datePicker = new DatePicker();
    datePicker.setRotate(5);
    datePicker.setTranslateY(-100);
    stackPane.getChildren().add(datePicker);

    stage.show();
  } 
  catch(Exception e) {
    e.printStackTrace();
  }
}

void draw() {
  stroke(random(256));
  line(random(width), random(height), width/2, height/2); 
}

datepicker

But don’t do it for any kind of serious project, because what the code above does is accessing private properties that should never be accessed, and those can change any time Processing is updated.

Instead, why not improve controlP5 to add a date picker control? I recently did my own date picker in C++ from scratch and it’s a fun exercise.

Update: thinking about it, it would be just a few lines of code in the Processing source to enable something like this. Not sure if it’s valuable or if it aligns with its goals though. Maybe it can be discussed.

2 Likes

wow, hamoid is fantastic!!!
I love the inmoral things, jajajaja

The project no is to much serious, will be a open project.
I made a Weather Station with arduino (temp, humidity, pression At., intensity and direction of the wind ) and processing captures the data by Serial and save it in a .txt
I have a blog where I explain all de the process (is in spanish) you can search by: santyArduino Excuse the publicity, please. But isn´t finished, I’m buiding it yet.
Now I’m working with the querys, you will can select one day, one month o the year.
I want to post the bbdd on the web and the application to make queries.
I show you a screenshot, Attached image.

I don’t know improve control IP5 sorry, but you are closer than me.
Convert your C ++ project to Processing, please, sure it would be valuable.
I could give you a big hug!!!

by the way, how the date variable is stored with your immoral sketch?

Best regards,
Santy.

Captura

vatuadell! :wink: Maybe in a few weeks I have time to port the calendar. About the calendar above:

// the list of required imports
import javafx.beans.value.*;
import java.time.LocalDate;
import java.util.Locale;
import java.lang.reflect.*;
import javafx.scene.layout.StackPane;
import javafx.scene.canvas.*;
import javafx.scene.control.DatePicker;
import javafx.stage.Stage;
import processing.javafx.*;

    // add this after constructing datePicker:
    datePicker.valueProperty().addListener(new ChangeListener<LocalDate>() {
      @Override
        public void changed(ObservableValue<? extends LocalDate> observable, LocalDate oldValue, LocalDate newValue) {
        println(newValue, 
          newValue.getDayOfMonth(), 
          newValue.getMonthValue(), 
          newValue.getYear());
      }
    }
    );

In more modern versions of java this ugly addListener would become much shorter and readable (for instance if you are writing Processing inside Idea or Eclipse).

I found the solution searching for “javafx datepicker addlistener”.

1 Like

vatuadell aixo es Fantastic, jajajaja.
I wait impatiently.

To make this post a bit more useful for others, I will try to explain what JavaFX does.

As I understand it, JavaFX was first developed by Sun Microsystems, then Oracle, and it seems like in the future it will be maintained by the community as OpenJFX. It seems to offer many configurable components to make it easy to create GUIs, style them and add visual effects. JavaFX is supposed to replace Swing. I think the Processing IDE uses Swing components (buttons, drop down menus, text boxes, etc).

There is a program called Scene Builder which lets you design interfaces for your programs using javaFX / openJFX:

Processing has a graphics mode that internally uses javaFX, and it’s triggered like this: size(400, 400, FX2D);. I haven’t really tried it, but it’s supposed to be very fast for 2D graphics. Using FX2D does not make it easy to use javaFX features like GUI components or visual effects though. Recently @lqdev showed at A way to add fancy effects to your sketches . In my answer about the Datepicker I showed how to tap into the Processing internals to get the stage element, which you would need to draw javaFX GUI components.

Would it make sense to give easy access to the javaFX features from Processing? Personally, I don’t know, since it is incompatible with P2D and P3D. Maybe it would be nice to open 2 windows, one with javaFX in which you can create a beautiful GUI, and another with P2D / P3D in which you render your graphics. I can imagine the preferred option is to leave javaFX entirely for more advanced users who can figure out how to combine it with Processing, or?

4 Likes

Yes Hamoid, I think it’s a good option to open a nice new window with javaFX in which I can create a beautiful GUI.
Thank you so much. :grin:

Hi hamoid,
I’m searching for a possibility to integrate a fxml-file in Processing to get a GUI.
The fxml-file is created with scene builder and I have an example to include it in a “normal” Java-IDE, but I need to integrate it in processing.
Do you or anybody else here in the forum have an idea how I can realize that?
Many thanks!

HI @em_sha :slight_smile: I’m sorry but I haven’t worked with fxml files myself. I hope someone else can help you!

Hi hamoid,
I got an answer in another topic: Fxml-file integrate in processing?