I have a showInputDialog. Whenever I type somethi开发者_高级运维ng to that field, I want it to be save as textfile when I click the ok button. My problem is I don't know where/how to put the listener.
Could somebody help me about this matter?
The saving code shouldn't be in the InputDialog
context, but in your code. InputDialog
is just a way to prompt for data.
String whatHeTyped = JOptionPane.showInputDialog("Type something...");
saveToFile(whatHeTyped);
No need to add actionListener just check variable value associated with JOptionPane.
Something like this:int i = JOptionPane.showConfirmDialog(null, "hi","Test Message",
JOptionPane.OK_CANCEL_OPTION);
System.out.println(i);
if(i==0){
/// OK is clicked.
}
To check for input dialog do as follows:
String i = JOptionPane.showInputDialog("hi");
System.out.println(i!=null);
If user has pressed OK
then i
will be not null even if he has not entered anything in textbox. For Cancel
button i
will be null.
精彩评论