JFileChooser File Name
Hi!
I'm using a JFileChooser to save in a local folder a file stored in my database and I'd like to set the File Name when I open the FileChooser, do you know how to do it ??
Thanks !!
TLLI
JFileChooser.setSelectedFile()?
Similar Messages
-
JFileChooser "File Name" field
Hi,
I am trying to use a JFileChooser as a component within a JDialog, and everything works fine, except that I do not understand how I can get the value of a filename typed into the "File Name:" field on the form. I've read the "JFileChooser" Java doco about five times and I can't see anything that would give me access to this field. GetName() appears to be a red herring.
Anyone got any ideas?
Thanks,
TimThanks for your swift reply.
Unfortunately perhaps I wasn't quite clear - I have added the JFileChooser as a component to a JDialog, not run it standalone via showDialog() or similar.
I am trying to get the user to select a directory and type a file name that a new file can be saved into.
Here is the code that displays the chooser:
private JPanel saveFilePnl(String folder)
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("<html>Select the folder to save the new sequence into,<br>" +
"and type in an appropriate File Name:<br>"),"North");
saveFileChooser.setCurrentDirectory(new File(folder));
SimpleFileFilter filter = new SimpleFileFilter("xml", "Nucleotide Sequences");
saveFileChooser.setFileFilter(filter);
saveFileChooser.setControlButtonsAreShown(false);
saveFileChooser.setBorder(BorderFactory.createCompoundBorder
(BorderFactory.createEtchedBorder(),
BorderFactory.createEmptyBorder(8,8,8,8) ));
panel.add(saveFileChooser, "Center");
return panel;
}This panel is added to a card on a CardLayout JDialog. After the user selects a folder and types in a filename I want to verify that they have done so before moving on to the next card. So I need to ensure that there is a name in the "File Name:" field on the chooser.
Thanks,
Tim -
Can JFileChooser file name field return all entered chars
In JFileChooser, if I want to get all entered characters from file name field, is there a method I can call?
I used method chooserUI.getFileName() or chooser.getSelectedFile(). These two methods returned texts which missed back slash and forward slash. I want to get everything what I entered in file name field. I searched and read related JFileChooser topic, but there isn't a topic about this. Can any of you know what I can do?
Thank you very much.JFileChooser isn't for entering text, it's for selecting files. If you put for example "abc/def" in the filename box, it's going to interpret that as "file def in directory abc" and your getFileName() will return "def". (You'll notice that getFileName() doesn't return the directory path, just the filename part of it, right?)
-
JFileChooser file name extention queistion
hi i would like to know if it is possible to place a file extention in the filename section when you open the save dialog. So as when the user types a file name in the file extention is added automaticly to only allow the user to save in .png. My code is as follows.
private void screenshot()
/** currently no dialog to confirm action as remains
on screen when saving screenshot! **/
int status;
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
try {
Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit ().getScreenSize ());
BufferedImage image = new Robot().createScreenCapture(rect);
try {
fc.addChoosableFileFilter(new PNGFilter());
status = fc.showSaveDialog(information);
if(status == JFileChooser.APPROVE_OPTION)
File selFile = fc.getSelectedFile();
ImageIO.write(image, "png", selFile);
JOptionPane.showMessageDialog(information, "File saved to "+ selFile);
} catch (IOException e) {
Error.SaveError(e);
catch (AWTException e) {
Error.AbstractWindowToolkit(e);
}You can always check selFile's name, and if it
doesn't end with .png replace it
with a new File with the same name, but with .png
added on.Thanks for that. I have done just that and it works great now.
if(getExtension(selFile).equals("NO_EXTENSION"))
selFile = new File(selFile.toString() + ".png");
} -
Suggest file name in JFileChooser?
I'm a newbie (student) to both Swing and Java in general. In using JFileChooser, It occurred to me that it would be nice to offer a suggested file name when saving files.
For those of you who have used MS Word, when one saves a new file, Word offers the document title or a few words of the document's first line as a suggested file name. This suggestion is displayed in the file name textbox of the save file dialog.
Is it possible to do this sort of thing in Java and with JFileChooser? If so, how?
Thanks in advance for any feedback.JFileChooser fc = new JFileChooser(".");
fc.setSelectedFile(new File("AnyFileNameYouWant.txt")); -
Easy -- default file name in JFileChooser
Hello,
I have a JFileChooser dialog ready and working. I select the directory, then input the file name and press Save.
The enhancement I would like to add to the dialog would be displaying a default name already in the FileName field once the dialog is opened. For instance "REPORT_ONE_" and then I would only add some text to the end of the default name. This would save me from typing the "REPORT_ONE_" all the time as this would already be displayed in the filename field.
I could add the "REPORT_ONE_" to the filename later, when saving the report. However appending anything to the filename "behind the scenes" is not acceptable by the customer. They need to see the filename, not guess it.
Looking forward to your help, fellow forum users.
Thank you!
auloThank you, 5p given.
However this is what I execute and in my file chooser dialog the filename field is still blank.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("html");
filter.addExtension("csv");
JFileChooser chooser = new JFileChooser(new File("REPORT_ONE_"));
chooser.setDialogTitle( "Save Report" );
chooser.setFileSelectionMode(chooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(this);
...It is in try-catch, no errors are returned.
Operating System: WindowsXP
Java Version: 1.3.1 -
Make "File name" and "Files of type" fields read-only in JFileChooser
I try to make the "File name" and "Files of type" fields read-only in JFileChooser dialog. Anybody
knows how to do? Thanks.You mean so the user can't choose the name of the file to open or save? Not much point in even using a JFileChooser, then, is there? Or did I misunderstand the question?
-
How to position the cursor in File Name area in JFileChooser??
Hi,
I have been trying to position the cursor directly to filename area in JFileChooser. But no success.
It positions itself at the directory path by default.
How should I achieve this?
The problem is that I do not know the name of this
component used for File Name area hence difficult to
use "requestFocus() or getFocus()" like methods.
Thanks for your help.
-ManojSomewhere I saw a tip that explained how to do it, but I didn't keep a link to it. But basically it's like this: JFileChooser is a Container, so getComponent(n) will return the n-th component in the JFileChooser. One of them is the one you want to give the focus to, and I expect it will be a JTextField. A little experimentation should tell you which index you need to use.
-
How to set file name in a JFileChooser to be some default String?
Dear all,
I am tring to make my JFileChooser more user-friendly, so every time that a JFileChooser show up, I would like to fill the file name with some default String, instead of just leaving them blank.
Does anybody know how to do this?
Many Thanks!look up the set...() methods of the JFileChooser api docs
-
JFileChooser does not display japanese file names
I am using JFileChooser to load some files in my application. I have some files which have japanese file names. When I try to load these files it does not even display these files for me to choose and then load them. Any ideas?
Hi,
I am trying to pick up data from an excel sheet and insert them into a database. However, once updated, they end up with '?' values.
I have tried to make font.properties.ja as the default font.properties file, which failed to display Japanese characters.
Any suggestions?
This is a part of my code:
String sJIS = "";
sJIS = new String(cell1.getStringCellValue().getBytes("8859_1"), "JISAutoDetect");
String sUTF = "";
sUTF = new String(cell1.getStringCellValue().getBytes(), "UTF-16");
System.out.println("sUTF" + sUTF + " " + "sJIS" + sJIS);
Thanks, -
JFileChooser Replacing File Name
I am using a JFileChooser as a "Save" dialog box. Here is the situation: the user has entered a proposed file name, then he/she double clicks on a directory where the file should be saved. Then they click "Save". At the point the user double clicked the directory, his/her proposed file name has been replaced by the directory name. The user may not notice it before he/she clicks "Save" thus creating a file in the directory with the same name as the directory.
This "proposed file name replacing" behavior is fine when the user clicks a file but shouldn't happen when a directory is selected. The common Windows filechooser gets this right.
Is it possible to make JFileChooser stop doing this?
Thanks!When you doubleclick, you have to check wether the selection is a file or a directory with the isDirectory() method. You can than decide to overwrite the Stringvalue or not, depending on the case.
KR,
Jan -
How to set selected file in FileChooser showSaveDialog to default file name
Hi,
How do I set selected file in JavaFX 2.0 FileChooser showSaveDialog, so I can prompt the user with a suggested default file name?
I am converting a Java Swing application I wrote a few years ago to JavaFX 2.0.
In the Swing application, I use setSelectedFile() as follows:
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setSelectedFile(new File(backupfile));
jFileChooser.setCurrentDirectory(new File(outputDirectory));
FileFilter filter = new FileNameExtensionFilter("Comma Delimited (*.csv)", "csv");
jFileChooser.addChoosableFileFilter(filter);
jFileChooser.setFileFilter(filter);
filter = new FileNameExtensionFilter("XML Document (*.xml)", "xml");
jFileChooser.addChoosableFileFilter(filter);
jFileChooser.setAcceptAllFileFilterUsed(false);
jFileChooser.setDialogTitle("Export File");
int returnVal = jFileChooser.showDialog(jFrame, "Export");
This would show a file chooser with the file name text field pre-populated with a default backup file name.
I can't find an equivalent in JavaFX.
Also, in the Swing application, I was able to determine which extension filter was selected at run time using getFileFilter().getDescription() as follows:
String extension = jFileChooser.getFileFilter().getDescription();
if (extension.equals("XML Document (*.xml)")) { ...
I can't find an equivalent in JavaFX.
Thanks,
BarryYou can use the open sequence file method on the application manager (then you only need to wire the file path)
Rodéric L
Certified LabVIEW Architect -
How to get the full path instead of just the file name, in �FileChooser� ?
In the FileChooserDemo example :
In the statement : log.append("Saving: " + file.getName() + "." + newline);
�file.getName()� returns the �file name�.
My question is : How to get the full path instead of just the file name,
e.g. C:/xdirectory/ydirectory/abc.gif instead of just abc.gif
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class FileChooserDemo extends JFrame {
static private final String newline = "\n";
public FileChooserDemo() {
super("FileChooserDemo");
//Create the log first, because the action listeners
//need to refer to it.
final JTextArea log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
//Create a file chooser
final JFileChooser fc = new JFileChooser();
//Create the open button
ImageIcon openIcon = new ImageIcon("images/open.gif");
JButton openButton = new JButton("Open a File...", openIcon);
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would open the file.
log.append("Opening: " + file.getName() + "." + newline);
} else {
log.append("Open command cancelled by user." + newline);
//Create the save button
ImageIcon saveIcon = new ImageIcon("images/save.gif");
JButton saveButton = new JButton("Save a File...", saveIcon);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would save the file.
log.append("Saving: " + file.getName() + "." + newline);
} else {
log.append("Save command cancelled by user." + newline);
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
buttonPanel.add(saveButton);
//Explicitly set the focus sequence.
openButton.setNextFocusableComponent(saveButton);
saveButton.setNextFocusableComponent(openButton);
//Add the buttons and the log to the frame
Container contentPane = getContentPane();
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(logScrollPane, BorderLayout.CENTER);
public static void main(String[] args) {
JFrame frame = new FileChooserDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
frame.pack();
frame.setVisible(true);simply use file.getPath()
That should do it!Thank you !
It takes care of the problem !! -
Need help adding a default file name in a file chooser of save dialog type
I need to create a file chooser with save dialog type, how can I add a highlighted default file name into the File Name textfield? As in Microsoft Word, when you want to save a document, a default file name Doc1.doc will appear in the File name text field of the file chooser even when you change to other directories.
For JRE 1.4.0 you can use this fix:
public class FileChooserFix implements PropertyChangeListener {
private String fileName;
* @see PropertyChangeListener
public void propertyChange(PropertyChangeEvent ev) {
JFileChooser chooser = (JFileChooser)ev.getSource();
if (JFileChooser.FILES_ONLY == chooser.getFileSelectionMode()) {
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(ev.getPropertyName())) {
File selectedFile = (File)ev.getNewValue();
if (selectedFile != null) {
// remember fileName of selected file
fileName = selectedFile.getName();
if (fileName != null &&
JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(ev.getPropertyName())) {
// reset selected file
File directory = (File)ev.getNewValue();
chooser.setSelectedFile(new File(directory, fileName));
* Convenience method to create a fixed file chooser.
* @return fixed file chooser
public static JFileChooser create() {
JFileChooser chooser = new JFileChooser();
chooser.addPropertyChangeListener(new FileChooserFix());
return chooser; -
Hi,
I have a method in my application called "export" which exports the JPanel content into a file.
I used the JFileChooser to let the users choose their desired location for saving the file.
The problem is unless user explicitly types in the file format, it saves the file with no extension.
How can I have formats like jpg, png in the File Type drop down menu.
The code is on a different computer, so dont worry about syntax errors, I just write the important bits:
JFileChooser jfc = new JFileChooser();
int answer = jfc.showSaveDialog(this);
if(answer==jfc.APPROVE_OPTION) {
try {
File file = jfc.getSelectedFile();
ImageIO.write(Graph, "png", file); //Graph is what that needs to be exported
} catch () {}any help, highly appreciated, thanksTheParthian wrote:
still got the problem,
I implemented the FileFilter and its two methods, but what I get in the menu is the string i used in the getDescription method.Yeah, that's what the description is meant for.
what if i want to include more than one extension?Add more filters.
Also, even if I choose that as the file type, it still saves it with no extension. Do I need to get the string from the menu and add it
manually to the end of file name?Yes, of course. (That is, you get the selected filter, determine the appropriate extension and add it, if the user did not explicitly specify an extension.)
BTW - why did you start a new thread? You could have continued in your old one http://forums.sun.com/thread.jspa?threadID=5334819&messageID=10440802#10440802.
Maybe you are looking for
-
Multiple physical connections on a single repository
Hi, I have customers who have multiple databases, one for every location. This Databases are identical. So I want to create a single repository and the users from location 1 to see only his data and the users from location 2 to see only his data. Can
-
Hi there thanks in advance. I used to have a Macbook that I upgraded to 4gb Ram for music purposes - I run pro tools on it. My macbook was stolen in a break in on Friday! Contacted insurance and they are quite rightly asking for a receipt but as it's
-
How do I get it to ring. The button is turned on correctly and I have re-set the phone
-
I created ADFS v2 on one of windows 2008 R2 serves. The AD Sync works fine. However, I can't login the ADSF server using adfssvr.mydomain.com/adfs/ls/idpinitiatedsignon.aspx even we have correct internal DNS settings. Troubleshooting steps: 1. I rea
-
Adobe Photoshop CS6 crash message, but I have Photoshop 5.1?
Adobe Update Manager says all programs up to date. Crashes most frequently occur when using Blur or other Filters. The posted recommended "fix" 12.0.4 says it applies to AMD Catalyst, but my PC has Intel processor. It says it applies to CS6, but I