1

Im searching for an example how to deal with idle times in javafx and found this perfect answer Idle time in javafx. But how i can add this in my Controller class because i create all on a Fxml class and call this like:

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

so my question is how to get it work with register idle monitor and how i create the idle monitor

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


}}

Edit:

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

}

Community
  • 1
  • 1
tbere
  • 105
  • 1
  • 10
  • Why is it any different to the post you linked? Just register that idle monitor with whichever node is the root of the scene in the `initialize()` method. – James_D Apr 11 '17 at 11:26
  • hmm ok that is what i tried but its not working because i dont get the scene – tbere Apr 11 '17 at 13:10
  • Register it with the root node instead. If you want to register it with the scene, you have to do that from the place you create the scene. – James_D Apr 11 '17 at 13:10
  • ok that works thanks. But now i have this void initialize() throws IOException { IdleMonitor idleMonitor = new IdleMonitor(Duration.seconds(3), () -> { anch.getChildren().clear(); anch.getChildren().add(new Button("test")); }, true); idleMonitor.register(anch, Event.ANY); } so where i should load the fxml file? – tbere Apr 11 '17 at 13:16
  • 1
    I don't understand that last question. You would load the FXML file wherever you were previously loading the FXML file: this makes no difference to that at all. – James_D Apr 11 '17 at 13:17
  • Ok im not that familiar with javafx so im loading the first instance of the view in the main File where the Application has its start method there i load the first fxml file and register it at the scene. Now i want back to that scene and i load the fxml file in the controller exactly the same as in the Main class i updated the question with the Main.java file – tbere Apr 11 '17 at 13:22
  • Perfect its working pretty good, i add a node and set the view in the controller. – tbere Apr 13 '17 at 09:37

0 Answers0