Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.
On the JFrame:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()
I'd use a shut-down hook instead that way it'll save even if you close the app via another method.
Looky here
You can try using a WindowListener interface. If I remember correctly, it should allow you to do things when something on the frame is clicked on(like exiting).
See http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html
Related
Using swing, I want to close my mainGUI with "setVisible(false)", how can I close it when I opened it like this:
public void run() {
new mainGUI().setVisible(true);
}
I want to close the GUI from a method like this:
public void close(){
//Close GUI here
}
What call is missing from my code to let the GUI close itself?
It depends the place you wanna do that:
In the same class
// Close the window
this.detach();
or
// Hide the window
this.setVisible(false);
In other class you need to send the window like a param and use the same methods.
// Close the window
window.detach();
or
// Hide the window
window.setVisible(false);
I want to close the GUI from a method like this:
Presumably you invoke this method when the user clicks on a button or menu item. So the best way is to create an Action that you add to the component. Then the Action will be executed when the component is clicked.
For example you can use the Exit Action found in Closing An Application. The ExitAction will find the current window with focus and then close it.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) or you could use focus listener
Go through this in java docs this might help you
I have a container that has only JButtons in it and I want to click on all the buttons from code so that I don't click on them manually from the interface, how do I do that?
You can actually call a method called doClick() to do this
http://docs.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html#doClick()
Basically, you should be able to do something like:
myButton.doClick()
and if you have an ActionListener attached to the button it will act as if the user clicked the button.
You can call clickListener method explicitly. Or write a method like clickListener and call it.
Is it possible to simulate a click on a button, similar to doClick() but just graphical simulate it, not generate any ActionEvent´s. If know that i can extend the class, do my own doClick with a simple if-statement. But is it any other possibility?
I want to this because I have a button that the user can press, but sometimes the computer (it s in a game) "presses" the button. All the logic is done in another thread, I just wanna display it for the user.
if you dig into the details in the look and feel you're using, you might be able to see how it detects and paints the button in its "down" state, and then simulate that?
or you could extend/implement ButtonModel, and mess with the setPressed/isPressed state?
I think the simplest solutions is to just check the model in the ActionListener.
I don't see a way to distinguish between the two; the model is oblivious as to who calls its methods. Instead, you could save all the listeners, invoke doClick(), and restore the listeners. It looks like you would have to check action, change and item listeners.
If you want to click on the button try this:
try
{
robot = new Robot();
robot.mouseMove(0,500);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}catch(Exception e){}
But maybe you will prefer to simply disable the button when the user shouldn't click it
Button.setEnabled(false);
You can set whether it's enabled or not, so use "buttonName.setEnabled(false);"
I would like to make possible the navigation of frames in java.Whenever i close a frame the remaining frames which are also opened get closed;and the entire program stops.
Please help...
You probably used
//this will terminate or exit your application
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Maybe you want to use this instead,
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
for your reference go to this link
If you want to close only that one frame, you should do something like this: setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
If you want to close all frames whenever a single frame closes you can do the following:
You could use a window listener and call System.exit(0); when the JFrame closes, or try setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); on each JFrame. That way your program would close all frames and end.
If you need to perform some tasks before application quits, you should probably use the window listener.
You can also do that graphicaly. right click on your frame and select properties and from there you can change that like the below picture.
If you were using swing palette. In frame properties choose default close operation as (Dispose). Follow as per the image given in this solution.
My problem was that I used a listener found on the basic tutorials :
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);
I know it's dumb. I didn't see it, but some people might have done the same thing, so I'll just leave this here ;)
we use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) for JFrame and those classes that implements JFrame.
what can we do like this for JFileChooser?(the window that suddenly when you push browse button will pups up)
On a closing JFileChooser you normally don't want to exit your application, at first you want to get the inputs the user made from the dialog. After that you can call System.exit() directly.
You can put a JFileChooser on a custom JFrame and set the default close operation on that JFrame.