开发者

How can I put an actionlistener to the OK button of JOptionpane.showInputDialog?

开发者 https://www.devze.com 2023-04-07 04:14 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消