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
    Posts
    55
    Rep Power
    0

    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
    		super("Find");
    		setSize(300,300);
    		setLocation(100,100);
    
    		// 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);
    		subTop.add(lbl);
    		subTop.add(fnd);
    
    		// the bottom panel gets two buttons
    		find = new JButton("Find");
    		clear = new JButton("Clear");
    		subBottom.add(find);
    		subBottom.add(clear);
    
    		// add subpanels to main panel
    		bottom.add(subTop);
    		bottom.add(subBottom);
    
    		// add main panel to frame
    		add(bottom,BorderLayout.SOUTH);
    
    		// add an anonymous inner classes here
    
    		addWindowListener (
    			new WindowAdapter(){
    				public void windowClosing(WindowEvent e){
    					System.exit(0);
    				}
    			}
    		);
    
    		clear.addActionListener (
    			new ActionListener(){
    				public void actionPerformed(ActionEvent e){
    					fnd.setText(null);
    				}
    			}
    		);
    
    		// instantiate a FindListener object
    		FindListener fl = new FindListener();
    		find.addActionListener (fl);
    		// make the frame appear
    		setVisible(true);
    	}
    
    	// 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) {
     					ta.select(start, start + fnd.getText().length());
      					ta.requestFocus();
    				}
    				else {
    					JOptionPane.showMessageDialog(null, "Please enter something into find field");
    				}
    			}
    
    	}
    
    	public static void main(String [] args) {
    		new FindFrameStudents();
    	}
    }
    error:
    ----jGRASP exec: javac -g FindFrameStudents.java

    FindFrameStudents.java:77: 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.
    ----jG

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    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
    Posts
    55
    Rep Power
    0

    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
    Posts
    55
    Rep Power
    0

    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
  •