Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: JTextArea addActionListener

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default JTextArea addActionListener

    I am on the controller side of my application and I am having a issue with adding a actionListener to my JTextArea.

    package addItemBtn.Home.DataBase;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
     
    public class AddItemController 
    {
    	private AddItemModel model;
    	private AddItemView view;
     
    	AddItemController()
    	{
     
    		//adding actionListeners for the AddItemClass
    		view.cancelBtn.addActionListener(new ActionListeners());
    		view.submitBtn.addActionListener(new ActionListeners());
    		view.previewBtn.addActionListener(new ActionListeners());
    		view.nameBox.addActionListener(new ActionListeners());
    		view.priceBox.addActionListener(new ActionListeners());
    		view.locationBox.addActionListener(new ActionListeners());
    		view.descriptionBox.addActionListener(new ActionListeners());
    	}
     
    	class ActionListeners implements ActionListener
     
    	{
     
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
     
    		}
     
     
    	}
     
    }

    view.descriptionBox.addActionListener(new ActionListeners());//this is where my error is

    Error states: The method addActionListener(AddItemController.ActionListener s) is undefined for the type JTextArea)

    I clicked on each of the suggestions it gives me but it is still not working. Another things is I found examples on the web with people using actionListener with JTextArea..... Any advice?


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: JTextArea addActionListener

    Its simple as that. A JTextArea does not support ActionListeners.
    What kind of "Action" should a JTextArea produce in your opinion?
    Perhaps you should try with a different listener.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: JTextArea addActionListener

    yes I understood the error and and read the API doc and still get the same issue:

    package addItemBtn.Home.DataBase;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
     
     
    public class AddItemController 
    {
    	private AddItemModel model;
    	private AddItemView view;
     
    	AddItemController(AddItemModel model,AddItemView view)
    	{
    		this.model = model;
    		this.view = view;
     
    		//adding actionListeners for the AddItemClass
    		view.cancelBtn.addActionListener(new ActionListeners());
    		view.submitBtn.addActionListener(new ActionListeners());
    		view.previewBtn.addActionListener(new ActionListeners());
    		view.nameBox.addActionListener(new ActionListeners());
    		view.priceBox.addActionListener(new ActionListeners());
    		view.locationBox.addActionListener(new ActionListeners());
    		view.descriptionBox.addDocumentListener(new ActionListeners()); //error is here
     
    		view.frame.setVisible(true);
    	}
     
    	class ActionListeners implements ActionListener, DocumentListener
     
    	{
     
    		@Override
    		public void actionPerformed(ActionEvent e) 
    		{
    			try
    			{
    				if(e.getSource() == view.submitBtn)
    				{
    					System.out.println("submit Pressed");
    				}
    				else if(e.getSource() == view.cancelBtn)
    				{
    					view.frame.dispose();
    					System.out.println("Cancel Pressed");
    				}
    				else if(e.getSource() == view.previewBtn)
    				{
     
    				}
    				else if(e.getSource() == view.descriptionBox)
    				{
     
    				}
    				else if(e.getSource() == view.nameBox)
    				{
     
    				}
    				else if(e.getSource() == view.priceBox)
    				{
     
    				}
    				else if(e.getSource() == view.locationBox)
    				{
     
    				}
    				else
    				{
    					System.out.println("else");
    				}
     
    			}catch(Exception ex)
    			{
    				System.out.println("There was an error "+e);
    			}
     
    		}
     
    		@Override
    		public void insertUpdate(DocumentEvent e) {
    			// TODO Auto-generated method stub
     
    		}
     
    		@Override
    		public void removeUpdate(DocumentEvent e) {
    			// TODO Auto-generated method stub
     
    		}
     
    		@Override
    		public void changedUpdate(DocumentEvent e) {
    			// TODO Auto-generated method stub
     
    		}
     
     
    	}
     
    }

    Also my actionListeners are not working. I put println statements in there to double check and nothing it happening.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JTextArea addActionListener

    I put println statements
    They're inside if statements which means they may not be executed. Add a println() first thing in the method, outside of the if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: JTextArea addActionListener

    it is simply a JTextArea from AddItemView()

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JTextArea addActionListener

    Review the JTextArea API (near the top) for the technique required to add a document listener. Your existing approach is incorrect.

  7. #7
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: JTextArea addActionListener

    Thank you. When in doubt read the API.

    TextListener!! Says it plan as day...

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JTextArea addActionListener

    You're welcome, and that's a good rule to follow.

Similar Threads

  1. addActionListener to JButton
    By ICEPower in forum AWT / Java Swing
    Replies: 11
    Last Post: April 5th, 2013, 01:02 PM
  2. Clarification needed for addActionListener and actionPerformed
    By mabelanger in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2013, 09:03 PM
  3. Can't use button.addActionListener(this); ?
    By xdega in forum AWT / Java Swing
    Replies: 2
    Last Post: April 23rd, 2012, 08:44 AM
  4. Not sure how to deal with addActionListener! :3
    By Asteroid555 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2011, 01:57 PM
  5. non-static variable, addActionListener()
    By bobbyrne01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 10:58 AM