Question: JFileChooser

Ads

 
 
 

Share on Google+Share on Google+

zam
JFileChooser
1 Answer(s)      7 years ago
Posted in : Java Beginners
How to create a GUI application that allows a user to select a text file (using the JFileChooser). The program will then count the number of words in the selected file and display the result containing the name of the file, its size (in bytes) and the number of word it contains.

(Hint: You can use the StringTokenizer class or the String split() method to count the number of words which are separated by whitespace characters such space and tab)
Ads
View Answers

March 15, 2010 at 4:54 PM


Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class CountWordsFromFile extends JFrame {

JButton button;
JPanel p;
JLabel l1,l2,l3;
String dirName = "C:\\Answers\\ImagesAndText\\";
int count=0;

CountWordsFromFile(){
p=new JPanel(new GridLayout(4,1));
l1=new JLabel();
l2=new JLabel();
l3=new JLabel();
button=new JButton("Get File Details");
p.add(l1);
p.add(l2);
p.add(l3);
p.add(button);

l1.setVisible(false);
l2.setVisible(false);
l3.setVisible(false);
add(p);
setVisible(true);
pack();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(dirName));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

FileNameExtensionFilter filter = new FileNameExtensionFilter(
".txt and .java files", "txt", "java");
chooser.setFileFilter(filter);

try {
int code = chooser.showOpenDialog(null);
if (code == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
Scanner input = new Scanner(selectedFile);
while (input.hasNext()) {
++count;
input.next();
}
String f=selectedFile.getName();
long size=selectedFile.length();
l1.setText("File Name is: "+f);
l2.setText("File Size is: "+Long.toString(size));
l3.setText("No of words : "+Integer.toString(count));
l1.setVisible(true);
l2.setVisible(true);
l3.setVisible(true);

}

} catch (Exception f) {
f.printStackTrace();
}
}
});
}
public static void main(String[] a) {
CountWordsFromFile c=new CountWordsFromFile();
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
JFileChooser - Swing AWT
JFileChooser  Hi,iam using jfilechooser to select a directory....now... ActionListener { JButton button; JFileChooser chooser; String... JFileChooser(); chooser.setCurrentDirectory(new java.io.File
Using a JFileChooser - Java Beginners
Using a JFileChooser  Dear Editor, How to select an image from JFileChooser & add the image in mySQL5 database? Thanks in advanced. Regards... void main(String[] args) { JFileChooser chooser = new JFileChooser(); int
Advertisements
JFileChooser - Java Beginners
JFileChooser  How to create a GUI application that allows a user to select a text file (using the JFileChooser). The program will then count...(ActionEvent e){ JFileChooser chooser = new JFileChooser
How to open a file in java using JFileChooser
How to open a file in java using JFileChooser  How to open a file in java using JFileChooser
wait until a file is opened using JFileChooser
JFileChooser to open a file. b. call a funtion that takes in the filename as argument...){ JFileChooser chooser=new JFileChooser(); int ret = chooser.showDialog
How to select default FileFilter when creating a JFileChooser dialog?
How to select default FileFilter when creating a JFileChooser dialog? ... default FileFilter when creating a JFileChooser dialog? Thanks   Hi, Following code can be used: JFileChooser c = new JFileChooser
Java Program - Swing AWT
Java Program  A Java Program that display image on ImageIcon after selecting an image from the JFileChooser
Java Code - Swing AWT
Java Code  Write a Program using Swings to Display JFileChooser that Display the Naem of Selected File and Also opens that File
java - Swing AWT
java  iam getting the xml result then i'll save my result in folder using jfilechooser in swings.give me any examples
Java file browse
file or directory from a list. The JFileChooser class opens up a file browser... returns the name of the file. showOpenDialog()- This method of JFileChooser...()- This method of JFileChooser class returns the selected file. getName()- This method
Java file browser
. It has provide this utility too by introducing the class JFileChooser. Through... of JFileChooser class open the dialog and allow to select the file. getSelectedFile()- This method of JFileChooser class returns the selected file. getName
menuItem and actionPerformed
e) { JFileChooser chooser=new JFileChooser...) { JFileChooser chooser=new JFileChooser...) { JFileChooser chooser=new JFileChooser
Java File Chooser
purposes. Among them, JFileChooser is a standard dialog for selecting a file from.... getSelectedFile()- This method of JFileChooser class returns the selected file... FileChooser { public static void main(String[] args) { JFileChooser chooser = new
java swing - Java Beginners
Maheshwari.  Hi Friend, If you want to use JFileChooser then use...(String[] args) { JFileChooser chooser = new JFileChooser
How to get filename in JTextArea in following case?
saveasdirectorybuttonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fc = new JFileChooser(); fc.setSelectedFile(new File("c:/Temp/anyFilename.txt... actionPerformed(ActionEvent e){ JFileChooser fc = new JFileChooser
JFRAME - Java Beginners
() { public void actionPerformed(ActionEvent e) { JFileChooser chooser=new JFileChooser(); chooser.showOpenDialog(null... actionPerformed(ActionEvent e) { JFileChooser chooser=new JFileChooser
Open TextFile on JButton Click - Java Beginners
Open TextFile on JButton Click  Hello Sir I Want to open TextFile on JButton Click. plz Help Me  Hi Friend, Do you want to simply open the text file or you want to open the file using JFileChooser,read that file
File transfer to teh server - Java Beginners
an application where the user select several fiels using JFileChooser... { JFileChooser jf = new JFileChooser("C:"); jf.setMultiSelectionEnabled..."); JFileChooser jf2 = new JFileChooser(allinone); int returnVal
java code - Java Beginners
){ JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(null...(ActionEvent e){ JFileChooser chooser = new JFileChooser(); int returnVal... actionPerformed(ActionEvent e){ JFileChooser chooser = new JFileChooser(); int
Concatenate 2D arrays
Concatenate 2D arrays  JFileChooser jf = new JFileChooser(); jf.setMultiSelectionEnabled(true); int returnVal = jf.showOpenDialog(frame); File[] file = jf.getSelectedFiles(); for(int i=0;i<file.length;i
java swings - Java Beginners
(imgPanel); } public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File
java swings - Java Beginners
) { JFileChooser chooser = new JFileChooser(); int returnVal
java swings - Java Beginners
(ActionEvent e) { JFileChooser chooser = new JFileChooser
How I Choose a folder - Java Interview Questions
static void main(String[] args) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES
Java get File Type
) of class JFileChooser returns the file type.       Here...;chooser = new JFileChooser();   File file = 
File format validation and text field validation in java swings
File format validation and text field validation in java swings   i am reading a jpg image file throuh jfilechooser in java swings,when we press browse button for selecting file it will display filechooser in this process when
java swings - Java Beginners
(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int returnVal...) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new
File transfer from client to server - Java Beginners
File transfer from client to server  hi,, I've been trying to make an application where the user select a file using JFileChooser and then the program directly send it to the server using client/server sockets, I've tried
Error in laodin and saving the image . - Swing AWT
facing problem while laoding and saving the image with help of JFileChooser class..., null); } public static void main(String[] args) { JFileChooser chooser = new JFileChooser(); chooser.addChoosableFileFilter(new ImageFileFilter()); int
Java Code - Java Beginners
Java Code  A Java Program to load Image using Swings JFileChooser...) { JFileChooser chooser = new JFileChooser(); chooser.addChoosableFileFilter(new... fi=file.getName(); File f = new File(fi); JFileChooser chooser1 = new
uploading a text file into a database
(ActionEvent e){ JFileChooser chooser=new JFileChooser
Tutorials   
Java Spring Hibernate Struts Training Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include?

Ads

 
Advertisement null

Ads