I'm working on a automatic logout class. After x minutes without actions I want to log out the user.
Everything is working, but I need a point in my application where to reset my timer.
I want to reset the timer with every click in my application. Is there a way to notice every click an throw an event? Maybe a LayeredPanel or GlassPanel?
I don't like the idea of resetting the timer by moving the mouse.
You can add some code to your EntryPoint:
Event.addNativePreviewHandler(new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getNativeEvent().getType().equals("click")) {
// reset your timer
}
}
});
In case the user clicks in your app, you'll get the chance to do something.
Related
I'm setting up a listener for the isShown property of a Stage in JavaFX:
stage.showingProperty().addListener((observable, old, showing) -> {
if (showing) {
System.out.println("Now it's shown");
} else {
System.out.println("Now it's hidden");
}
});
I have a complex logic that may cause the stage to not be shown when the app starts (it just shows a tray bar icon). When it starts hidden, the first time I show it, the listener doesn't get any notification. Afterwards it starts working normally. I tried to reproduce it in a minimum example but I haven't been able yet.
What's weird is that if I add an invalidation listener:
stage.showingProperty().addListener(observable -> { });
It works as expected. The change listener starts to get called even from the first change.
What could possible cause this behavior?
I have been working from an example I found, here's the link to the Git repo:
https://github.com/basakpie/vaadin8-spring-security-sample
It works great, it's just what I need, except for one thing: I need Server Push.
Here's what I've done so far:
added the Vaadin Push dependency
added the following lines to the start of the MainUI.init() method:
getPushConfiguration().setTransport(Transport.WEBSOCKET);
getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
Added the following fields to the MainUI class:
Label time = new Label();
Timer timer;
Added the following method to the MainUI class:
private void updateTime() {
access(() -> time.setValue(String.format("The server-side time is %s", LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")))));
}
Finally, added the following to the end of the MainUI.init() method:
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
updateTime();
}
}, 1000L, 1000L);
It mostly works. I am able to see the current system time updating every second. But when I hit refresh in the browser, the application just hangs with the vaadin loading spinner. There are no error messages.
I have tried the following alternatives:
Adding the method
public void attach() {
getPushConfiguration().setTransport(Transport.WEBSOCKET);
getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
}
and removing the getPushConfiguration lines from init()
This solves the hanging problem, but the push does not work - no errors, just the time is not displayed at all.
I also tried adding a #Push annotation to MainUI. This results in the same behaviour as before - freezing on refresh.
How can I fix this? Any suggestions would be welcome.
Try out the below procedure:
Add #Push to the MainUI.java file
#Push(transport = Transport.WEBSOCKET,value = PushMode.AUTOMATIC)
Instead of :
getPushConfiguration().setTransport(Transport.WEBSOCKET);
getPushConfiguration().setPushMode(PushMode.AUTOMATIC);
Add #PreDestroy to exit the timer when you navigate from the mainUI
#PreDestroy
void destroy() {
timer.cancel();
}
Find the complete revised code HERE
https://gist.github.com/cansoftinc/351452ee0e616d353519f147c4a961ba
I Had exactly the same problem and my solution to this was to implement the vaadin servlet. Check this for more information.
I have a PlayState and a MenuState. In PlayState, when I click the back-button with my Samsung(Android)-Phone, then it exits the App. What I want it to do when I click the back-button, is to get back into MenuState.
So in PlayState, I tried to set up isKeyPressed:
#Override
protected void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.BACK));
gsm.set(new MenuState(gsm));
}
#Override
public void update(float dt) {
handleInput();
I'm not too disappointed, because when I hold the back-button then it goes to MenuState and when I let the button go after like 3 seconds, then the MenuState even stays.
Of course this is not the way I want it to work, because nobody wants to hold the button, but just click it.
Tho, when I just click it, then it loops between MenuState and PlayState (like a framework) and errors/closes the app without a notification.
Any ideas?
Override onBackPressed then write your condition on it, That will help you to solve the problem .
I have implemented a JFace Wizard with 2 WizardPages.
By default, the first WizardPage has these 4 Buttons:
Back (disabled)
Next (focused)
Cancel
Finish (disabled)
Now I want to set the default focus on the Cancel Button. How do I do that?
Removing the focus and setting it to some Control of the page's content would also be ok.
I tried setting the focus to a Button in the content layout of the WizardPage, but this only sets me a second focus on the immediateButton. The focus on the Next button is still there, and the Next button reacts to pressing enter, which is what I want to avoid.
#Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
immediateButton.setFocus();
}
}
How can I access the Dialog buttons and change their focus?
The Next button does not actually have focus, rather it is the Shell Default button.
The logic in WizardDialog makes either Next or Finish the default button and there does not seem to be a way to change this.
You may be able to override this by calling getShell().setDefaultButton(button) in your wizard page.
Update: Testing this you can do it in setVisible but you need to use Display.asyncExec to make the code run at the right time:
final Shell shell = getShell();
shell.getDisplay().asyncExec(() -> shell.setDefaultButton(immediateButton));
above is for Java 8, for Java 7 or earlier:
shell.getDisplay().asyncExec(new Runnable()
{
#Override
public void run()
{
shell.setDefaultButton(immediateButton);
}
});
I am developing a application. I want when I exit it, a message box appears having message, for example, "Thanks for using Soft Attendance". Then disappear automatically say after few seconds.
I have write code for this as following:
public void windowClosing(WindowEvent e){
int whichOption;
whichOption=JOptionPane.showConfirmDialog(f1,"Are you Serious?","Soft Attendence",JOptionPane.YES_NO_OPTION);
if(whichOption==JOptionPane.YES_OPTION){
f1.dispose();
JOptionPane.showMessageDialog(null,"Thanks for using Soft Attendence");
}
}
When i click on exit button a confirmation dialog box appear and after clicking yes button
application is exited and a another message box appears.
Now I have two questions in my mind :
First question is I have dispose application already and then message box is shown. Is it fine to show message box after its parent is killed?
When second message box appears I don't want to click on "OK" button. I want it should automatically disappear. So my second question is How to shown message box that disappear automatically after some time?
Is it fine to show message box after its parent is killed?
I think this would not be a ideal case. But you can open dialog if the parentComponent has no Frame JOptionPane.showConfirmDialog(null,"...");
How to shown message box that disappear automatically after some time?
Auto disposable you can get it by a trick, you can create a SwingWorker which normally performs GUI-interaction tasks in a background thread. Set a timer and which will execute after time period and close your dialog explicitly.
class AutoDisposer extends SwingWorker<Boolean, Object> {
#Override
public String doInBackground() {
try{
JOptionPane.getRootFrame().dispose();
return Boolean.True;
}catch(Exception ex){...}
return Boolean.False;
}
}
...
new Timer(seconds * 1000, autoDisposer).start();