Hi, I'm doing an interface using API and the interface includes a JTextArea to write the text, a word count label at the center, and 2 buttons at the bottom which enable me to save the text and to load it. In this case I'll be using action listener to create the function of saving the Text I write inside the JTextArea and when I press load it will load intothe TextField. However, I can't seems to find any resource on doing a letter count with API. The letter count is suppose to work the same as Microsoft word count such that as you type the letters, they will do the count on the go for you. Here is my codes, help me how can I make modification to add this letter count features.
"
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.*;
public class Reader {
static JTextArea input = new JTextArea("");
JLabel wordCount = new JLabel("Word Count : ");
JPanel panel1 = new JPanel();
JButton save = new JButton("Save");
JButton load = new JButton("Load");
/*Scanner sc = new Scanner(System.in);
int charCount = 0;
char userChar = sc.nextLine().charAt(0);
String userString = sc.nextLine();
{
for(int i= 0;i<userString.length();i++) {
if (userString.charAt(i) == userChar)
{
charCount++;
}
}
}*/
public Reader() {
// frame
JFrame frame = new JFrame();
frame.setBounds(300, 50, 300, 300);
frame.setLayout(new GridLayout(3, 1));
// Adding buttons into panel
panel1.add(save);
panel1.add(load);
// Adding TextField & word count into frame
frame.add(input);
frame.add(wordCount);
frame.add(panel1);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("C:/Users/82115/Downloads/Junliang.txt");
FileWriter writer = new FileWriter(file);
String text = input.getText().toString();
writer.write(text);
input.setText("Text has been saved!");
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("C:/Users/82115/Downloads/Junliang.txt");
FileReader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String text = bufferedReader.readLine();
while (text != null) {
input.setText(text);
text = bufferedReader.readLine();
}
// String text = input.getText().toString();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Reader();
}
}
"
Thanks in advanced!
"
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.*;
public class Reader {
static JTextArea input = new JTextArea("");
JLabel wordCount = new JLabel("Word Count : ");
JPanel panel1 = new JPanel();
JButton save = new JButton("Save");
JButton load = new JButton("Load");
/*Scanner sc = new Scanner(System.in);
int charCount = 0;
char userChar = sc.nextLine().charAt(0);
String userString = sc.nextLine();
{
for(int i= 0;i<userString.length();i++) {
if (userString.charAt(i) == userChar)
{
charCount++;
}
}
}*/
public Reader() {
// frame
JFrame frame = new JFrame();
frame.setBounds(300, 50, 300, 300);
frame.setLayout(new GridLayout(3, 1));
// Adding buttons into panel
panel1.add(save);
panel1.add(load);
// Adding TextField & word count into frame
frame.add(input);
frame.add(wordCount);
frame.add(panel1);
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("C:/Users/82115/Downloads/Junliang.txt");
FileWriter writer = new FileWriter(file);
String text = input.getText().toString();
writer.write(text);
input.setText("Text has been saved!");
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
File file = new File("C:/Users/82115/Downloads/Junliang.txt");
FileReader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
String text = bufferedReader.readLine();
while (text != null) {
input.setText(text);
text = bufferedReader.readLine();
}
// String text = input.getText().toString();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Reader();
}
}
"
Thanks in advanced!