Excuse me if it is a silly mistake but i am newbie to java and couldn't figure it out. Basically it searches if txtText (text area) contains the txtSearchParam (text field) and returns the number of txtSearchParam in txtText. Algorithm is working. simply i can't access variables defined in main from actionPerformed method. how to get the text from textbox and use in the method ?
package javaproject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Hasan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame("search a word");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,320);
JPanel panel = new JPanel();
frame.add(panel);
JTextField txtSearchParam = new JTextField(13);
panel.add(txtSearchParam);
JButton btnSearch = new JButton("search");
panel.add(btnSearch);
btnSearch.addActionListener(new Search());
JButton button2 = new JButton(" open ");
panel.add(button2);
JTextArea txtText = new JTextArea(13,30);
panel.add(txtText);
JLabel label = new JLabel("number of times :");
panel.add(label);
JLabel lblResult = new JLabel();
panel.add(lblResult);
}
static class Search implements ActionListener{
public void actionPerformed (ActionEvent e){
String text = txtText.getText(); //can not find symbol txtText
String SearchParam = txtSearchParam.getText(); //can not find symbol txtSearchParam
String[] Words;
int index = 0;
int counter = 0;
Words = text.split(" ");
for(String s : Words)
{
Words[index++] = s.trim();
}
for (index = 0;index<Words.length;index++)
{
if(Words[index].contains(SearchParam))
counter += 1;
}
String Result = Integer.toString(counter);
lblResult.setText(Result); //can not find symbol lblResult
}
}
}