Say If i have two classes, in each class is a different JFrame, e.g JFrame A and JFrame B ( in seperate classes).
Now from the constructor of JFrame A I may push a button with an actionlistener attached, which will instantiate the other cl开发者_Go百科ass, thus creating JFrame B. The problem is when JFrame B is created, both the JFrames are visible. If i close JFrame B, then JFrame A closes as well. How can i make it so only JFrame B closes?
Thanks
edit DISPOSE_ON_CLOSE
does not work for me, it closes all the jframes.
some sample code:
public class classone {
public classone() {
JFrame a = new JFrame("this is A");
classtwo newFrame = new classtwo();
}
}
public class classtwo {
public classtwo() {
Jframe b = new JFrame("this is B");
b.setDefaultCloseOperation(b.DISPOSE_ON_EXIT);
}
}
please ignore any syntax errors, just for demonstration.
For the JFrame B, set the default close operation to "dispose" as shown below :
frameB.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Then closing the child windows won't shut down your entire application.
HTH ! ;-)
Do you have DISPOSE_ON_CLOSE
on one frame and EXIT_ON_CLOSE
on the other? If so then that would explain why your program is exiting prematurely. Ensure that all frames are set to DISPOSE_ON_CLOSE
.
I got question now. Just when you create an instance of a Window tell how this object live, Review this code
...
new JFrame(){
@Override
public synchronized void addWindowListener(WindowListener l) {
// You may ask here also add windowClosing method and look at my previous post
super.addWindowListener(l);
}
}.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
....
Just DO_NOTHING_ON_CLOSE and addWindowListener in WindowClosing method show a JOptionPane.showConfirmDia and if result return no(1) then return; else system.exit(0); its all
I see my first StackOverFlow post ,What a shame! I'm editting my post.here you are;
Until now , I realize Depending developing software approachs Swing getting older. I'm missing a technology like Microsofts XAML.
soyatec inc. has some deals using XAML with java you may have a look but "In my opinion" not successfull work.Anyway...
JFrame frame=new JFrame();
frame.addWindowListener(new WindowListener() {
@Override
public void windowClosing(WindowEvent e) {
int result= JOptionPane.showConfirmDialog(JOptionPane.getRootFrame() //or your parent swing element
, "Sure ?");
switch (result) {
case 1:
break;
default:
System.exit(0);
break;
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
);
精彩评论