Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Need help with an action listener please

  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Rep Power

    Default Need help with an action listener please

    Hi, I need to create a program that will find the first instance of the String you are searching for and highlight it and set focus to the end of the word. I am getting an error when adding one of my listeners. Any help would be greatly appreciated

    Java Code:
    // Author: me
    // Date:3/14/13
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class FindFrameStudents extends JFrame{
    	private JTextField fnd;
    	private JTextArea ta;
    	private JButton find;
    	private JButton clear;
    	public FindFrameStudents(){
    		// set up the frame to hold all the components
    		// Set up the top text area
    		ta = new JTextArea();
    		add(new JScrollPane(ta));
    		// set up a panel top hold the rest of the components
    		JPanel bottom = new JPanel();
    		bottom.setLayout(new GridLayout(2,1));
    		// create two subpanels to go into the grid
    		JPanel subTop = new JPanel();
    		JPanel subBottom = new JPanel();
    		// top panel get a label and a text field
    		JLabel lbl = new JLabel("Find:");
    		fnd = new JTextField(10);
    		// the bottom panel gets two buttons
    		find = new JButton("Find");
    		clear = new JButton("Clear");
    		// add subpanels to main panel
    		// add main panel to frame
    		// add an anonymous inner classes here
    		addWindowListener (
    			new WindowAdapter(){
    				public void windowClosing(WindowEvent e){
    		clear.addActionListener (
    			new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    		// instantiate a FindListener object
    		FindListener fl = new FindListener();
    		find.addActionListener (fl);
    		// make the frame appear
    	// define an instance inner class as a listener for the Find button
    	public class FindListener {
    			public void actionPerformed(ActionEvent e){
    				int start = ta.getText().indexOf(fnd.getText());
    				if (fnd != null) {
, start + fnd.getText().length());
    				else {
    					JOptionPane.showMessageDialog(null, "Please enter something into find field");
    	public static void main(String [] args) {
    		new FindFrameStudents();
    ----jGRASP exec: javac -g error: method addActionListener in class AbstractButton cannot be applied to given types;
    find.addActionListener (fl);
    required: ActionListener
    found: FindFrameStudents.FindListener
    reason: actual argument FindFrameStudents.FindListener cannot be converted to ActionListener by method invocation conversion
    1 error

    ----jGRASP wedge2: exit code for process is 1.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Need help with an action listener please

    required: ActionListener
    found: FindFrameStudents.FindListener
    What this means is that when you say "find.addActionListener (fl);" the compiler expects that fl will implement the ActionListener interface. But FindListener is not declared to implement that interface. I imagine that you forgot to add "implements ActionListener" to the FindListener.
    ndsmith20 likes this.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Rep Power

    Default Re: Need help with an action listener please

    Thank you :), im going to try that now

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Rep Power

    Default Re: Need help with an action listener please

    It did work...thank you very much

Similar Threads

  1. Need help with action listener please!!!
    By ndsmith20 in forum New To Java
    Replies: 3
    Last Post: 01-17-2013, 02:44 AM
  2. action listener
    By skuskusas in forum New To Java
    Replies: 4
    Last Post: 09-04-2012, 07:13 PM
  3. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts