2013-12-16 91 views
1

我想說明,在JFrame選擇了與JFileChooserJTextarea選定的文件,如:顯示選定的文件

JTextArea textArea = new JTextArea(6, 12); 
contentPane.add(textArea); 
JFileChooser fileChooser = new JFileChooser(); 
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 
fileChooser.setDialogTitle("XML Datei auswählen"); 
int ret = fileChooser.showDialog(fileChooser, "auswählen"); 
if (ret == JFileChooser.APPROVE_OPTION) { 
    file3 = fileChooser.getSelectedFile().getAbsoluteFile(); 
    textArea.setText(file3.getName()); 

但它表明我的textarea的什麼都沒有,我究竟做錯了什麼?

回答

2

嘗試此方法

int ret = fileChooser.showOpenDialog(this); 
if (ret == JFileChooser.APPROVE_OPTION) { 
    String file3 = fileChooser.getSelectedFile().getAbsolutePath(); 
    textArea.setText(file3);