Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,805 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,663 people online right now. Registration is fast and FREE... Join Now!




file chooser

 

file chooser, to put multiple file into the TextArea

qema_86

4 Jun, 2009 - 05:27 PM
Post #1

New D.I.C Head
*

Joined: 4 Jun, 2009
Posts: 12


My Contributions
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





User is offlineProfile CardPM
+Quote Post


NeoTifa

RE: File Chooser

4 Jun, 2009 - 06:30 PM
Post #2

Yay caek! ZOMG!!!
Group Icon

Joined: 24 Sep, 2008
Posts: 6,525



Thanked: 83 times
Dream Kudos: 150
My Contributions
Are you wanting to append the multiple files in the same area? (needed clarification)
User is offlineProfile CardPM
+Quote Post

pbl

RE: File Chooser

4 Jun, 2009 - 06:34 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,962



Thanked: 1187 times
Dream Kudos: 450
My Contributions
You called the method that returns only 1 file
If multiple selections is enabled you should call the other one
CODE

// 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



User is online!Profile CardPM
+Quote Post

qema_86

RE: File Chooser

4 Jun, 2009 - 07:29 PM
Post #4

New D.I.C Head
*

Joined: 4 Jun, 2009
Posts: 12


My Contributions
thanks for helping. I had try to put the code. but still not work. i tried to put the
CODE
plain1.add(f);
The plain1 is the TextArea.

You mean that the add function is to add the other files?
User is offlineProfile CardPM
+Quote Post

pbl

RE: File Chooser

4 Jun, 2009 - 07:51 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,962



Thanked: 1187 times
Dream Kudos: 450
My Contributions
QUOTE(qema_86 @ 4 Jun, 2009 - 07:29 PM) *

thanks for helping. I had try to put the code. but still not work. i tried to put the
CODE
plain1.add(f);
The plain1 is the TextArea.

You mean that the add function is to add the other files?


try
CODE

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

User is online!Profile CardPM
+Quote Post

qema_86

RE: File Chooser

11 Jun, 2009 - 12:30 AM
Post #6

New D.I.C Head
*

Joined: 4 Jun, 2009
Posts: 12


My Contributions
tq pbl. you help me to solved it. i write my code as below and it's work

CODE

for (int i=0;i < selectedFiles.length;i++)
        
        plain1.append(selectedFiles[i].getName());



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 09:41PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month