How to create a Word Count program with java swing and AWT

In this program, we will create a simple word count app by using swing in java. In this app, we will count the word in the text area & also change the color of the text. Although, this program specially created for word count. We will use swing, Applet, Framework.

Java Program to create word count program in Swing

import java.awt.*;  
import javax.swing.*;  
import java.awt.event.*;  
public class CharCount extends JFrame implements ActionListener{  
    JLabel lb1,lb2;  
    JTextArea ta;  
    JButton b;  
    JButton pad,text;  
    CharCount(){  
        super("Char Word Count Tool ");  
        lb1=new JLabel("Characters: ");  
        lb1.setBounds(50,50,100,20);  
        lb2=new JLabel("Words: ");  
        lb2.setBounds(50,80,100,20);  
          
        ta=new JTextArea();  
        ta.setBounds(50,110,300,200);  
          
        b=new JButton("click");  
        b.setBounds(50,320, 80,30);//x,y,w,h  
        b.addActionListener(this);  
      
        pad=new JButton("Pad Color");  
        pad.setBounds(140,320, 110,30);//x,y,w,h  
        pad.addActionListener(this);  
  
        text=new JButton("Text Color");  
        text.setBounds(260,320, 110,30);//x,y,w,h  
        text.addActionListener(this);  
  
        add(lb1);add(lb2);add(ta);add(b);add(pad);add(text);  
          
        setSize(400,400);  
        setLayout(null);//using no layout manager  
        setVisible(true);  
        setDefaultCloseOperation(EXIT_ON_CLOSE);//be like file chooser  
    }  
    public void actionPerformed(ActionEvent e){  
        if(e.getSource()==b){  
        String text=ta.getText();  
        lb1.setText("Characters: "+text.length());  
        String words[]=text.split("\\s");  
        lb2.setText("Words: "+words.length);  
        }else if(e.getSource()==pad){  
            Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);  
            ta.setBackground(c);  
        }else if(e.getSource()==text){  
            Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);  
            ta.setForeground(c);  
        }  
    }  
public static void main(String[] args) {  
    new CharCount();  
}}

In this program, we will use the text bar in which we write the text & Also we use JColorChooser. JColorChooser is the method that provides the color bar in which all colors are present. We use Action Listener as well as if anybody doesn’t know about the action listener please go through my posts.

The main logic of the program



 if(e.getSource()==b){ 
String text=ta.getText(); 
lb1.setText("Characters: "+text.length()); 
String words[]=text.split("\\s"); 
lb2.setText("Words: "+words.length); 
}else if(e.getSource()==pad){ 
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK); 
ta.setBackground(c); 
}else if(e.getSource()==text){ 
Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK); 
ta.setForeground(c); 
} 
}

The output of the program

word count app in java swing

 

word count in Java

Read other tutorials,

Leave a Reply

Your email address will not be published. Required fields are marked *