如何检测Javafx中的空闲时间

时间:2017-04-11 09:45:03

标签: javafx javafx-8 idle-processing

我正在寻找一个如何在javafx中处理空闲时间的例子,并找到了这个完美答案Idle time in javafx。 但是我如何在我的Controller类中添加它,因为我在Fxml类上创建了所有并调用它:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));

所以我的问题是如何使用寄存器空闲监视器以及如何创建空闲监视器

public class LoginController {

static final Logger logger = Logger.getLogger(Controller.class.getName());

@FXML
private ResourceBundle resources;

@FXML
private URL location;

@FXML
private AnchorPane anch;

@FXML
private TextField machineNumberField;

@FXML
private Label label;

private Label labelwrong = new Label();

private static Integer machineNumber;

public static Integer getMachineNumber() {
    return machineNumber;
}

public static void setMachineNumber(Integer machineNumber) {
    LoginController.machineNumber = machineNumber;
}

@FXML
void initialize() {}

@FXML
void onEnterMachineNumber(ActionEvent event) throws IOException {
    PlatformHelper platformHelper = new PlatformHelper();
    String machineNumber = machineNumberField.getText();

    if (!machineNumber.isEmpty() && machineNumber.contains("$")) {
        DbConnection dbConnection = new DbConnection();
        try {
            dbConnection.Connect();
            FileReaderWriter fileReaderWriter = new FileReaderWriter("thread");
            if(fileReaderWriter.isCacheFileNotEmpty()) {
                fileReaderWriter.copyCacheFile();
                fileReaderWriter.flushStorageFile(null);
                fileReaderWriter.start();
            }
        } catch (SQLException e) {
            logger.error("SQL Exception " + machineNumberField.getText() + "\n" + e.getMessage());
        }
        try {
            String machinenr = machineNumber.split("\\$")[1];
            setMachineNumber(Integer.parseInt(machinenr));
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
            Parent root = fxmlLoader.load();
            Controller controller = fxmlLoader.getController();
            controller.setMachineNumber(getMachineNumber());

            anch.getChildren().clear();
            anch.getChildren().add(root);
        } catch (NumberFormatException e) {
            platformHelper.displayWarning(anch, labelwrong, "wrong Barcode");
            machineNumberField.clear();
            logger.info("Wrong Barcode format exception " + machineNumberField.getText() + "\n" + e.getMessage());
        }

    } else {
        platformHelper.displayWarning(anch, labelwrong, "wrong Barcode");
        machineNumberField.clear();
        logger.info("Wrong Barcode format exception " + machineNumberField.getText());
    }


}}

编辑:

Main.java

public class Main extends Application {

Scene loginScene;

@Override
public void start(Stage primaryStage) throws Exception {
    StackPane root = new StackPane();

    Parent login = FXMLLoader.load(getClass().getResource("/login.fxml"));
    primaryStage.setTitle("Timer for process time");

    loginScene = new Scene(login);
    primaryStage.setScene(loginScene);
    primaryStage.setFullScreen(true);
    primaryStage.show();



    setUserAgentStylesheet(STYLESHEET_MODENA);


    new FileReaderWriter("setPath").setPathandCacheFile();
}


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

}

0 个答案:

没有答案