|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-29-2008, 07:37 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 14
|
|
|
JFrame Conditional Close
I want the JFrame to be closed conditionally, based on a yes, no or cancel dialog.
I am not able to control it for "Cancel" and "No" button.s. Every time the JFrame Closes.
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
// Displaying a JFrame at the center of the screen using Java Toolkit Class
public class jFrameDemo extends JFrame {
jFrameDemo() {
setTitle("Center a Frame on Screen"); // jframe title
setSize(400, 300); // jframe size
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
// java jframe close
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(null, "Are you sure ?") == JOptionPane.YES_NO_OPTION) {
setVisible(false);
dispose(); // jframe exit
} else {
}
}
});
}
public static void main(String[] args) {
jFrameDemo cfd = new jFrameDemo();
cfd.setVisible(true);
}
}
|
|

06-29-2008, 07:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
You have to use YES_OPTION only. Got it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:03 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,144
|
|
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FrameDemoRx extends JFrame {
FrameDemoRx() {
setSize(200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int n = 99;
do {
n = JOptionPane.showConfirmDialog(null,
"Are you sure ?");
String s = "unknown";
if(n == JOptionPane.YES_OPTION)
s = "YES_OPTION = " + JOptionPane.YES_OPTION;
if(n == JOptionPane.NO_OPTION)
s = "NO_OPTION = " + JOptionPane.NO_OPTION;
if(n == JOptionPane.CANCEL_OPTION)
s = "CANCEL_OPTION = " + JOptionPane.CANCEL_OPTION;
if(n == JOptionPane.CLOSED_OPTION)
s = "CLOSED_OPTION = " + JOptionPane.CLOSED_OPTION;
System.out.println("s = " + s);
} while(n != -1);
}
});
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FrameDemoRx();
}
}
|
|

06-29-2008, 08:03 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 14
|
|
|
I changed to the YES_OPTION only. But I still experience the same probelm
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:08 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Oops, you have another mistake too. You have set the default close operation to exit. So what happened is, what ever the option you selected at the time frame has a process to complete. So change the default close operation to DO_NOTHING_ON_CLOSE. It should work.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:12 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 14
|
|
Originally Posted by hardwired
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FrameDemoRx extends JFrame {
FrameDemoRx() {
setSize(200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int n = 99;
do {
n = JOptionPane.showConfirmDialog(null,
"Are you sure ?");
String s = "unknown";
if(n == JOptionPane.YES_OPTION)
s = "YES_OPTION = " + JOptionPane.YES_OPTION;
if(n == JOptionPane.NO_OPTION)
s = "NO_OPTION = " + JOptionPane.NO_OPTION;
if(n == JOptionPane.CANCEL_OPTION)
s = "CANCEL_OPTION = " + JOptionPane.CANCEL_OPTION;
if(n == JOptionPane.CLOSED_OPTION)
s = "CLOSED_OPTION = " + JOptionPane.CLOSED_OPTION;
System.out.println("s = " + s);
} while(n != -1);
}
});
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FrameDemoRx();
}
}
This snippet does not solve the purpose of Closing the JFrame conditionally. Can you please help in this regard ?
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:13 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
You have done the same error too here.
setDefaultCloseOperation(EXIT_ON_CLOSE);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:13 AM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 14
|
|
Originally Posted by Eranga
Oops, you have another mistake too. You have set the default close operation to exit. So what happened is, what ever the option you selected at the time frame has a process to complete. So change the default close operation to DO_NOTHING_ON_CLOSE. It should work.
Originally Posted by Eranga
You have done the same error too here.
setDefaultCloseOperation(EXIT_ON_CLOSE);
Thank you so much. It worked for me. :-)
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:16 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Nice to hear that.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-29-2008, 08:46 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 194
|
|
cross-posted. thanks for wasting my time.
|
|

06-29-2008, 08:49 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Don't worry Fubarable. Some people doesn't believe what we says at once.
Anyway, you are on the suns' forum too, isn't it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|