Duration.millis symbol not found?

1.7k Views Asked by At

I'm new to JavaFX, especially the built in API, which is the Timeline

But I wanna add a timer for an event using Timeline

                Timeline timeline;
                timeline = new Timeline(new KeyFrame(Duration.millis(2500),
                        (evt) -> {
                            System.out.println("hello");
                        }));
                timeline.setCycleCount(Animation.INDEFINITE);
                timeline.play();

error: cannot find symbol Duration.millis(2500), symbol: method millis(int) location: class Duration 1 error

This is the full code of it, just wanna test out the timeline thing to create a timer for an event.

In this case, I try to add a simple println "Hello" to see if it works, but it doesn't.

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.scene.image.Image;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.ResourceBundle;
import java.util.TimerTask;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javax.swing.*;

public class Scene2Controller implements Initializable  {

@FXML
private JFXButton btn_submit;
@FXML
private JFXTextField txt_email;
@FXML
private JFXPasswordField txt_pass;
@FXML
private ImageView img1;
@FXML
private ImageView img2;
@FXML
private Pane pane_loader;

@FXML
private void handleButtonAction(ActionEvent event) throws IOException, Exception {
    if (event.getSource() == btn_submit) {
        if (txt_email.getText().equals("") && txt_pass.getText().equals("")) 
        {
            JOptionPane.showMessageDialog(null, "Please fill in the field", "Error", JOptionPane.ERROR_MESSAGE);
        } 
        else 
        {
            pane_loader.setVisible(true);
            pane_loader.toFront();
            if (txt_email.getText().equals("root") && txt_pass.getText().equals("test1234")) 
            {

                Timeline timeline;
                timeline = new Timeline(new KeyFrame(Duration.millis(2500l),
                        (evt) -> {
                            System.out.println("hello");
                        }));
                timeline.setCycleCount(Animation.INDEFINITE);
                timeline.play();
            }                          

            else 
                JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", JOptionPane.ERROR_MESSAGE);
        }

    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    Image image = new Image ("/image/business_care_clinic_1282308.jpg");
    Image image2 = new Image ("/image/815.gif");
    img1.setImage(image);
    img2.setImage(image2);

    }
}
1

There are 1 best solutions below

0
On

Huge thanks to @kleopatra and @Pagbo , I just realised that I've imported the wrong class. my bad.. :)

import java.time.Duration;

to

import javafx.util.Duration;