file chooser
Page 1 of 1
file chooser to put multiple file into the TextArea
#1
Posted 04 June 2009 - 05:27 PM
I had create the GUI program. I used the file chooser in my program. I want to select the multiple files so that all the files chosen is appear in the TextArea. My problem is, I can select the multiple file from the file chooser but after i click the open button, only one file is appear in the TextArea. Howcan I put the multiple files into the TextArea. Below is my code of the file chooser.
[code]
private void browse1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFiles = fileChooser.getSelectedFile();
plain1.setText(selectedFiles.getName());
}
}
[/code
[code]
private void browse1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFiles = fileChooser.getSelectedFile();
plain1.setText(selectedFiles.getName());
}
}
[/code
#3
Posted 04 June 2009 - 06:34 PM
You called the method that returns only 1 file
If multiple selections is enabled you should call the other one
hope this help
If multiple selections is enabled you should call the other one
// this return the first selected file
File selectedFile fileChooser.getSelectedFile();
// this one (notice the ending S) returns an arrayof the selected files
File[] selectedFile fileChoosergetSelectedFiles();
// then you can
for(File f : selectedFile) {
textField.add(f);
}
hope this help
#5
Posted 04 June 2009 - 07:51 PM
qema_86, on 4 Jun, 2009 - 07:29 PM, said:
thanks for helping. I had try to put the code. but still not work. i tried to put the
You mean that the add function is to add the other files?
plain1.add(f);The plain1 is the TextArea.
You mean that the add function is to add the other files?
try
System.out.println("Number of file sleected: " + selectedFile.length());
for(int i = 0; i < selectedFile.length; i++) {
System.out.println("Adding: " + fileSelected[i] + " as file " + i);
plain1.append(fileSelected[i].toString + "\n");
}
at least you'll see if you selected the good number of files and extracted their name correctly
not 100% sure what add(object) to a textArea does
Page 1 of 1

Start a new topic
Add Reply




MultiQuote
| 


