检查触发的WindowClosing事件 [英] Check what triggered WindowClosing event
问题描述
public void windowClosing(WindowEvent e){
JOptionPane.showMessageDialog(null,event source:+ e.getSource(),Test,JOptionPane.OK_OPTION);
methodA();
}
由于方法dispose()触发WindowClosing事件,我想知道这一点。因此,如果单击一个按钮调用methodA()然后dispose(),dispose()将触发一个关闭事件,该事件也定义为调用methodA()。这导致methodA()被调用两次,我不想要。
public void actionPerformed(ActionEvent e){
if(e.getSource()== confirmButton){
methodA();
dispose(); //这将触发窗口关闭并再次调用methodA()
}
}
所以我想解决问题的方法是检查一个名为确认的特定按钮是触发关闭事件的按钮。那么我不想调用methodA(),以便它不被调用。
如果这是不可能的,我至少可以检查是否关闭(X)按钮框架是调用窗口关闭事件的框架?
谢谢
由于方法dispose()触发WindowClosing事件,我想知道这一点。因此,如果单击一个按钮调用methodA()
,然后调用dispose(),dispose()将触发一个关闭事件,该事件定义为调用methodA()定义为
。这导致methodA()被称为
两次,我不想要。
这里的设计错误与每个组件的责任有关,因为关闭
按钮应该做到预期的做法:关闭框架。或者更好地发送 WINDOW_CLOSING
事件,让 WindowListener
做任何事情。
如果您需要确保在关闭顶级容器(窗口)之前 c> methodA()被调用 WindowListener
听起来像是正确的候选人来调用该方法。我将默认关闭操作设置为 DO_NOTHING_ON_CLOSE
,让听众在满足必要条件的情况下处理窗口。
这种方法体现了此处,并考虑以下代码段:
JButton closeButton = new JButton(Close);
closeButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
窗口= SwingUtilities.windowForComponent((JButton)e.getSource() );
window.dispatchEvent(new WindowEvent(window,WindowEvent.WINDOW_CLOSING));
}
});
...
JFrame frame = new JFrame(Frame);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
// Call methodA()here。
//如果一切都正常,然后处理窗口,否则记录
// errors / exceptions并通知用户出现问题
e.getWindow()。dispose();
}
});
Is there a way to see what exactly triggered WindowClosing event in a JFrame? At the moment getSource(), it seems to only be returning the JFrame:
public void windowClosing(WindowEvent e) {
JOptionPane.showMessageDialog(null, "event source: " + e.getSource(), "Test", JOptionPane.OK_OPTION);
methodA();
}
I want to know this due to the method dispose() triggering a WindowClosing event. So if a button is clicked which invokes methodA() and then dispose(), dispose() triggers a closing event which is defined to call methodA() as well. This causes methodA() to be called twice and I don't want that.
public void actionPerformed(ActionEvent e) {
if (e.getSource() == confirmButton) {
methodA();
dispose(); //this will trigger window closing and call methodA() again
}
}
So the way I want to solve the problem is to check if a specific button named "Confirm" is the one that triggered the closing event. Then I wouldn't want to invoke methodA() so that it's not calledtwice.
If this is impossible, can I at least check whether the close (X) button in the frame is the one that invoked the window closing event?
Thanks
I want to know this due to the method dispose() triggering a WindowClosing event. So if a button is clicked which invokes methodA() and then dispose(), dispose() triggers a closing event which is defined to call methodA() as well. This causes methodA() to be called twice and I don't want that.
IMHO there's a design mistake here related to the responsibility of each component in the sense that Close
button should just do what it is expected to do: close the frame. Or even better dispatch a WINDOW_CLOSING
event and let the WindowListener
do whatever it has to be done.
If you need to be sure that methodA()
is called before close the top-level container (window) then the WindowListener
sounds like the right candidate to call that method. I'd set the default close operation to DO_NOTHING_ON_CLOSE
and let the listener dispose the window if and only if the necessary conditions are met.
See this approach exemplified here and also consider the following snippet:
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window window = SwingUtilities.windowForComponent((JButton)e.getSource());
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
}
});
...
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// Call methodA() here.
// If all went ok then dispose the window, otherwise log the
// errors/exceptions and notify the user that something went wrong.
e.getWindow().dispose();
}
});
这篇关于检查触发的WindowClosing事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!