What listener should I register in a JFrame instance to be notified if a modal JDialog is shown on top of the frame (the frame is the owner of the dialog)? Thanks in advance.
I think JFrame.addWindowListener(...) would work and then pay attention to WindowListener.windowDeactivated(...)
ETA:
jFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowDeactivated(WindowEvent e) {
if(e.getOppositeWindow() instanceof JDialog) {
JDialog dialog = (JDialog) e.getOppositeWindow();
if(dialog.isModal()) {
// do stuff
}
}
}
});
Related
I need to perform an action after the JFrame is closed and I have this part of code for it, but this doesn't work.
Could anyone please advise what should be change here?
private void changeDefaults(){
Thread changeDefaultsThread = new Thread(new Runnable(){
public void run(){
Change ch = new Change();
ch.setVisible(true);
ch.setListeners();
ch.defaultInput();
while(ch.isActive()){
System.out.println("active");
}
updateDefaults();
}
});
changeDefaultsThread.start();
}
Change is the JFrame I am opening for another action.
You can add listener to your JFrame
frame.addWindowListener (new java.awt.event.WindowAdapter)
and override the windowClosing
#Override
public void windowClosing
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
//do something
}
});
I'm surprised no one has mentioned the simplest solution: don't use a JFrame. The best tool for this behavior -- displaying a child window and doing something immediately after it has closed -- is to use a modal dialog window such as a JDialog or JOptionPane. The JDialog set up code is very similar to that of the JFrame, with an exception being that it uses different constructors, and should have the parent window passed into it, and it uses a subset of the default close operations.
If you use a modal dialog, then program flow is halted in the calling code immediately after the dialog has been displayed (think of how a JOptionPane operates), and then immediately resumes from the spot after calling setVisible(true) on the dialog once the dialog has been closed.
The only bugaboo is that if you don't want modal behavior -- if you don't want the parent/calling window to be disabled while the child window is displayed -- then you'll have to use a non-modal JDialog window with a WindowListener.
If you want to perform an action when closing a JFrame, you just need to attach a WindowListener (extending WindowAdapter so that you do not need to implement all WindowListener methods):
import javax.swing.*;
public class AfterJFrameClose {
public static void main(String[] args) {
JFrame frame = new JFrame("My frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.out.println("Frame closing");
}
});
}
}
Instead of the System.out.println, just write the code you want to have executed.
Update: If you want to access another frame, you either should pass it as a parameter as suggested above or you can also iterate through active frames using something like this:
Frame[] frames = Frame.getFrames();
for (Frame frame: frames) {
System.out.println(frame.getTitle());
}
I have a JFrame and if I press the close button in the top right corner I invoke a JDialog and ask if the user really wants to close.
This is the ActionListener on the close Button in my main JFrame:
...
//close listener
addWindowListener(new WindowAdapter() {//invoke "wirklich schließ3n" window if Alt+F4
#Override
public void windowClosing(WindowEvent event) {
MainGuiWindow.this.saveSettings();
CloseDialog cd = new CloseDialog(MainGuiWindow.this);
if (cd.getResult()) {
System.exit(0);
} else {
//MainGuiWondow is "setVisible(false)" but still running | I don't know how to fix it
MainGuiWindow.this.setVisible(true);//doesn't work
}
}
});
The JDialog has just two buttons (YES/NO) (after pressing any button i call dispose(); in the ActionListeners of the Buttons in the JDialog and after the dispose(); i return the answers:
yes returns true
no returns false
Can anybody tell me what to do in the else case of my Close ActionListener in my MainGuiWindow.
The CloseDialog is closed cause of the dispose(); but the main window is is setVisible(false); but still running
This my help you
You need to set default close operation on the JFrame to “do nothing”:
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
add a WindowListener for the frame.
jFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
handleClosing();
}
});
And Refer Here
How do I do action when I'm going to terminates the programme.
For example :
When I'm going to terminates the programme, I want to set
if( the x button is clicked or something )
deleteRow from database;
else
just end.
Is it should be done inside the ui frame ?
Use window listeners for that. windowClosing method is used to execute code when window is closing, windowClosed method can be used if you want to do something after the window has closed.
JFrame window=new JFrame("Window");
window.addWindowListener(new WindowAdapter(){
#Override
public void windowClosed(WindowEvent evt)
{
//Do something after window has closed.
}
public void windowClosing(WindowEvent evt)
{
//Do something when window is closing.
//Useful when you have to access data in window(buttons, textfields etc)
}
});
There are also other window event listeners.Documentation of WindowListener
addWindowListener(new WindowAdapter()
{
#Override
public void windowClosing(WindowEvent e)
{
.....
}
});
My JFrame opens in a minimized Mode but it can be maximized.
I want to disable the maximize icon so that user cannot maximize the frame and can see it only in the minimized or default mode.
Is it possible?
Use frame.setResizable(false). It disables the maximize button but let the the 'close' and 'minimize' buttons active.
Use this in your code, try using JDialog instead of JFrame for main window.
frame.setResizeable(false);
there is no direct way to remove the maximize button off the
Resizable JFrame as it is created by Windows and it is not painted
using Swing so U can’t touch this.
so you can replace JFrame with JDialog or remove the title bar and implement customized title bar.
You can disable it by using frame.setResizeable(false); or you can remove it completely by using following code
public class Test extends JDialog {
public Test(JFrame frame, String str) {
super(frame, str);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public static void main(String[] args) {
try {
Test myFrame = new Test(new JFrame(), "Removing maximize button");
JPanel panel = new JPanel();
panel.setSize(100, 100);
myFrame.add(panel);
myFrame.setSize(100, 100);
myFrame.setVisible(true);
} catch (IllegalArgumentException e) {
System.exit(0);
}
} }
I'm making a program with a logger. The logger has its own JFrame.
I'm trying to override the reaction from clicking on the minimize-button of that frame.
I would like the frame to either setVisible(false) or do the defaultCloseOperation (as i set that to hide earlier).
How should I do this?
Thanks in advance
Use a JDialog instead of a JFrame. JDialogs don't have a minimize button.
You can add a WindowListener and add a iconified handler that will react when the window is minimized.
Maybe:
frame.addWindowListener(new WindowAdapter(){
public void windowIconified(WindowEvent e){
frame.setVisible(false);
}
});
You can use the WindowStateListener like this
f.addWindowStateListener(new WindowStateListener() {
#Override
public void windowStateChanged(WindowEvent arg0) {
if (arg0.getNewState() == Frame.ICONIFIED) {
// do stuff
}
}
});
Try this:
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowIconified(WindowEvent event)
{
//do your stuff
}
});