Application not responding to Touch-Events after showing custom dialog - java

like the title implies i've got a problem with my application. The application is supposed to run in fullscreen mode (no intention for switching back to window mode), so i designed a footer-bar holding some images (with a Label, in a VBox) so the user could navigate or exit the program.
So after starting the application all Buttons work just fine with touch. Even the Exit-button in my footer-bar responded correctly by opening my custom Dialog. But here starts my Problem. The Dialog is shown by showAndWait()-Method call, but does not respond to Touch-Events. In contrary mouse-events are still processed (i still can use a mouse to click the Buttons in my Dialog and the Dialog is responding correctly).
I hope someone got an idea what i'm doing wrong.
MyDialog.java:
public static boolean showExitDialog(Window owner, ResourceBundle resources) {
LOGGER.info("Showing exit dialog...");
final Dialog<ButtonType> dialog = new Dialog<ButtonType>();
dialog.getDialogPane().getStylesheets().add(MyDialog.getInstace().getCssPath());
dialog.setContentText(resources.getString("label.exitdialog.text"));
dialog.setHeaderText(resources.getString("label.exitdialog.header"));
dialog.initOwner(owner);
dialog.initStyle(StageStyle.TRANSPARENT);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.getDialogPane().getButtonTypes().add(new ButtonType(resources.getString("btn.Exitdialog.exit"), ButtonData.OK_DONE););
dialog.getDialogPane().getButtonTypes().add(new ButtonType(resources.getString("btn.Exitdialog.cancel"), ButtonData.FINISH));
Optional<ButtonType> result = dialog.showAndWait();
LOGGER.debug("Result: {}", result.get());
if(result.isPresent() && result.get().getButtonData() == ButtonData.OK_DONE) {
LOGGER.info("Closing exit dialog returning true...");
return true;
} else {
LOGGER.info("Closing exit dialog returning false...");
return false;
}
}
In MainApp.java:
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
// close event handling logic.
// consume the event if you wish to cancel the close operation.
if(MyDialog.showExitDialog(primaryStage, rb)) {
event.consume();
System.exit(0);
}
};
...
primaryStage.setOnCloseRequest(confirmCloseEventHandler);
In FooterBar.java:
#FXML
private void exitProgramPressedTouch(TouchEvent event) {
event.consume();
controller.getWindow().fireEvent(new WindowEvent(controller.getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
}
*Edit* Oh totally forgot: No Exception or anything else is thrown.

I don't know the reason for the described behavior - maybe a bug. However, you could try to listen for ActionEvent instead of TouchEvent. It handles both touch and mouse events:
#FXML
private void exitProgramPressedTouch(ActionEvent event) {
event.consume();
controller.getWindow().fireEvent(new WindowEvent(controller.getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
}
Maybe you need also to change the attribute which binds the event listener (from onTouch to onAction) in your FXML file.
Finally, I think, you could avoid System.exit(0); if you consume the close event only when the cancel button has been clicked:
if(!MyDialog.showExitDialog(primaryStage)) {
event.consume();
}

Related

Loading a different Scene if some buttons are checked

So I'm trying to load three specific scenes if some buttons are clicked. It is a simple loading screen that calls three other scenes which has 2 textfields(txtUser,txtPass) and 2 buttons (btnCancel, btnEnter) and 2 selecting buttons (btnStd, btnProf) which loads a fxml separately - a third fxml if both are clicked disabled
The tricky part is in this Enter button which is called after enabled or disabling two other buttons, for students and teachers. This also loads a third if both buttons are pressed: the administrator
#FXML
private void onActionBtnEnter(ActionEvent event) {
try {
if (txtUser.getText() == null || txtUser.getText().isEmpty()) {
new Message().showModal(Alert.AlertType.ERROR, "User validation",
(Stage) btnEntrar.getScene().getWindow(),"Needs username");}
else if (txtPass.getText() == null || txtPass.getText().isEmpty()) {
new Message().showModal(Alert.AlertType.ERROR, "Password validation",
(Stage) btnEntrar.getScene().getWindow(), "Needs password");}
else {
FlowController.getInstance().goMain(); //this is one of the fxlm views to load
((Stage) btnEnter.getScene().getWindow()).close(); //closes the login and loads the scene
}
}
catch (Exception ex) {
Logger.getLogger(LandingViewController.class.getName()).log(Level.SEVERE, "Login error.", ex);
}
}
This works alright but instead of choosing specific fxml to load it only loads goMain() which contains administratorview.fxml. I also did a goMain2() and goMain3().
For students, the studentsview.fxml should load. For profesors the profesorview.fxml. Following code for buttons methods and actions
#FXML
private void onActionBtnStd(ActionEvent event) {
btnStd.setDisable(true);
txtPass.setDisable(false);
txtUser.setDisable(false);
}
#FXML
private void onActionBtnProf(ActionEvent event) {
btnProf.setDisable(true);
txtPass.setDisable(false);
txtUser.setDisable(false);
}
So far good, but loads only a single fxml because they aren't called yet. So I guess in the btnEnter action there should be some code which I tried as in (same event as above):
#FXML
private void onActionBtnEntrar(ActionEvent event) {
else if (btnStd.getValue().isDisabled()){ //added else if but I don't know how to call a boolean property in a void
FlowController.getInstance().goMain();
((Stage) btnEnter.getScene().getWindow()).close();}
else if (btnProf.getValue().isDisabled()){
FlowController.getInstance().goMain2();
((Stage) btnEnter.getScene().getWindow()).close();}
else if (btnProf.getValue().isDisabled() && btnStd.getValue().isDisabled()){
FlowController.getInstance().goMain3();
((Stage) btnEnter.getScene().getWindow()).close();}
Silly me, I added .getValue() in the if statement - all it was needed is to put the component.isDisabled() or .isEnabled() if it holds a boolean. However it doesn't load the adminview if both are checked disabled - it load the studentview. Investigating further...
Edit: Ok the last "else if" must be called first than the other two. It works like a charm.

ComboBox SAME item selected action listener

A combo box will fire an event if a DIFFERENT value is selected. I want to be also be able to listen to the SAME item being selected (that is, valueProperty has no change). There seems to be no way to do this.
I tried extending the ComboBox and finding a way to listen for the little popup menu being closed, but I don't even have access to that! What can I do?
Here is what I was trying:
class ResponsiveComboBox<E> extends ComboBox<E> {
public ResponsiveComboBox() {
super();
assert getContextMenu() != null; //Asssertion failed!
this.getContextMenu().setOnHiding((WindowEvent event) -> {
fireEvent(new ActionEvent());
});
}
}
comboBox.showingProperty().addListener((obs, wasShowing, isShowing) -> {
if (! isShowing) {
System.out.println("Combo box popup hidden");
}
});
This event handler might be triggered before the value is changed.

Vaadin popup should show and hide in the click event makes no appear popup

Having a
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
//lot of method calling here then
getWindow().removeWindow(popup);
log.warn("Removed Popup");
}
I would expect to show a popup window and after some milisecundom (after the expensive method calls) it should hide itself. The log says :
2014-02-19 15:26:51 WARN xyzClass:82 - Added POPUP
2014-02-19 15:26:51 WARN xyzClass:135 - Removed Popup
But the truth is that there is no popup showing here.
If i only show it, and not remove it later (the popup will show)
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
//lot of method calling here then
log.warn("Removed Popup");
}
My main reason for this i want to achieve a glasspanel/loading screen functionality # Vaadin, and not had found better solution yet. Any solution/description why the popup not shown up i would appreciate
Just do not have time to render it. You add it and immediately remove.
Try this approach, for example:
private MyPopup popup;
public void buttonClick(ClickEvent event) {
Thread workThread = new Thread() {
#Override
public void run() {
// some initialization here
getWindow().removeWindow(popup);
}
};
workThread.start();
popup = new MyPopup();
getWindow().addWindow(popup);
}
Depending on Vaadin version you can make use of ICEPush plugin (Vaadin 6) or built-in feature called Server Push (Vaadin 7).
public void buttonClick(ClickEvent event) {
MyPopup popup = new MyPopup();
getWindow().addWindow(popup);
log.warn("Added POPUP");
// start background thread with ICEPush or ServerPush
}
// Background thread in a separate class
// update UI accordingly when thread finished the job
getWindow().removeWindow(popup);
log.warn("Removed Popup");
Thanks to it you can move your time-consuming operations to another class thus decouple your business logic from the presentation layer. You can find examples of usage in the links above.

How do I detect that a SWT dialog has been opened and is visible?

I have an SWT WizardDialog with a number of pages. When this dialog first opens I have to do a check for some conditions and if those conditions are met I need to show a popup over the freshly opened dialog.
So I have this code to listen for SWT.Show event. The event listener responds to SWT.Show to conduct its tests and show a message box:
final WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setTitle("New Wizard");
dialog.create();
dialog.getShell().addListener(SWT.Show, new Listener()
{
private boolean firstShowing = true;
#Override
public void handleEvent(Event event)
{
if (firstShowing && someConditionExists())
{
MessageBox messageBox = new MessageBox(dialog.getShell(), SWT.OK
| SWT.ICON_WARNING);
messageBox.setMessage("Test");
messageBox.open();
firstShowing = false;
}
}
});
dialog.open();
Except it's called too soon! The dialog is not visible when the handler is called. My message box appears before the dialog is visible and the dialog only appears when I dismiss the message box.
So clearly the SWT.Show is unreliable, at least on Windows where I'm running it. I've also tried putting this code into a ShellListener on the activation but that happens even before the SWT.Show example above.
So how do I reliably show a message box when the dialog is made visible?
Plan B is a dirty timer based hack where a timer is set to fire 200 ms into the future and hope that it triggers when the dialog is visible but obviously this could introduce it's own issues.
I'm using in similar situation (need that appStarted() is called after application window is visible) something like below.
public class App extends ApplicationWindow {
#Override
protected Control createContents(Composite parent) {
// ...
getShell().addShellListener(new ShellAdapter() {
#Override
public void shellActivated(ShellEvent shellevent) {
if (!started) {
Shell s = (Shell) shellevent.getSource();
s.setVisible(true);
appStarted();
started = true;
}
}
});
}
}
Maybe You can use the same like below:
final WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setTitle("New Wizard");
dialog.create();
dialog.getShell().addShellListener(new ShellAdapter() {
#Override
public void shellActivated(ShellEvent shellevent) {
if (firstShowing && someConditionExists()) {
Shell s = (Shell) shellevent.getSource();
s.setVisible(true);
MessageBox messageBox = new MessageBox(dialog.getShell(), SWT.OK | SWT.ICON_WARNING);
messageBox.setMessage("Test");
messageBox.open();
firstShowing = false;
}
}
});
dialog.open();
Instead of hooking the SWT.Show event, you may get more luck with hooking a PaintListener on to your dialog's Composite. (You'll probably want to unhook it during the first execution.)
What about overriding dialog.open() methodon your WizardDialog class? The first line of the overridden method would call super.open(), which would make it visible. Just put your custom code after that, in the .open() method.
The issue with the approach you're taking above appears to be that it responds to a Show event, which is simply notification that Show has been requested, not that the dialog is visible. The Show event could very well be designed to allow you to know when something is about to be shown, and take some action before that happens, as you've experienced.
I know that this is an old thread. But in case someone finds it useful, I found that overriding Dialog.create() rather than Dialog.open() worked for me.
it's called too soon!
I also run recently in the same trouble. The code was executed too early - my upload action (which I wanted to start automatically under some conditions) was started before the page was displayed.
This happens because the page can only be shown after the code in the SWT.SHOW listener or in the inherited setVisible() method is completed.
#Override
public void setVisible(boolean visible) {
if (visible) {
org.eclipse.ui.progress.UIJob("Auto start the upload") {
#Override
public IStatus runInUIThread(IProgressMonitor monitor) {
if (isAutoStartQcUploadSelected)
startUpload();
return Status.OK_STATUS;
}
};
uiJob.schedule();
}
super.setVisible(visible);
}
org.eclipse.ui.progress.UIJob as described FAQ_Can_I_make_a_job_run_in_the_UI_thread has solved the issue.
P.S.: Yes, I know that's an old question :-)
But it is the first one propesed by google and the hint with the UI Job was missing.
The code of marioosh can be further improved, by storing the ShellAdapter in a variable.
Remove the ShellAdapter when the listener is triggered for the first time.
The variable started is no longer needed.
The statement s.setVisible(true); is not necessary, because this event is just triggered when the shell gets visible.
public class App extends ApplicationWindow {
#Override
protected Control createContents(Composite parent) {
// ...
ShellAdapter shellActivatedAdapter = new ShellAdapter() {
#Override
public void shellActivated(ShellEvent shellevent) {
shellevent.getSource().removeShellListener(shellActivatedAdapter);
appStarted();
}
};
getShell().addShellListener(shellActivatedAdapter);
}
}

How to omit the "Cancel" button in Java ProgressMonitor?

My task is necessary and shouldn't be canceled, how do I ask ProgressMonitor not to display the "Cancel" button, so when it finishes, it will auto close the panel.
Frank
I was thinking maybe I can ask it to
return the components in it and delete
the button
Using the ProgressMonitorDemo from the Swing tutorial (linked to by BalusC) I made the following changes:
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName() ) {
int progress = (Integer) evt.getNewValue();
progressMonitor.setProgress(progress);
// Added this
AccessibleContext ac = progressMonitor.getAccessibleContext();
JDialog dialog = (JDialog)ac.getAccessibleParent();
java.util.List<JButton> components =
SwingUtils.getDescendantsOfType(JButton.class, dialog, true);
JButton button = components.get(0);
button.setVisible(false);
// end of change
String message =
String.format("Completed %d%%.\n", progress);
progressMonitor.setNote(message);
taskOutput.append(message);
if (progressMonitor.isCanceled() || task.isDone()) {
Toolkit.getDefaultToolkit().beep();
if (progressMonitor.isCanceled()) {
task.cancel(true);
taskOutput.append("Task canceled.\n");
} else {
taskOutput.append("Task completed.\n");
}
startButton.setEnabled(true);
}
}
}
You will need to download the Swing Utils class as well.
The code should only be executed once, otherwise you get a NPE when the dialog closes. I'll let you tidy that up :).
That's not possible. You can however create a custom progress monitor as outlined in this tutorial.

Categories

Resources