returning the dialog from JOptionPane.showOptionDialog - java

I have created in Java swing an option dialog with the JOptionPane.showOptionDialog method. This dialog includes different buttons, and among those a Cancel button. I would like to attach a listener method to this Cancel button so that once selected, the dialog is disposed.
My question is: how do I "retrieve" (or return) the dialog generated by the JOptionPane.showOptionDialog method?

I will assume you have read "How to Make Dialogs" tutorial from Oracle?
The return value from showOptionDialog() is an int, which indicates which button was chosen. Regardless of pressing OK, cancel or whatever, the window should dispose of itself. If you need more information back from the window then just which button was pressed, look at the other options like showInputDialog().

Related

How to stop alert dialog from shifting upwards when keyboard is shown

I have ran into some issue with android. The situation is currently, when I tap the editText field in the alert dialog, the dialog will shift up to make way for the keyboard. However, I have calculated that there is enough space between my dialog and the keyboard. The reason behind is to have the dialog box hide the content that is underneath it.
Question
Is there a way to stop the alert dialog box from shifting upwards ?
Add or change in AndroidManifest.xml for that activity tag.
<activity
android:name=".YourActivity"
android:windowSoftInputMode="adjustNothing" />
As long as you always need to show the keyboard immediately once the dialog opens rather than once a specific form widget inside gets focus (for instance, if your dialog just shows an EditText and a button), you can do the following:
AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();
Rather than calling .show() on your builder immediately, you can instead call .create() which allows you to do some extra processing on it before you display it onto the screen.

How to wire action to a button in ControlsFX?

In Swing it was possible to connect action to a button and button was reflecting action's parameters like icon, enableness, text and so on.
Is this also possible with ControlsFX' Action?
If yes then how?
For an existing button you can use ActionUtils.configureButton(action, button).
To create a button bound to the action, use ActionUtils.createButton(action)

Close Dialog by clicking outside of it

Working with libGDX, and in this particular project we are using Dialog to have a box popup when the user clicks a certain button.
What I want is to be able to dismiss the Dialog by clicking outside of it.
At other times, I have used two tables, a background table and a menu table, and added a transparent background to the background table that when clicked will remove both of those tables from the Stage.
I have tried making a class that has a both a Dialog and a background table like the one mentioned above, but the background table never receives any actions.
I have also tried simply adding this background table to the stage before creating the dialog box, but this does not work either.
Finally, I have also tried to subclass Dialog, the idea being to override the show(stage) method to change its behavior, but I don't know how to do this one, and I'm not sure if it would work, anyway.
I believe the problem is that dialog.show(stage) changes the situation in the stage to only accept clicks inside the Window of the dialog box. I have seen this question about adding a close button to a dialog box, but playing with the clipping settings is not working to fix this problem.
There is also the possibility that when show() calls the pack() method and does its layout thing that something is happening that is making what I am trying to do impossible. I think that the solution will be overriding show() or overriding pack(), or both, but I don't know how to do this.
I can post code if need be, but this should be a pretty complete description of what I have tried and what I need to accomplish.
I know this is an old question but for those like me that searched the entire web for an answer only to find it inside libGDX code, the answer to .close() a libGDX dialog by code is simply to call the method
dialog.hide();
EDIT (added from the comments below):
so all he needs to do is register a global touch down event and see if
the touch has happened inside the Rectangle of his dialog, if not,
close it

On Close button action Java [duplicate]

This question already has answers here:
How to capture a JFrame's close button click event?
(6 answers)
Closed 9 years ago.
Sorry if there is a question like this already but I couldn't find it.
I found things like how to create a button listener and people create a button and perform action on it. I want when the close button ( x button ) is pressed a warning window to pop-up and say that the project is not saved. I couldn't find how to access the close button. How to use a button listener with the close button? Hope made myself clear.
Thanks
You need to add a WindowAdapter to your JFrame.
myFrame.addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent e){
// do something
}
});
Now, every time someone presses the close button, the windowClosing() method will be called. Check if the user has saved the work. If not, either auto-save it like or promprt user to save it.
overload the close/exit action
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
Create custom operation for setDefaultCloseOperation?
define a method separately and add listener for close action.

How do you hide the keypad on activity change or when edit text is not enabled?

How do you hide the keypad?
I'm currently using the following code to hide the keypad. Is there a better way to do this?
Window window = getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You have the right idea for hiding the soft keyboard. You could probably use the SOFT_INPUT_STATE_HIDDEN flag instead of the ALWAYS_HIDDEN one, this would allow it so it will re-open when the user clicks in the EditText without the need to call setSoftInputMode() again.

Categories