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.
Related
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
This question already has answers here:
How to detect "Recent Apps" system button clicks (Honeycomb+)
(8 answers)
Closed 9 years ago.
How can I detect when I press the return application button in android? it is on the right side of the home button on samsung s3. sorry I dont really know what is the name of the button.
Not the back button of the keyboard
It is called the Back Button. You can override its method to handle click events. Something like
#Override
public void onBackPressed()
{
// do stuff
super.onBackPressed();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.i("MainActivity", "Back button pressed, exiting..");
//Your code here..
}
return super.onKeyDown(keyCode, event);
}
Like this. i checked if it is back button. you can look up for the right button constant inside 'KeyEvent' class.
I guess you mean the button for opening the Recent Apps (the one on the right in the image):
There's no way to overriding it. The only method you can intercept is the Activity's onWindowFocusChanged, which is called once the recent apps is displayed, but also is been called on a lot of other different situations.
Take a look here for more info:
How to detect "Recent Apps" system button clicks (Honeycomb+)
And here if you want to avoid opening the recent apps (but it seems it doesn't work on Jellybean and up):
https://stackoverflow.com/a/17095749/1991053
I'd like to ask you couple of question about Gui.
I saw the following example:
public class ShellWithButton {
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = newShell (display);
Button ok = newButton (shell, SWT.PUSH);
ok.setText ("Push Me!");
ok.addSelectionListener(new ButtonHandler());
ok.setLocation(0,0);
ok.setSize(100,30);
shell.pack ();
shell.open ();
while(!shell.isDisposed ()) {
if(!display.readAndDispatch())
display.sleep ();
}
display.dispose ();
}
}
public class ButtonHandler
implements SelectionListener {
public void widgetSelected(SelectionEvent e) {
if(e.getSource() instanceofButton) {
Button b = (Button) e.getSource();
b.setText("Thanks!");
}
}
public voidwidgetDefaultSelected(SelectionEvent e){
// TODO Auto-generated method stub
}
}
(i)- Someone pushes the button- How does the program know to activate widgetSelected?
I can see that the button added the ButtonHandler listener to itself, but why that the pushing the button and not just clicking the box will send the event to ButtonHandler?
I can't see where only the pushing was sent to this listener.
(ii)-why do I send an instance of the ButtonHandler to the listeners? what does that mean?
(iii)- what's happeing when I push the button? what is this event? event is an instance of the button itself?
(iv)- Button b = (Button) e.getSource(); why do I need this casting of the source? the event, as was written, can come only from ok, which is instance of button.
(v)- why that the original button will change its title? we change B.
Thank you very much!
When someone pushes the button, the button calls widgetSelected()
because that's how the library was designed; it needs to call some
method so you can do something and they settled on that method. The
reason it calls YOUR widgetSelected() is because you gave it your
class for it to call. The button knows your class has a
widgetSelected() method because you implemented
SelectionListener, and that requires you to implement the
widgetSelected() method. That is the very reason for interfaces,
and I suggest you read up on them. Only clicking the button will
get the button to call your method because the button only knows
when it is clicked. When there is a click on the screen, only the
widgets that need to know about it are told.
As I mentioned above, you send your handler to the button so it
knows what to do when it's pushed.
When the button is pushed, it has to tell your handler what
happened, and so all the relevant information is given to you as a
SelectionEvent. The event itself isn't the button, but the event
tells you which button is pushed, in case you want the same handler
to handle more than one button.
You need the cast because your widgetSelected() method can be
called when something happens to all sorts of GUI objects, not just
buttons. Therefore, the source is given as some superclass common
to all the widgets that can call your method, and you need to cast
it back to a button when you're sure it's your button. Yes, in this
program it can only be called by the button, but that's not always
the case.
The button's text changes because B and the button you created and displayed are the same object. Objects (and arrays) in Java are "pointers," they tell you where the object is. When you assign one object to another variable, you're not copying the object, you're just using another variable to point to the same object.
(i) GUI usually uses the observer pattern, in which one or more objects subscribe to an event, and whenever this event happens it is send to all the subscribed objects, just like in your button case.
(ii) You send the instance to the listeners in order to associate them, so they may receive the event when appropriate.
(iii) What happens is that the event is causing the observers to receive a notification that your button was pushed, which eventually leads to some code being executed. The event itself is not an instance of the button, but rather a separate instance to handle the events.
(iv) You need to cast it, because the method signature is just generic, since it is used for several types of events.
(v) It changed its title, because using the observer pattern, the observer in this case your button was notified when the event which was pressing the button happened.
(i) The idea behind "Listeners" is that you want to provide a list of components, object, software modules, etc. that will be notified of the event. The reason the button click doesn't just trigger something is because something's got to be listening for that event in order to react to it. Any object implementing the appropriate Listener interface (depending on the type of event) can be added, and therefore process the event.
(ii) It's a callback. You have to tell the Listener, "Here's an instance of an object that can handle your events. Please add it to the list of objects to be notified." It's kind of like subscribing to an RSS feed, in a sense - everyone on the list gets the update when it happens.
(iii) The event is a separate object. The windowing system (which, at some deep level, connects to the windowing library of the underlying OS) creates the event object, and then goes down the list of registered Listeners, notifying each of them. There are some exceptions to this (for example, it's possible for a Listener to absorb an event, preventing anyone else on the list from hearing it, but that's a separate question of its own)
(iv) Because getSource() returns an instance of a component. If you want to be able to access the Button-specific methods (which is done in the following line, with setText, you have to be dealing with an instance of Button for that method call to know what to do (i.e. which class on which to operate).
(v) The button isn't changing it's title - the ButtonHandler is doing it. So, when the widget gets selected, the "widgetSelected" method gets called inside the ButtonHandler. It then checks the source of the event (which provides a reference to the original button) and then updates the button's text to reflect that it's been clicked.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Problem with Image Button visibility! Android
When you start the application, the buttons should be visible for 5 seconds and then become invisible, so people will know there are buttons on the screen. For example If I have a MapActivity running, the button will be an obstruction. So I want to make it invisible. It should be visible again on touching that area or around that button area so as to trigger another activity. I tried setting visibilitiy. nextbutton.setVisibility(View.INVISIBLE) I can't make it visible onTouch() Please help me with the problem.
See your original thread which has more than enough information for you to do what you want.
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().