setDefaultCloseOperation for JOptionPane
843804Oct 11 2004 — edited Dec 11 2006hi guys
I need to do Do_Nothing on the exit for a JOptionPane. The exit is to be handled by the 'ok' or 'cancel' button. setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); compiles well but does not work. Please help me. The code is below:
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JTextField;
import java.beans.*; //property change stuff
import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
import java.awt.*;
/* 1.4 example used by DialogDemo.java. */
public class passAsk extends JDialog implements PropertyChangeListener{//implements ActionListener{
private String password = null;
private JOptionPane optionPane;
private String btnString1 = "Enter";
private String btnString2 = "Cancel";
private static ImageIcon icon2 = createImageIcon("");
private static String fileName = "password.txt";
private static String line;
/**
* Returns null if the typed string was invalid;
* otherwise, returns the string as the user entered it.
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = DialogDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public String getValidatedText() {
return password;
}
/** Creates the reusable dialog. */
public passAsk(Frame frame)
{
JOptionPane optionpane = new JOptionPane();
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
String s = (String)optionpane.showInputDialog(frame,"Type in a password",
"Password for Close Operation",
JOptionPane.PLAIN_MESSAGE,
icon2,
null,"");
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
try{
PrintWriter print = new PrintWriter( new BufferedWriter( new FileWriter( fileName ) ) );
print.println(s);
print.close();
BufferedReader in = new BufferedReader(new FileReader( fileName ) );
line = in.readLine();
while ( line != null ) // continue until end of file
{
line = in.readLine();
}
in.close();
}
catch ( IOException iox )
{
System.out.println("Problem reading " + fileName );
}
DialogDemo.labelsetting("Choose from the menu to start server");
}
else {
labelsetting("password not set");
}
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (isVisible()
&& (e.getSource() == optionPane)
&& (JOptionPane.VALUE_PROPERTY.equals(prop) ||
JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = optionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
}
}
}