The purpose of this JAVA project is to use threads to separate a long running pr
ID: 3670905 • Letter: T
Question
The purpose of this JAVA project is to use threads to separate a long running process from the event dispatch thread. You will use a JFileChooser to select one or more text files from your local file system. Display these files (including the path) in a JList. After the user is finished adding files the application will count the vowels in each file and display the results for each file in a second JList.
Create the user interface with the these features:
The application (the class with the main method) must be named VowelCounterApp.java. You may use more classes if desired.
Create a user interface with a button that when clicked displays a JFileChooser. If the user selects a text file, display the file name, including the path, in a JList. Allow the user to click this button and add as many files as desired. Filter the file dialog so only folders and .txt files are displayed. Properly handle the situation where the user cancels the JFileChooser. Review the material in the textbook if you are unfamiliar with the JFileChooser class.
Include a button that when clicked processes the selected text files from the JList. Processing requires each file to be read and the vowels counted. This work must be done in a separate thread. Process each file in a different thread. This means a thread separate from the event dispatch thread and separate from the main processing thread.
Include a "clear" button that will return the application to its beginning state. "Beginning state" means as if the application had been restarted.
Display the file name and the number of vowels in a second JList. The order the files appear in the second JList doesn't have to match the order of the files in the first JList.
Add a JScrollPane to each JList so scroll bars appear as necessary.
Process only text files. Review the material in the textbook or the prerequisite course if you are unsure how to read a text file. To really test this application you need to either process a great many files at one time or you need a smaller number of really big text files. You can find some large text files by searching the Internet for "large text files".
Review the material on JList in the Week 1 online content. There is a page there describing how to update a JList.
Provide a button that when click displays a dialog with "help" information that explains how the application works.
Update all user interface elements on the event dispatch thread only.
T Text Document T Text Document T Text Document T Text Document C:Usersgrantd Documen. C:UsersgrantdNDocumen.. C:Users grantd Documen. CUsers grantd Documen. 54114 27301 6771 70 ] tabprotosrv.txt tdeserver.txt 413293 7/22/20 187774 7/22/20 45596 7/22/20 254 11/24/2 tdeserver 2.txt cop1001.txt Count Vowels Count Vowels in Big Files! C:lUserslgrantdiDocumentsltextiRandomNovel.txt C:lUsersigrantdiDocumentsltextiRandomText.txt C:lUsersigrantdiDocumentsltextiSherlock Holmes.txt C:lUserslgrantdiDocumentsltexticop1001.txt C:lUserslgrantdiDocumentsltextiweb sites.txt rantd Documentsltexticop1001.txt has 70 vowels. rantd Documentsltextiweb sites.txt has 692 vowels. rantd DocumentsltextiRandomText.txt has 6,720 vowels. rantd DocumentsitextiRandomNovel.txt has 737,376 vowels. rantd Documentsltexti Sherlock Holmes.txt has 1,933,144 vow Add Files Process Clear HelpExplanation / Answer
JFileChooser();
JFileChooser(String path);
// The first constructor takes no arguments and uses the default directory as the starting point for all of its dialog boxes.
The second constructor takes a String argument containing a valid path. This path will be the starting point of the objects’s dialog box.
A JFileChoose object can display two types of predefined dialog boxes:
Open File dialog box: lets the user browse for an existing file to open.
Save File dialog box: lets the user browse to a location to save a file.
Both methods return an integer that indicates the action taken by the user to close the dialog box. We can compare the return value to one of the following constants:
JFileChooser.CANCEL_OPTION: indicates that the user clicked the Cancel button.
JFileChooser.APPROVE_OPTION: indicates that the user clicked the OK button.
JFileChooser.ERROR_OPTION: indicates that an error occurred, or the user clicked on the standard close button on the window to dismiss it.
Example code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
void openFile() {
final JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
final File selected = fileChooser.getSelectedFile();
try {
final Scanner scanner = new Scanner(selected);
while (scanner.hasNextLine())
System.out.println(scanner.nextLine());
} catch (final FileNotFoundException e) {
System.err.println("File not found.");
void saveFile() {
final JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
final File selected = fileChooser.getSelectedFile();
try {
final PrintStream printer = new PrintStream(selected);
printer.println("this is the first line");
printer.println("this is the second line");
} catch (final FileNotFoundException e) {
System.err.println("File not found.");
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.