Javafx : Dialog pane reference - java

I have a controller, which contains buttons, if I press one button an Alert shows, with Yes and No options, and I want to write a test for this controller class testing the result of this Alert, for example if I press the Yes button what happens. I could get the Window/Stage, and the Scene, but how can I get the Alert from the controller which popped up. Is that the primaryStage or I don't really understand how can I get it. I had a look at the hierarchy of the Alert and it doesn't have any relation with the Stage or Scene or just I didn't observed it. Is there a possibility to get a reference of the Alert or of its buttons?

Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Look, a Confirmation Dialog");
alert.setContentText("Are you ok with this?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
// ... user chose OK
} else {
// ... user chose CANCEL or closed the dialog
}
This code above does what you need.
Look here for more
And you can always consult java docs.

Related

Dismiss Popup Window on Touch Outside

I'm trying to open up a pop-up window with 4 buttons on it that will dismiss when a button is pressed or when the user clicks outside of the pop-up window. I would just make an alert dialogue, but that will only support 3 buttons.
There have been a lot of questions about this same thing, and I can't find any consistent answer or any answer that works for me (including the deprecated Bitmap Drawable). I've put all of the suggestions I've seen into my code, but to no avail.
Here's everything I've used so far:
//to create new popup window
LayoutInflater chooseMealInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View chooseMealLayout = chooseMealInflater.inflate(R.layout.choose_meal_dialog, null);
PopupWindow chooseMealPopup = new PopupWindow(chooseMealLayout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//to make popup dismiss on touch outside
chooseMealPopup.setOutsideTouchable(true);
chooseMealPopup.setFocusable(true);
chooseMealPopup.setContentView(chooseMealLayout);
chooseMealPopup.showAtLocation (chooseMealLayout, Gravity.CENTER,0,0);
chooseMealPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
I've tried to find everything I can, like keeping setFocusable before showAtLocation, but when I run the app, nothing happens when I click. Figured it might be something individual to my code, since I'm new and don't really know what I'm doing.
don't know what you really want to do...
if you want show dialog when click button
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do like show dialog
}
});

Check the dialog is visible - Espresso

I found on Stackoverflow something like that about checking when dialog is visible:
onView(withText("Yes"))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
Of course this works if Dialog with button 'yes' is visible, but in different scenario, if dialog will be invisible I got crash:
android.support.test.espresso.NoMatchingViewException: No views in
hierarchy found matching: with text: is "Yes"
So how to write that if the dialog exists, click yes, and if it does not exist, then nothing will be clicked?
You could try this:
onView(withText("Yes")).inRoot(isDialog()).withFailureHandler(new FailureHandler() {
#Override
public void handle(Throwable error, Matcher<View> viewMatcher){
}
}).check(matches(isDisplayed())).perform(customClick());
//if dialog is visible, perform click, otherwise do nothing.

AlertDialog shows SystemUiVisibility

I am making an android game and i set my SystemUiVisibility with the setSystemUiVisibility to not show the top bar and other system ui's, but now when u die in game i want to show an AlertDialog with the score you have. When i show the AlertDialog it brings the SystemUiVisibility back and i dont want to have that happen. Does someone know how to fix that. Here is the code i am using right now for the AlertDialog:
dlgAlert.setMessage("You lost, you hit the right color " + Points + " times");
dlgAlert.setTitle("You died");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.create().show();
Make the Dialog non-focusable when we create it (so we don't trigger a user-interaction) and then make it focusable after it's displayed.
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
//Show the dialog!
dialog.show();
//Set the dialog to immersive
dialog.getWindow().getDecorView().setSystemUiVisibility(
context.getWindow().getDecorView().getSystemUiVisibility());
//Clear the not focusable flag from the window
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

Show Alert JavaFX

When the alert is opened, this minimized the main window, displays the alert, when I close the alert, returns to the main window.
It is possible that the alert is displayed above the main window ??
Thanks.
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
alert.show();
Thanks.
Please set the owner of the alert box like below
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(primaryStage);
Cheers

How to check all items in multichoice alert box by providing a SELECTALL CHECKBOX programmatically

Hi I have an alert dialog with multichoice check box & i want to set one select all checkbox so that by clicking this SELECTALL CHECKBOX all items(i.e checkbox ) in the alert dialog list will automatically select programmatically.
Kindly help me.Thanks in advance.
You can read my answer here or example here or continue reading...
You need to setMultiChoiceItems(R.array.items, null, null) on AlertDialog.Builder and after that, get the ListView from an onCreated and shown dialog and setOnItemClickListener with little logic and you are done. Check my example here

Categories

Resources