Get the username entered by the user from the Save JOffice dialog box

This answer to this question may seem obvious, but I'm really struggling a bit with it. I searched for JFileChooser methods in the API, and I looked through some of the questions already asked and answered here on stackoverflow.

My question is that. In my program, I allow the user to enter a file name that I will use to create a new file that I will write. How do you get the text entered by the user in the text box next to the Save As: label in the Save dialog box provided by JFileChooser? Is there a JFileChooser method that will allow me to get the text entered by the user? Or will I have to go through another class or do something else to get this text?

Thank you so much to everyone who answers. I am very late, and this program should go in a few hours (this means that I will have another sleepless night). Despair may be too strong a word, but I'm close enough.

+5
source share
3 answers
JFileChooser chooser=new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORY_ONLY);
chooser.showSaveDialog(null);

String path=chooser.getSelectedFile().getAbsolutePath();
String filename=chooser.getSelectedFile().getName();

...... in the file name variable you will get the file name entered by the user

+11
source

After you open the save file dialog and determine that the user wants to save the file, take the file name with this:

String filename = mySaveDialog.getSelectedFile().getName();
+4
source

JFileChooser getSelectedFile(). File.

showSaveDialog(), File ( getName()). , . (, ... , :))

.

+3

All Articles