0

sorta new to Javafx. I'm running into a problem when in my controller class for my main stage, Im trying to: after clicking on a button, start a delay that will then open a new stage.

The problem comes to when loading the FXML file within the button action event and in the run() part of the Scheduler that I'm using for the delay. I can't throw the exception in the run() because it clashes with itself?

And I can't catch it either in the Parent Root by doing :

   Parent root = null;
                try {
                    root =       FXMLLoader.load(getClass().getResource("Popup.fxml"));
                } catch (IOException e) {
                    e.printStackTrace();
                }

because it just flat out won't run. I think Im approaching this poorly or just the wrong way entirely.

I can't seem to get this to work. The stage runs fine just on button click but I need the delay.

Code for Controller Class:

public class Controller implements Initializable {

public static int seconds;
public static boolean yes = false;
public String strtime = String.format("Current Date/Time : %tc", new Date());

@FXML
Button remind;
@FXML
Label secondsuntil,date;
@FXML
TextField Day, Minute, remindname;
@FXML
private Button Butt;
static String Nameoftask;
int days, minutes;
@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub
    date.setText(strtime);
}



@FXML
private void handleButtonAction(ActionEvent actionEvent) throws IOException {
    final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    ScheduledFuture<?> countdown = scheduler.schedule(new Runnable() {
        @Override
        public void run(){

            Stage primaryStage = new Stage();
            Parent root = FXMLLoader.load(getClass().getResource("Popup.fxml"));
            primaryStage.setTitle(Nameoftask);
            primaryStage.initModality(Modality.NONE);
            primaryStage.setScene(new Scene(root, 200,50));
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            double width = screenSize.getWidth();
            double height = screenSize.getHeight();
            primaryStage.setX(width);enter code here
            primaryStage.setY(height*-1);
            primaryStage.show();


            try {
                FXMLLoader loader = new
                        FXMLLoader(getClass().getResource("Popup.fxml"));
                loader.setController(new Controller());

                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }}, 1, TimeUnit.SECONDS);


}

}

  • Look [here](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task) at the [`PauseTransition`](https://docs.oracle.com/javase/8/javafx/api/javafx/animation/PauseTransition.html) solution. Remove the `wait.playFromStart();` method and place the code to load your stage there. Set the time to want you need. – SedJ601 Nov 11 '18 at 17:28
  • 1
    Thank you so much! It works perfectly. – Thatguysomewhere Nov 11 '18 at 21:08

0 Answers0