开发者

java filechooser

开发者 https://www.devze.com 2022-12-13 02:36 出处:网络
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 click

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,开发者_StackOverflow中文版 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号