Word Counter Java Swing

Word Counter Java Swing

Introduction:

In this project, we will demonstrate how to build Word Counter using Java Swing library. This Word Counter allows you to count the words easily using a graphical user interface (GUI) application. This system is develop with the Java Swing library which gives a set of components that can be use to build a Graphical user interface(GUI). This system has an easy and simple design with button to count the words in TextArea. So grab a seat, pour a cup of coffee and start using Word Counter.

Explanation:

This Word Counter is a Graphical user interface(GUI) system that allows user to easily count the words written in TextArea using button. Users may very quickly count the words using the system simple design. The Word Counter has simple design with button and TextArea. The user can write a text in TextArea click on the button to count the words.

The users may count the words written in Text Area using count buttons. The user can write their content or sentences in the Text Area and after clicking the count button the numbers of words in the Text Area will be displayed to the users. Further user can change the text and can again count the words.

The Java Swing library which gives a flexible set of components is used to build the Word Counter. The application makes use of Swing components such as JTextArea, JButton, JLabel and JPanel among many other Swing components. Button event is handled by the ActionListener interface and button events are defined using ActionListener’s actionPerformed() method.

Methods used in the project:

actionPerformed():

The action or events of the buttons are handled using this method. Buttons in the system to count the number of words in TextArea is handled using this method. This method describe how the component should behave after the button is click.

split():

This method is used to split the text based on white-space. This method will check for the white-space and it will split text into words and further counts the number of words in the Text Area.

Source Code:

				
					package blogs_program;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class word_counter extends JFrame implements ActionListener {

    private JTextArea textArea;
    private JButton countButton;
    private JLabel countLabel;

    public word_counter() {
        super("Word Counter");

        // Create text area and count button
        textArea = new JTextArea(10, 30);
        countButton = new JButton("Count");

        // Create count label with initial value of zero
        countLabel = new JLabel("Word count: 0");
        // Add components to frame
        JPanel panel = new JPanel();
        panel.add(new JScrollPane(textArea));
        panel.add(countButton);
        panel.add(countLabel);
        add(panel);

        // Register action listener for count button
        countButton.addActionListener(this);

        // Set frame properties
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        // Get text from text area
        String text = textArea.getText();

        // Split text into words
        String[] words = text.split("\\s+");

        // Count number of words
        int count = words.length;

        // Update count label
        countLabel.setText("Word count: " + count);
    }

    public static void main(String[] args) {
        new word_counter();
    }

}
				
			

Output:

Find More Projects

Hostel Management System Using Python With Source Code by using Python Graphical User Interface GUI Introduction: Managing a hostel efficiently can be …

Contra Game Using Python with source code using Pygame Introduction: Remember the super fun Contra game where you fight aliens and jump …

Restaurant Billing Management System Using Python with Tkinter (Graphical User Interface) Introduction: In the bustling world of restaurants, efficiency is paramount, especially …

Jungle Dash Game Using Python with source code with pygame module Introduction: Dive into the excitement of jungle adventures with our new …

Building a Tetris Game with Python with source code Introduction: Welcome to the world of Tetris, where falling blocks meet strategic maneuvers …

Super Mario Game Using Python with source code Introduction: Remember playing Super Mario as a kid? It’s that classic game where you …

All Coding Handwritten Notes

Browse Handwritten Notes