Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of JFileChooser

 
pandu chinnu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to open a file chooser by clicking the browse button. I have managed
to have a have a open dialog box which shows me all the directory of mycomputer. However when I select a file it dosen't show the path of the file in the text field next to the browse button. How do I select a file and implement action on it?

my code was as follows:

 
mani k
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after selecting the file from your filechooser dialog,

tf.setText(chooser.getSelectedFile().getAbsolutePath();

(note : assuming tf is your text field reference)
 
pandu chinnu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, now it shows the path. However if I have to implement an action on the chosen text. Say I need to count the number of words in the chosen text document, how do I go about doing it? Say if I have a button name 'word count' . On pressing that button I have to implement an action listener. So we come to the listening method....how do we action on the selected text?

Thanks
 
mani k
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this,
import java.util.regex.*;
.......................
Pattern pat = Pattern.compile(<regexp> ;
Matcher match = pat.matcher(<file content> ;
int count = 0;
while(match.find()) {
count++;
}

note : <regexp> - pattern of your word to be counted
<file content> - you can match line by line of the file, or you
can make the file contents to stringbuffer

assumption : you are using jdk1.4 or more
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic