I wanted to create a desktop project. In that If the program is not used for some minutes it should automatically display the login screen. I understand that it can be easily done using http servlet session in web apps (as told by some friends). But how it is possible in javafx or desktop apps. Can anyone tell me... Please
-
Attach an observer to the main event dispatching queue, monitor the events going through it - make determinations about weather the event is a "user input" event or not – MadProgrammer May 13 '18 at 05:48
-
Could you please make it clear with codes.. – Sam May 13 '18 at 05:49
-
3I don't know, can you do an internet search? Sorry if that's a little unfriendly, but we're not a code making machine and my understanding of the event dispatching algorithm in JavaFX is limited - I just wanted to try and seed the idea to provide you with a better idea of what to search for – MadProgrammer May 13 '18 at 05:52
-
Thanks for replying and giving me an idea – Sam May 13 '18 at 05:53
-
Please review the documentation on how to ask effective questions. This is not a code writing service – Claire May 13 '18 at 06:15
1 Answers
Okay, so based on Working with Event Filters, you could do something like...
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
TextField btn = new TextField();
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
scene.addEventFilter(KeyEvent.KEY_PRESSED, new KeyHandler());
scene.addEventFilter(KeyEvent.KEY_RELEASED, new KeyHandler());
scene.addEventFilter(KeyEvent.KEY_TYPED, new KeyHandler());
scene.addEventFilter(MouseEvent.MOUSE_CLICKED, new MouseHandler());
scene.addEventFilter(MouseEvent.MOUSE_ENTERED, new MouseHandler());
scene.addEventFilter(MouseEvent.MOUSE_DRAGGED, new MouseHandler());
scene.addEventFilter(MouseEvent.MOUSE_EXITED, new MouseHandler());
scene.addEventFilter(MouseEvent.MOUSE_MOVED, new MouseHandler());
scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new MouseHandler());
scene.addEventFilter(MouseEvent.MOUSE_RELEASED, new MouseHandler());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
protected class KeyHandler implements EventHandler<KeyEvent> {
@Override
public void handle(KeyEvent event) {
System.out.println("Key Event");
}
}
protected class MouseHandler implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent event) {
System.out.println("Mouse Event");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Now, all this really does is provides you with information about when an event occurred. You'd need to setup some kind of "timeout", maybe something like JavaFX periodic background task.
My personal idea wold be to have a Timeline
running on a short period (Maybe 1-5 seconds) and checking how long the last input was, if it exceeds the "time out" period, you can switch to the login view. This way, you don't need to keep stoping and starting the Timeline
.
For this I'd be making use of the new date/time API introduced in Java 8. See Period and Duration for more details
PS...
I also found that ...
scene.setOnKeyPressed(new KeyHandler());
scene.setOnKeyReleased(new KeyHandler());
scene.setOnKeyTyped(new KeyHandler());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
could also work.
I've not tested it, but Stage
also has addEventFilter
and addEventHandler
, which might be suitable for establishing the same concept from above to the Stage
instead of the Scene
.

- 1
- 1

- 343,457
- 22
- 230
- 366