JFileChooser disable acept button

Im using JFileChooser (a sub class of it actually) and have a situation where I must ensure that pick an existing file.

My thoughts were that I could either

1. not allow users to type their own file name into the file box

or

2. do not allow them to choose the accept button when the files does not exist.

Exploring these ideas:

for #1 you could change the text box to be read only (not accept typing) at all times

for #2 you would have to change the accept button to read only if the file did not exist

playing with # 2, I changed the propertyChange listener

if ("SelectedFileChangedProperty".equals(evt.getPropertyName())){

if (!getSelectedFile().exists()){

// this is where you would disable the button

}

}

Does anyone have a cluehow you would go about disabling either the accept button or the text field?

[1137 byte] By [dingfeldera] at [2007-10-2 21:11:17]
# 1
couple of ideas here http://forum.java.sun.com/thread.jspa?forumID=57&threadID=643013
Michael_Dunna at 2007-7-13 23:57:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

thanks for the link.

I think I agree with some in that post that it might annoy users if the field was read only...

ok, after further exploration, here is my solution now:

override the default approveSelection method and check the file before processing...

public void approveSelection() {

if (!getSelectedFile().exists()) {

JOptionPane.showMessageDialog(this,getSelectedFile().getName() + " does not exist", "Info", JOptionPane.PLAIN_MESSAGE);

} else {

super.approveSelection();

}

}

dingfeldera at 2007-7-13 23:57:28 > top of Java-index,Desktop,Core GUI APIs...