Cannot Finds Symbol Action Listener

i am doing a browse file class by using JFileChooser. i meet a problem when compile. it keep told me than cannot find symbol actionlistener. below is my code:

import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.filechooser.*;


public class BrowseForFile 
{
private JTextField txtFileName;
private JFrame layout;

public BrowseForFile()
{
    super();
    initialize();
}

    public void initialize() 
    {
        //empty layout
        layout = new JFrame();
        layout.setTitle("Task Synchronization ");
        layout.setBounds(100, 100, 800, 600);
        layout.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        layout.getContentPane().setLayout(null);

        //set the copyright
        JLabel lblNewLabel_5 = new JLabel("(c) 2012 All Rights Reserved");
        lblNewLabel_5.setForeground(Color.GRAY);
        lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 10));
        lblNewLabel_5.setHorizontalAlignment(SwingConstants.RIGHT);
        lblNewLabel_5.setBounds(527, 548, 255, 14);
        layout.getContentPane().add(lblNewLabel_5);

        //set the label
        JLabel lblSendAFile = new JLabel("Select a file to be sent to all nodes");
        lblSendAFile.setBounds(404, 400, 378, 14);
        layout.getContentPane().add(lblSendAFile);

        //set the textfield
        txtFileName = new JTextField();
        txtFileName.setBounds(404, 425, 277, 20);
        layout.getContentPane().add(txtFileName);
        txtFileName.setColumns(10);

        //set the browse button and let it to choose file after click.
        JButton btnBrowse = new JButton("Browse");
        btnBrowse.setBounds(691, 424, 91, 23);
        layout.getContentPane().add(btnBrowse);

        btnBrowse.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(new File(dirName));
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

                FileNameExtensionFilter filter = new FileNameExtensionFilter(".txt only", "txt");
                chooser.setFileFilter(filter);

                try {
                int code = chooser.showOpenDialog(null);
                if (code == JFileChooser.APPROVE_OPTION) {
                File selectedFile = chooser.getSelectedFile();
                Scanner input = new Scanner(selectedFile); 
                String f=selectedFile.getName();
                txtFileName.setText("File Name is: "+f);



                }

                } catch (Exception f) {
                f.printStackTrace();
                }
            }
        });
    }







    public static void main(String[] args)
    {
        try 
        {
            BrowseForFile window = new BrowseForFile();
            window.layout.setVisible(true);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }


}

this is the error :

BrowseForFile.java:52: error: cannot find symbol
                    btnBrowse.addActionListener(new ActionListener()
                                                    ^
symbol:   class ActionListener
location: class BrowseForFile
1 error

can anyone tell me what is the error ? thanks in advance.

3

4 Answers

import java.awt.event.ActionListener; // seems to be missing.
2

You have to import following classess.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
1

Since your question has already been answered, I would like to offer a suggestion.

Use an IDE like Eclipse or Netbeans. They take care of imports, formatting, checked exceptions, etc making your life much easier.

import java.awt.* means import all the classes from this package but not from the sub packages. For every sub package, you have to put separate import statement. e.g. import java.awt.event, import java.awt.datatransfer.* etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest