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
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …
Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …
Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …