java JfileChooser 设置文件下载路径

用 JFileChooser的时候,如果设置了 只选择路径,是可以只选择路径的,不过标签还是显示 “文件名:”,会导致用户困惑,所以,可以将其换成 “文件路径:”,方法如下“:
public static void main(String[] args) {
JFileChooser jfcWorkFolder = getJFileChooser("d:\\");
jfcWorkFolder.setDialogTitle("title");
jfcWorkFolder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

int iState = jfcWorkFolder.showDialog(jfcWorkFolder, "ok");

}
public static JFileChooser getJFileChooser(String defaultName){
JFileChooser jfcWorkFolder = new JFileChooser(defaultName);
try {
JPanel cons = (JPanel) jfcWorkFolder.getComponent(3);
Component jp = cons.getComponent(0);
System.out.println(jp.getClass());
JPanel la = (JPanel) jp;
JLabel jl = (JLabel) la.getComponent(0);
System.out.println(jl.getText());
jl.setText("文件路径:");

} catch (Exception e) {

}

return jfcWorkFolder;
}



如果你是用的其他的LookAndFeel 导致找不到该JLabel,那你可以用 JPanel.remove(i);的方法试出该JLabel的位置,然后再设置其 text.

你可能感兴趣的:(java)