Purchase Solution

Java GUI and code help

Not what you're looking for?

Ask Custom Question

Develop an application that counts the number of words, number of characters with spaces and without spaced. The program should display the text file statistics as follows:

File Name: sample.txt
Number of Words: 124
Number of characters without spaces: 1045
Number of characters with spaces: 1200

The application shall be a counsel compatible application-(needs a GUI)
here's what I have so far...
i'm missing both character counters and am not sure how to get files in my gui

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

//////////////////////////////////////////////////////// CountWords
public class CountWords extends JFrame {

//====================================================== fields
JTextField _fileNameTF = new JTextField(15);
JTextField _wordCountTF = new JTextField(4);
JFileChooser _fileChooser = new JFileChooser();

//================================================= constructor
CountWords() {
//... Create / set component characteristics.

//... Add listeners

//... Create content pane, layout components
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(new JLabel("File:"));
content.add(new JLabel("Word Count:"));

//... Create menu elements (menubar, menu, menu item)
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
openItem.addActionListener(new OpenAction());

//... Assemble the menu

//... Set window characteristics
this.setTitle("Count Words");
this.pack(); // Layout components.
this.setLocationRelativeTo(null); // Center window.

//============================================= countWordsInFile
private int countWordsInFile(File f) {

int numberOfWords = 0; // Count of words.

try {
Scanner in = new Scanner(f);

while (in.hasNext()) {
String word = in.next(); // Read a "token".
in.close(); // Close Scanner's file.

} catch (FileNotFoundException fnfex) {
// ... We just got the file from the JFileChooser,
// so it's hard to believe there's problem, but...
return numberOfWords;

///////////////////////////////////////////////////// OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent ae) {
//... Open a file dialog.
int retval = _fileChooser.showOpenDialog(CountWords.this);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
File file = _fileChooser.getSelectedFile();

//... Update user interface.
_wordCountTF.setText("" + countWordsInFile(file));

//========================================================= main
public static void main(String[] args) {
JFrame window = new CountWords();

Purchase this Solution

Solution Summary

The solution discusses Java GUI and code help.

Solution Preview

The coding is finished for the input file, I think you should use a plain text file. If you use a file that ...

Purchase this Solution

Free BrainMass Quizzes
C# variables and classes

This quiz contains questions about C# classes and variables.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.