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
        }
    }
}

You defined the variables inside the main method. In the other method you are trying to access them. I would recommend moving the variable definition outside the main method.

Also I would create a method to build the GUI and not do it inside the main method.

Even if you move out your txtText, txtSearchParam and lblResult the code wont compile because the getText() is not a static one so you would not be able to call it in the Search class that is static classe.

ok i created a method for gui they are outside of main now but still can't access variables ?

public class Main {
    
    public static void createAndShowGUI(){

        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);

    }
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                   createAndShowGUI();
            }
        });
        
    }
    
    static class Search implements ActionListener{

        public void actionPerformed (ActionEvent e){
            
            String SearchParam = txtSearchParam.getText();    //can not find  symbol txtSearchParam
            String text = txtText.getText();     //can not find symbol txtText
            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
           }
       }
}

Well lets resolve the first probleme; move out the declaration of the tree variable means some thing like this.

public class Main {
 JTextArea txtText;
    JTextField txtSearchParam;
    JLabel lblResult;
.......
}

This ovoid the "can not find symbol" error
But bring in to another error that I'v mentioned befor.

problem solved :) i defined them outside of the method and wrote return functions and its working :)

/**
 *
 * @author Hasan
 */
public class Main {

    static JTextField txtSearchParam = new JTextField(13);
    static JTextArea txtText = new JTextArea(13,30);
    static JLabel lblResult = new JLabel();

    JTextField asd(){
        return txtSearchParam;
    }

    JTextArea dsa(){
        return txtText;
    }

    JLabel fds(){
        return lblResult;
    }

    public static void createAndShowGUI (){

        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);

        panel.add(txtSearchParam);

        JButton btnSearch = new JButton("search");
        panel.add(btnSearch);
        btnSearch.addActionListener(new Search());

        JButton button2 = new JButton("  open   ");
        button2.addActionListener(new Open());
        panel.add(button2);

        panel.add(txtText);

        JLabel label = new JLabel("number of times :");
        panel.add(label);

        panel.add(lblResult);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                   createAndShowGUI();
            }
        });       
    }
    
    static class Search implements ActionListener{

        public void actionPerformed (ActionEvent e){

            String SearchParam = txtSearchParam.getText();
            String text = txtText.getText();
            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);
           }
       }
}
This question has already been answered. Start a new discussion instead.