等待对属性更改进行反应JavaFX 8 [英] Wait before Reacting to a Property Change JavaFX 8

查看:64
本文介绍了等待对属性更改进行反应JavaFX 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以让您继续监听属性更改几秒钟,然后触发一个事件(调用方法)?

Is there a way to keep listening to a property change, for a few seconds, then fire an event (call a method)?

例如,当用户在文本字段中输入数据时:

For example, when the user enter data in a text field:

textField.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> arg0, String arg1, String arg2) {
        //before calling a method to do something.. wait for a few seconds ...
        }
    }); 

一种情况是基于字符串值触发操作.例如,按"M"表示移动,或按"MA"表示蒙版.我想先保持2秒钟,然后再进行操作.

A scenario would be firing an action based on the string value. For example, hitting "M" for move, or "MA" for mask. I would like to "keep listening" for 2 seconds before making an action.

推荐答案

有几种解决方法.

通常,我建议使用 Java Timer API ,但是如果通过执行操作"暗示要更新FX线程上的内容,则必须同步线程,这很麻烦.

Usually I would recommend the Java Timer API, but if by "making an action" you imply updating stuff on the FX thread, you would have to synchronize the threads, which is bothersome.

根据您的用例,您可以在FX中使用转场或时间轴.

Depending on your use case, you could instead use Transitions or the Timeline in FX.

这是一个带有过渡的示例:

Here is an example with a transition:

打包申请;

import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TransitionedInputExample extends Application {
  @Override
  public void start(final Stage stage) {
    final ImageView spinner =
        new ImageView("https://cdn1.iconfinder.com/data/icons/duesseldorf/16/process.png");
    spinner.setVisible(false);

    final Label title = new Label("Timed Action Commander Example", spinner);
    title.setContentDisplay(ContentDisplay.BOTTOM);
    title.setFont(Font.font("Helvetica", FontWeight.BOLD, FontPosture.REGULAR, 16));

    final TextField textInput = new TextField();
    textInput.setPromptText("Enter command");

    final TextArea textOutput = new TextArea();
    textOutput.setPromptText("Command results will show up here");

    final VBox layout = new VBox(title, textInput, textOutput);
    layout.setSpacing(24);

    // setup some transition that rotates an icon for 2 seconds
    final RotateTransition rotateTransition = new RotateTransition(Duration.seconds(1), spinner);
    rotateTransition.setByAngle(90);
    // delay rotation so that user can type without being distracted at once
    rotateTransition.setDelay(Duration.seconds(1));

    // restart transition on user input
    textInput.textProperty().addListener((observable, oldText, newText) -> {
      spinner.setVisible(true);
      rotateTransition.playFromStart();
    });

    rotateTransition.setOnFinished((finishHim) -> {
      // execute command
        textOutput.setText("Executing " + textInput.getText());
        spinner.setVisible(false);
      });


    final Scene scene = new Scene(layout);
    stage.setScene(scene);
    stage.show();
  }

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

有关使用时间轴的解决方案,请参见这篇文章.

For a solutition using a Timeline see this post .

这篇关于等待对属性更改进行反应JavaFX 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆