Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default question regarding objects

    Hey guys, I am confused about something. So I am practicing actionListeners in Swing, and noticed that they need to create a method to follow (as to handle what to do when the event happens). Knowing, this, I would like to know what it means for it to take "ActionEvent e" as a parameter (I will copy and paste code below), but the code for that part is
    Java Code:
    public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				
    			}
    I did not ever see ActionEvent e be created. Also, is it creating the method to deal with the event? Does the method for this actionListener ever need to be called? That is my first question. The second question is about the line below in the code that goes
    Java Code:
        	btn.addActionListener(new ActionListener() {
    What I don't understand here is why you need to instantiate an ActionListener object to add an actionListener to the JButton, "btn"


    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import java.awt.BorderLayout;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class MainFrame extends JFrame{
    
    	private JTextArea textArea;
    
    	private JButton btn;
    
    
    	public MainFrame(){
    		super("Hello World");
    		
        	setLayout(new BorderLayout());
        	textArea = new JTextArea();
        	btn = new JButton("Click me");
        	btn.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				
    			}
        		
        	});
        	add(textArea,BorderLayout.CENTER);
        	add(btn,BorderLayout.SOUTH);
        	setVisible(true);
        	setSize(600,500);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    }
    I hope you guys can answer, sorry about the confusing style in which I wrote the questions.

    Happy 2014!

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: question regarding objects

    read this: ActionEvent (Java Platform SE 7 )

    the ActionListener gets the event when it actually occurs, it is passed to it.

    the addActionListener(..) method accepts an ActionListener, so why doesn't it make sense to you to pass an ActionListener?

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: question regarding objects

    The Swing toolkit creates the event and calls your actionPerformed(...) method. It does all the work of figuring out what object should receive it. This an example of event-driven programming. Google that term to learn more.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 12
    Last Post: 04-29-2012, 03:55 PM
  2. Question for a list of objects
    By johnhelen in forum New To Java
    Replies: 4
    Last Post: 02-17-2011, 10:23 PM
  3. A question about objects
    By TaxpayersMoney in forum New To Java
    Replies: 20
    Last Post: 05-13-2010, 12:18 PM
  4. Simple question about objects.
    By shroomiin in forum New To Java
    Replies: 10
    Last Post: 10-10-2009, 03:33 AM
  5. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 05:10 PM

Tags for this Thread

Posting Permissions

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