views:

291

answers:

5

Hey guys

I am working on a project, in java, where the user has to choose a file to be uploaded through a jfilechooser. Is there anyway to make the textfield readonly so that you can only choose files by clicking them, and not by writing in the textfield. I hope that i made my problem clear ;D

+1  A: 

I don't believe this is directly possible - and judging by the API, I believe this is not the case. You could probably simulate it by registering a listener that cleared the text box after every keypress.

However, are you sure that you want to do this? Personally I'd be very upset if I was using a file selector that was crippled in this way, especially if I'd copied a file path to the clipboard from somewhere and was forced to manually descend through a bunch of directories, amongst other situations.

Is there a particular reason you want to cripple the user interface? Because I can't see any legitimate reason to do so...

Andrzej Doyle
well, it was just an easy way to assure that the selected file was an actual file. I guess I will just have to check if the file exists before i start the upload
ev00l
+1  A: 

This is not really an attempt at an answer, but I would question WHY you would want to make the textfield read only. You are limiting users and for what benefit exactly? Changing the standard expected behaviour of the file-chooser is not usually a good approach to go down unless you have VERY good reason to do so.

So, sorry, not really an answer, but please do think about why you want to do this. As a personal preference I like being able to write in the text field. This enables me to do pasting, which allows extremely quick and simple navigation from one directory to another.

Phil
I can see why it was a crippling way to ensure that the file exists. Thanks for the help
ev00l
A: 

I think the only way to do this would be by providing your own FileChooserUI and setting it via setUI() on your JFileChooser. It's probably not much work to do this when you subclass an existing implementation from the javax.swing.plaf packages, but the problem is that you're circumventing the whole PLAF mechanism; changing the global L&F would then not work for your file chooser.

Michael Borgwardt
+1  A: 

If your intent is to make sure the user has selected a file rather than a directory (hinted at by your responses to other answers), you can use FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);. However, that is the default setting when you instantiate the file chooser, so unless your code changes that at some point, you shouldn't need to set it explicitly.

For your viewing pleasure: http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

spork
A: 

If you want to ensure that the file name entered is valid before closing the file chooser then you should be able to do something like this:

JFileChooser chooser = new JFileChooser(...)
{
    public void approveSelection()
    {
        if (getSelectedFile().exists())
            super.approveSelection();
        else
            JOptionPane.showMessageDialog(this, "Selected file does not exist");
    }
};
camickr
Thank you very much, i will try this out!
ev00l