Getting filename from JFileChooser embedded in a .form

After much confusion, I was able to copy a dialog from another application which has a JFileChooser in it as well as a couple of additional entry fields. The JFileChooser portion of the dialog is used to save a file. I don't understand how to return the file name that the user keyed into the file name text field. The Save and Cancel buttons work fine, but how do I access the keyed in text?

I can obviously do it without using IDEA forms, but I am trying to do it the "nice" way.

5 comments
Comment actions Permalink

Hello Rich,

After much confusion, I was able to copy a dialog from another
application which has a JFileChooser in it as well as a couple of
additional entry fields. The JFileChooser portion of the dialog is
used to save a file. I don't understand how to return the file name
that the user keyed into the file name text field. The Save and Cancel
buttons work fine, but how do I access the keyed in text?

I can obviously do it without using IDEA forms, but I am trying to do
it the "nice" way.


Sorry, I don't quite understand your question. If you need to access the
filename entered in JFileChooser from Java code, you'll need to write code
which accesses that. I don't see how this could be "nicer" in any way.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

I guess my confusion stems from the following. One would normally get the keyed in fiile thusly:

JFileChooser fc = new JFileChooser();
int result = fc.showSaveDialog(parentFrame);
....
file = fc.getSelectedFile();

however, in my case the file that is returned is always null. I also can't put in the line:
int result = fc.showSaveDialog(parentFrame);
because I would get a "second" JFileChooser since it already appears on the form.

Perhaps I'm having a "blonde moment", but I don't see how to indicate that the JFileChooser is of the "save" variety, and also why the keyed in file name always returns a null with

int result = fc.showSaveDialog(parentFrame);

0
Comment actions Permalink

Sorry, cut and pasted the last line "int result = fc.showSaveDialog(parentFrame);" wrong, it should have been "file = fc.getSelectedFile();"

0
Comment actions Permalink

Found a method that I hadn't seen: fc.setDialogType(JFileChooser.SAVE_DIALOG);
which answers one of my questions, but still don't understand why the returned file is always null;

0
Comment actions Permalink

After removing:

{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE
$$$setupUI$$$();
}

and

/**

  • @noinspection ALL

*/
public JComponent $$$getRootComponent$$$() {
return backgroundPanel;
}


and renaming $$$setupUI$$$() to setupUI, calling setupUI from the constructor in the .java file, and deleting the .form file then file = jFileChooser.getSelectedFile(); returns the file name as expected. Prior to doing the things mentioned above it returned null.

There seems to be bug afoot.

0

Please sign in to leave a comment.