0

I am new in Java FX. I expect to close my JavaFX application if the user is inactive for a period of time. In other words App is closed automatically if there are no any mouse event or Key event in for duration It's likely Sleep Mode of Window

I did try the code from Auto close JavaFX application due to innactivity. However My Program doesn't work

I get an example from https://www.callicoder.com/javafx-fxml-form-gui-tutorial/ . And I edited on RegistrationFormApplication Class

public class RegistrationFormApplication extends Application {
 private Timeline timer;
 Parent root ;
@Override
public void start(Stage primaryStage) throws Exception{
     timer = new Timeline(new KeyFrame(Duration.seconds(3600), new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            // TODO Auto-generated method stub
            root = null;
            try {
                root = FXMLLoader.load(getClass().getResource("/example/registration_form.fxml"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                primaryStage.setTitle("Registration Form FXML Application");
                primaryStage.setScene(new Scene(root, 800, 500));
                primaryStage.show();        

        }
     }));

     timer.setCycleCount(Timeline.INDEFINITE);
     timer.play();

     root.addEventFilter(MouseEvent.ANY, new EventHandler<Event>() {
         @Override
         public void handle(Event event) {
             timer.playFromStart();
         }
     });

Thanks for help

ELIP
  • 311
  • 6
  • 19
  • 1
    Use @fabian answer from the link you shared. – SedJ601 Sep 09 '19 at 06:06
  • 1
    I don't see where you call `Platform.exit()`. Your code looks like it just keeps trying to load the FXML file. – SedJ601 Sep 09 '19 at 06:08
  • I used code which @fabian wrote .However The program was closed by timer regardless of mouse event or key pressed. That I did not expect. I spent much time to looking for somewhere on Internet But It seems this matter rarely is posted – ELIP Sep 09 '19 at 06:21
  • 2
    please read both comments from @Sedrick carefully, do as suggested and if you have problems with fabian's solution, come back with an example (as [mcve]) that really uses it :) And repeating for emphasis: why are you re-loading the content over and over again? – kleopatra Sep 09 '19 at 07:41
  • 1
    The code you've posted will yield an exception during the execution of the `start` method, since `root` is `null` when you call `addEventFilter` (unless you're hiding some code in a constructor/initializer or the `init` method)... Furthermore there more less complex things you could do in the event handler (`System.out.println`/`Platform.exit`) and the time of the keyframe is also not well-suited for testing. It would be a good idea to make finding/reproducing he issue as simple as possible for us. – fabian Sep 09 '19 at 16:48

1 Answers1

3

Get RxJavaFx and run the code. After 4 seconds of inactivity (lack of any events) it will close the app.

    import java.util.concurrent.TimeUnit;

    import io.reactivex.Observable;
    import io.reactivex.schedulers.Schedulers;
    import io.reactivex.subjects.PublishSubject;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.input.InputEvent;
    import javafx.stage.Stage;
    import javafx.stage.WindowEvent;

    public class CloseAfterApp extends Application {


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

        @Override
        public void start(Stage stage) throws Exception {
            Scene scene = new Scene(new TextField());

            PublishSubject<InputEvent> sceneEventPublishable = PublishSubject.create();
            PublishSubject<WindowEvent> windowEventPublishable = PublishSubject.create();

            scene.addEventFilter(InputEvent.ANY, sceneEventPublishable::onNext);
            stage.addEventFilter(WindowEvent.ANY, windowEventPublishable::onNext);

            Observable.merge(sceneEventPublishable, windowEventPublishable)
            .switchMap(event -> Observable.just(event).delay(4, TimeUnit.SECONDS, Schedulers.single()))
            .subscribe(event -> Platform.exit());

            stage.setScene(scene);
            stage.show();
        }
    }
  • I'm using Eclipse Platform and I added rxjava-2.2.12.jar to Referenced Libaries. It showed java.lang.ClassNotFoundException: org.reactivestreams.Publisher . Could you help me how to solve it ? – ELIP Sep 11 '19 at 02:14
  • Get [RxJavaFx.jar](https://repo1.maven.org/maven2/io/reactivex/rxjava2/rxjavafx/2.2.2/rxjavafx-2.2.2.jar) and [RxJava.jar](https://repo1.maven.org/maven2/io/reactivex/rxjava2/rxjava/2.1.6/rxjava-2.1.6.jar) and add to your project as referenced libraries. – Przemek Krysztofiak Sep 11 '19 at 08:53
  • I put _rxjava-2.1.6.jar_ and _rxjavafx-2.2.2.jar_ Then execute program I still found error **Caused by: java.lang.ClassNotFoundException: org.reactivestreams.Publisher** in console – ELIP Sep 11 '19 at 09:49
  • I forgot about RxJava dependencies. Please add [reactive-streams.jar](https://repo1.maven.org/maven2/org/reactivestreams/reactive-streams/1.0.1/reactive-streams-1.0.1.jar) to your libraries. Would be much easier if maven or gardle used :) – Przemek Krysztofiak Sep 11 '19 at 11:12
  • :) Well, I work well. That's really what I expect these day. Thank you so much @Przemek Krysztofiak. I hope every one can find this post for themselves. It is actually valuable. – ELIP Sep 12 '19 at 04:01