-1

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);

    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
dongdong
  • 51
  • 5
  • Why do you believe that [`java.time.Duration`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#method.summary) has a `millis()` method? The **documentation**, i.e. the javadoc, doesn't list one. – Andreas Mar 11 '19 at 17:02
  • 5
    @Andreas because s/he should use the fx version of Duration ... :) – kleopatra Mar 11 '19 at 17:03
  • 5
    You are using the wong `Duration` class. Have a look on your import and use this [one](https://docs.oracle.com/javase/8/javafx/api/javafx/util/Duration.html). Aside note : using swing in the fx application thread is a terrible idea. – Pagbo Mar 11 '19 at 17:03
  • @kleopatra omg this works! since im new to this Timeline things, I just click whatever the NetBeans suggest me to import, didn't realize that it suggest the wrong. Thanks so much!! :) – dongdong Mar 12 '19 at 00:20

1 Answers1

4

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;
dongdong
  • 51
  • 5