English 中文(简体)
java filechooser
原标题:

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

问题回答

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

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");
    }
};

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...

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.

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签