I went through some documentations here and here and found that gluon dialogs Can* have a generic type which will be the generic type of the object to be returned when you call showAndWait(). But gluon alerts (com.gluonhq.charm.glisten.control.Alert which is a subclass of com.gluonhq.charm.glisten.control.Dialog) does not seem to have a generic type and does not also seem to allow you to give it a generic type.
The problem occured when I tried to call setOnHidden as:
boolean shown;
String report = "";
Alert al = new Alert(AlertType.ERROR);
al.setContentText(report);
al.setAutoHide(false);
al.setOnHidden(e->{
shown = false;
});
shown = true;
al.showAndWait();
and I got the following warning on the setOnHidden() Call:
The method setOnHidden(EventHandler) belongs to the raw type Dialog. References to generic type Dialog should be parameterized
Any clarifications about gluon dialogs or ways to get rid of the warning are most welcome.
Like in the built-in JavaFX Alert control, the implicit type of the Gluon's Alert control is the same: the JavaFX built-in ButtonType, so if you click the OK button, it will return ButtonType.OK.
As you can see at the Alert JavaDoc, the control has one or two default buttons: an OK button for all of them, and a Cancel button for the Confirmation alert. Each of these buttons has as default result ButtonType.OK and ButtonType.CANCEL.
So this works for both Alert controls:
alert.showAndWait().ifPresent(result -> {
if (result == ButtonType.OK) {
// do something;
}
});
One of the things you will notice with both OK and Cancel buttons: the alert will be dismissed, so you don't have to do it.
You can also provide your custom buttons. Then you'll need to take care of calling hide():
final Button myYesButton = new Button("Yes");
myYesButton.setOnAction(event -> {
alert.setResult(ButtonType.YES);
alert.hide();
});
alert.getButtons().add(myYesButton);
About the setOnHidden, see Javadoc. It requires a LifecycleEvent:
alert.setOnHidden((LifecycleEvent event) -> System.out.println("alert hidden"));
but you can use just:
alert.setOnHidden(event -> System.out.println("alert hidden"));
Finally, make sure you are importing the right control:
import com.gluonhq.charm.glisten.control.Alert;
...
Alert alert = new Alert(javafx.scene.control.Alert.AlertType.ERROR);
Related
I have a requirement to change the default value of a checkbox in SysUserSetup form to true. The data source of this field is SysUserInfo > EventWorkflowTasksInEmail. I have tried to do this by writing the following code in post event handler of initValue method of the table:
[PostHandlerFor(tableStr(SysUserInfo), tableMethodStr(SysUserInfo, initValue))]
public static void SysUserInfo_Post_initValue(XppPrePostArgs args)
{
SysUserInfo sysUserInfo = args.getThis() as SysUserInfo;
sysUserInfo.EventWorkflowTasksInEmail = NoYes::Yes;
}
But the value of checkbox is still false. Can anyone suggest if I am missing something ?
Edit : I debugged my code and found that the customization is not being executed , any reason why ?
The method gets executed when I import users from AAD and the change is working fine but it does not work when I create a new user from D365 itself , is there a way to achieve the latter ?
My Dialog is a simple Frame with an Image, a label to display a question and two more labels (Yes / No) with TapCommand.
I've set up the container with the DialogPage.xaml and DialogPageViewModel and injected in the ViewModel I want to open the dialog.
Here is the code I'm using to call the Dialog:
public void ShowDialog()
{
_dialogService.ShowDialog("DiscardPopup", CloseDialogCallback);
}
void CloseDialogCallback(IDialogResult dialogResult)
{
var goBack = dialogResult.Parameters.GetValue<bool>("GoBack");
if (goBack)
NavigationService.GoBackAsync();
}
If the user taps over the "Yes label", I execute this command:
YesCommand = new DelegateCommand(() => YesTapped());
private void YesTapped()
{
IDialogParameters pa = new DialogParameters();
pa.Add("GoBack", true);
RequestClose(pa);
}
If the user taps over the "No label", I simply call:
NoCommand = new DelegateCommand(() => RequestClose(null));
The "problem" is when the ShowDialog is fired, the DiscardPopup is taking up to 3 seconds to show up.
Is there a way to make it faster?
The same happens with the TapCommands, 2 - 3 seconds when the RequestClose is invoked.
Without actual code telling you exactly what the issue is, is going to be best guess. Based on your feedback to my comments above I would suggest the following:
Try displaying the dialog on a test page that doesn't have a complex layout. My guess is that you won't see such a long load time. If that's the case this would point to your layout being overly complex and that the lag time is due to the device struggling to re-render the View
Try using Prism.Plugin.Popups. You'll need to initialize Rg.Plugins.Popup and register the DialogService. You can see docs on that at http://popups.prismplugins.com
In order to verify that all changes made by the user have been saved I want to intercept the exiting/quitting of a JavaFX application.
Is there a common way-to-go to achieve this like overriding an event or is there more to it?
As they have already said, this is done by intercepting WindowEvent.WINDOW_CLOSE_REQUEST. You can then stop the suspension by calling event.consume().
This is an example of how to capture the event and display a confirmation dialog. Depending on the user's choice, you can take serialization actions if you wish.
primaryStage.setOnCloseRequest(event -> {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initOwner(primaryStage);
alert.initModality(Modality.APPLICATION_MODAL);
alert.setHeaderText("Exit");
alert.setContentText("Do you want to exit?");
alert.getDialogPane().getButtonTypes().setAll(ButtonType.OK, ButtonType.NO);
Optional<ButtonType> optional = alert.showAndWait();
if(optional.isPresent() && optional.get() == ButtonType.OK) {
// save data
return;
}
event.consume();
});
In order for the implementation to be complete, you need to implement a logic for clear exit from the application from control. For example, when choosing from the File menu -> Close. When capturing the event, you must run WindowEvent.WINDOW_CLOSE_REQUEST to trick the exit logic.
closeMenuItem.setOnAction(event -> {
Window window = menuBar.getScene().getWindow();
window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
});
In the class Application there is the stop method which you can override possibly.
I have an activity indicator on xaml page. Initially its IsVisible property is false. I have a button on page. When user click on button it calls a web service to get data. I change the value of IsVisible property to true before calling the service so that activity indicator starts to display on page and after successful calling of service I change its value to again false so that it doesn't show any more on page.
But it is not working. I know the actual problem. When we call the web service the UI thread gets block and it doesn't show the activity indicator.
How I can enable the UI thread when web service gets called so that activity indicator can show on page until we get the data?
Try making your webservice call into an async and await it.
Depending on how you've structured things you may have to use a TaskCompletionSource as the following example demonstrates.
In this example when the button is clicked, the button is made invisible, and the ActivityIndicator is set to IsRunning=True to show it.
It then executes your long running task / webservice in the function ExecuteSomeLongTask using a TaskCompletionSource.
The reason for this is that in our button click code, we have the final lines:-
objActivityIndicator1.IsRunning = false;
objButton1.IsVisible = true;
That stop the ActivityIndicator from running and showing, and also set the button back to a visible state.
If we did not use a TaskCompletionSource these lines would execute immediately after calling the ExecuteSomeLongTask if it was a normal async method / function, and would result in the ActivityIndicator not running and the button still being visible.
Example:-
Grid objGrid = new Grid()
{
};
ActivityIndicator objActivityIndicator1 = new ActivityIndicator();
objGrid.Children.Add(objActivityIndicator1);
Button objButton1 = new Button();
objButton1.Text = "Execute webservice call.";
objButton1.Clicked += (async (o2, e2) =>
{
objButton1.IsVisible = false;
objActivityIndicator1.IsRunning = true;
//
bool blnResult = await ExecuteSomeLongTask();
//
objActivityIndicator1.IsRunning = false;
objButton1.IsVisible = true;
});
objGrid.Children.Add(objButton1);
return objGrid;
Supporting function:-
private Task<bool> ExecuteSomeLongTask()
{
TaskCompletionSource<bool> objTaskCompletionSource1 = new TaskCompletionSource<bool>();
//
Xamarin.Forms.Device.StartTimer(new TimeSpan(0, 0, 5), new Func<bool>(() =>
{
objTaskCompletionSource1.SetResult(true);
//
return false;
}));
//
return objTaskCompletionSource1.Task;
}
You need to do your work in an asynchronous way. Or in other words: Use Asnyc & Await to ensure, that you UI works well during the call.
You can find more informations in the Xamarin Docs.
async and await are new C# language features that work in conjunction
with the Task Parallel Library to make it easy to write threaded code
to perform long-running tasks without blocking the main thread of your
application.
If you need further asistance, please update your question and post your code or what you have tried so far.
The code bellow generates an Alert Dialog with two buttons, Ok and Cancel; and it also works as expected: if I click Ok, the system exits, otherwise the dialogs vanishes.
The strange thing is: if I ommit the else block handling the event, the platform will always exit, not considering the button I clicked.
Is that really the expected behaviour? Am I missing something?
private void setCloseBehavior()
{
stage.setOnCloseRequest((WindowEvent we) ->
{
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
a.setTitle("Confirmation");
a.setHeaderText("Do you really want to leave?");
a.showAndWait().ifPresent(response -> {
if (response == ButtonType.OK) {
Platform.exit();
} else {
we.consume();
}
});
});
}
Here is the documentation for windows.onCloseRequest:
Called when there is an external request to close this Window. The installed event handler can prevent window closing by consuming the received event.
So, if you don't consume the close request event in the close request handler, the default behavior will occur (the window will be closed).
You don't really need to invoke Platform.exit() in the close request handler because the default behavior is to exit, so you could simplify your logic. You only need to consume the close request event if the user does not confirm that they want to close:
stage.setOnCloseRequest((WindowEvent we) ->
{
Alert a = new Alert(Alert.AlertType.CONFIRMATION);
a.setTitle("Confirmation");
a.setHeaderText("Do you really want to leave?");
Optional<ButtonType> closeResponse = alert.showAndWait();
if (!ButtonType.OK.equals(closeResponse.get())) {
we.consume();
}
});
There is a similar fully executable sample in the answer to the related StackOverflow question:
How can I fire internal close request?.