Word Counter Java Swing With Source Code
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
Complain Management using Python with a Graphical User Interface (GUI) Introduction: The Complain Management using Python program designed to manage complaints effectively …
COVID 19 Hospital Management Using Python [Django Framework] Introduction: The COVID-19 Hospital Management is a Python-based application that tracks web applications for Hospitals. …
Drawing Ganesha Using Python Turtle Graphics[Drawing Ganapati Using Python] Introduction In this blog post, we will learn how to draw Lord Ganesha …
Contact Management System in Python with a Graphical User Interface (GUI) Introduction: The Contact Management System is a Python-based application designed to …
KBC Game using Python with Source Code Introduction : Welcome to this blog post on building a “Kaun Banega Crorepati” (KBC) game …
Basic Logging System in C++ With Source Code Introduction : It is one of the most important practices in software development. Logging …