有没有办法暂时禁用JFileChooser
窗口中的文件 Select 器?
EDIT:
我在这里搜索了一下,找到了Start a JFileChooser with files ordered by date号帖子.如果我理解正确,那么根据它,这段代码实际上应该能够访问JFileCHooser
中的FilePane
(当然,我首先下载了SwingUtils.java类):
JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
但当我这么做的时候,我在NetBeansIDE中得到了一个错误:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
我还找到了post How to disable file input field in JFileChooser?,根据它,这段代码访问JFileChooser的文本字段,显示所选的文件名:
import java.awt.Frame;
import java.lang.reflect.Field;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.plaf.metal.MetalFileChooserUI;
public class FileChooser {
public static void main(String[] args) throws Exception{
Frame f = new JFrame();
JFileChooser jFileChooser = new JFileChooser();
MetalFileChooserUI ui = (MetalFileChooserUI)jFileChooser.getUI();
Field field = MetalFileChooserUI.class.getDeclaredField("fileNameTextField");
field.setAccessible(true);
JTextField tf = (JTextField) field.get(ui);
tf.setEditable(false);
tf.setEnabled(false);
jFileChooser.showDialog(f, "Select");
f.dispose();
}
}
所以,通过判断所有可用字段,我发现有一个名为"filePane".所以我冒了一个风险,试着模仿上面的代码,只是做了一些修改,这样FilePane
个目标就会变成这样:
Field fieldB = MetalFileChooserUI.class.getDeclaredField("filePane");
fieldB.setAccessible(true);
FilePane filePane = (FilePane) fieldB.get(ui);
filePane.setEnabled(false);
我原以为上面的代码会禁用JFileChooser
窗口的文件 Select 部分,但不幸的是,它什么也没做.