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:
Get Discount on Top Educational Courses
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
ludo game in python using gui and thinkter introduction The “ludo game in python“ project is a simplified digital version of the …
hotel management system in python introduction The Hotel Management System is a simple, interactive GUI-based desktop application developed using Python’s Tkinter library. …
cryptography app in python using gui introduction In the digital age, data security and privacy are more important than ever. From messaging …
portfolio generator tool in python using GUI introduction In today’s digital-first world, having a personal portfolio is essential for students, job seekers, …
interactive story game in python using gui introduction The Interactive story game in python using gUI is a visually rich, dynamic, and engaging …
maze generator using Python with Pygame Module GUI introduction In this maze generator using Python, realm of logic, problem-solving, and computer science, …