Prevent AlertDialog from closing - java

I have a custom AlertDialog with a bunch of checkboxes. I want to prevent the AlertDialog from closing if none of the checkboxes are selected. Is this possible?
I know I can close momentarily and re-open it, but I would rather not do this as I have some code within the setPositiveButton which I do not want to repeat.
Thanks.

I want to prevent the AlertDialog from closing
Already Posted Answer on SO
OR
Create Custom Dialog to solve the issue.

I'm assuming you're using a custom view, so disable the OK/Cancel buttons at the bottom. Next you'll need to disable the back button:
#Override
public void onBackPressed() {}
The only thing left is to make sure your dialog fills the screen. If the user clicks beside the box on the parent view the dialog will close.

Related

How to hide dialog by checkbox?

How to hide the dialog box if the checkbox checked? i dont know how to do
This is my dialog method
I tried to hide dialog box permanently by shared preference. But not working (my fault). So please give me the whole function

JFace - Remove keyboard focus from dialog

I have a ProgressMonitorDialog object which contains a cancel button. I want to disable the initial focus of the cancel button. The reason I want to remove the initial focus from the button is that a user may accidentally hit a key on the keyboard while a batch operation is in progress and cancel the whole operation. If a user wants to cancel an operation I would prefer that they press the tab key and manually set the focus, or click on the cancel button with a mouse.
From what I can tell, there is no easy way to do this. I can create another button on the ProgressMonitorDialog that does nothing and have that take focus, but that's an ugly workaround. Especially since the button has to be visible or the focus will shift to the cancel button. I have also tried overriding the method that creates the cancel button and bypassing the shell.setDefaultButton() method but no luck.
Any clues/suggestions?
If you are extending Dialog class, then you can do the following, override createButtonsForButtonBar(Composite) method, and pass in false as default button argument on the button which you don't want the initial focus on.
#Override
protected void createButtonsForButtonBar(Composite parent) {
// create OK and Cancel buttons by default
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}

View above Dialog?

Just a quick question.
Would it be possible to bring a View (Button) above a Dialog that is currently showing?
Thanks
You could splash another dialog(that contains a button) ontop of the first one.
If you are aiming to get literally just a button and nothing else you'd have to define a custom layout for a dialog that is all transparent except for your button.
No, but you can create it as a custom View on the Dialog, at least (with AlertDialog.Builder.setView()

RadioButtons in JDialog in Swing?

I need to display a dialog box which contains radio buttons; and when I select the appropriate radio button, the dialog box should disappear?
Try to use TaskDialog framework. It helps to accomplish just the things you ask for in few lines of code. For your case using Command Links is the best solution.
Radio buttons are possible but not a better solution from usability point of view.
In your radio button listener, use setVisible(false), as discussed in the articles How to Use Radio Buttons and Creating and Showing Simple Dialogs.
I agree with the others that having a "dialog disappear" on radio button click is not a very good UI design. Users generally expect that a "dialog disappears" when they select a button at the bottom (e.g. OK, Cancel, Yes, No, etc.).
In any event, if I am to assume that by "dialog disappears" you mean the window closes, then the way to do that is to call dispose on the dialog.
Also, you may want to consider using JOptionPane.

Adding a scrollable label to an option dialog

I am creating an options dialog using JOptionPane.showOptionDialog(...)
When I click one of the buttons added to this dialog, I need a label to apear underneath on the dialog (and this label should be scrollable if necessary). I have written event handlers for the buttons, but I am not sure how to get this label to appear on the dialog.
Any help would be great.
Update: I realized that it would be ok if I somehow called JOptionPane.showOptionDialog(...) with an initial message, and then when one of the buttons was clicked I would change the message. Is this possible?
JOptionPane static methods are only shortcuts to easily create a dialog with option buttons and a fixed message. If you check the source from it, you will see that all is wrapped in this purpose. It's only a convenience class over a frequent use case of dialogs.
The suggestion from comment is correct, if you want more than this, you will have to create your own JDialog, as it will be easier than trying to change something from this generated dialog.
Edit: You can create your own JDialog yourself, using layout managers. A more simple way, suggested as well in the previous link, is to use a GUI builder, like the one included in Netbeans.

Categories