java - JFileChooser cancel button returning APPROVE_OPTION -


This is my first question, so please interact with me. I have JFileChooser here, and even though I hit "Cancel" in the window, it opposes CANCEL_OPTION as the return APPROVE_OPTION. This is a dialog that I would like to save on opening like this

< Code> returnVal_2 = fileChooser_2.showSaveDialog (frame);

And here's the start and it's use

  fileChooser_2 = new JFileChooser (); fileChooser_2.addActionListener (New ActionListener) {@Override Public Null ActionPerformed (ActionEvent e) {if (returnVal_2 == JFileChooser.APPROVE_OPTION) {savePath = fileChooser_2.getCurrentDirectory (); println ("yes");} else if (returnVal_2 = = JFileChooser.CANCEL_OPTION) {System.out.println ("no");}}});   

This works when I hit "Save" (which by the way, also returns APPROVE_OPTION) as it is expected.

You are checking return values ​​in an action listener. Applying your mechanism () method to before Your file selector has returned the result to returnVal_2 . Because your variable is initially 0 and APPROVE_OPTION is also 0 because you think the APPROVE_OPTION has been returned is.

Instead, you forget about the action listener and just check the return value after the call to showSaveDialog () :

 < Code> fileChooser_2 = new JFileChooser (); ReturnVal_2 = fileChooser_2.showSaveDialog (frame); If (returnVal_2 == JFileChooser.APPROVE_OPTION) {savePath = fileChooser_2.getCurrentDirectory (); Println ("yes"); } And if (returnVal_2 == JFileChooser.CANCEL_OPTION) {System.out.println ("no"); }    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -