Results 1 to 13 of 13

Thread: JButton help

  1. #1
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default JButton help

    Why am I getting an error here?

    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;


    public class Event4 extends JFrame
    {
    private JButton myButton;

    public static void main (String[ ] args)
    {
    JFrame f = new Event4( );
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setSize(250,170);
    f.setVisible(true);
    }

    public Event4( )
    {
    super("Event Demo 4");
    getContentPane( ).setLayout(new FlowLayout( ));
    myButton = new JButton("Example");
    myButton.addActionListener(this);
    getContentPane( ).add(myButton);
    }

    public void actionPerformed(ActionEvent event)
    {
    System.out.println("Button clicked");
    }
    }

  2. #2
    SankaR7 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: JButton help

    Your problem is in the line:

    myButton.addActionListener(this);

    What you need to do is create a new class that implements Action Listener and then put your method that performs the action in that class.
    Then when you add the actionListener to the button put the name of of the class you created in the brackets instead of the word this.

    Here's your code revised to show you what i mean:

    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;


    public class Event4 extends JFrame
    {
    private JButton myButton;

    public static void main (String[ ] args)
    {
    JFrame f = new Event4( );
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setSize(250,170);
    f.setVisible(true);
    }

    public Event4( )
    {
    super("Event Demo 4");
    getContentPane( ).setLayout(new FlowLayout( ));
    myButton = new JButton("Example");
    myButton.addActionListener(new ButtonListener());
    getContentPane( ).add(myButton);
    }

    private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent event)
    {
    System.out.println("Button clicked");
    }
    }
    }

    Hope that helps

  3. #3
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    OH thanks. I thought that because you import ActionListener you didn't have to make a new class. Could I just add implements ActionListener to the Event4 class header?

  4. #4
    SankaR7 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: JButton help

    No i think you would be better of wanting to create the new class and put the header there.
    Adding implements ActionListener to the Event4 class would give you compiling errors.

  5. #5
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    OH thanks. I thought that because you import ActionListener you didn't have to make a new class. Could I just add implements ActionListener to the Event4 class header?

  6. #6
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    actually it doesn't, I tried it and it compiled fine.

  7. #7
    SankaR7 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: JButton help

    Glad it worked out.

  8. #8
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    Me too but I still don't understand why it has to be in there. Can you explain?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: JButton help

    Quote Originally Posted by kyle_maddisson View Post
    Me too but I still don't understand why it has to be in there. Can you explain?
    What is your specific question? Importing a class just makes the class available to use but doesn't actually use the class. You have to do that in your code.

  10. #10
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    well if I put implements ActionListener on the class header like this:

    public class Event4 extends JFrame implements ActionListener
    {
    ........
    .........
    }

    which is actually how the teacher has it in the example I see, it compiles fine and runs but what happens when I leave out implements ActionListener?
    It says to leave that out and try to guess what would happen. Obviously an error right, but why? We import ActionListener so why is it an error?
    Also is the ActionListener part the part that allows this program to respond to JButton events?
    Last edited by kyle_maddisson; 11-14-2011 at 05:17 AM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: JButton help

    Quote Originally Posted by kyle_maddisson View Post
    well if put implements ActionListener on the class header like this:

    public class Event4 extends JFrame implements ActionListener
    {
    ........
    .........
    }

    which is actually how the teacher has it in the example I see.
    An ActionListener is a type that handles button (and menu) actions, and if you do this, then yes, you can add this as your ActionListener, and so yes, this could work, but most experienced Swing programmers that I know frown on this as it asks the class to do too much: to be both a GUI and an ActionListener, and it often leads to the dreaded "switchboard" ActionListener, where one listener (the gui class) handles all the actions for a bunch of buttons leading to a horrendously long and hard to debug method.

    what happens when I leave out implements ActionListener?
    If you leave it out and don't use the full package name within your code, the compiler won't recognize what an ActionListener is.

    Also is the ActionListener part the part that allows this program to respond to JButton events?
    Yes.

  12. #12
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    I see. Thanks very much. :)

  13. #13
    Join Date
    Nov 2011
    Posts
    47
    Rep Power
    0

    Default Re: JButton help

    I'm so sorry to bother you again. I thought I had this figured out, now I don't know. I need help answering the following questions:

    1) Describe what would happen, and why, if implements ActionListener were removed.

    2) Identify and describe the parts of the code that allow this Java program to respond to the JButton action.



    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;


    public class Event4 extends JFrame implements ActionListener
    {
    private JButton myButton;

    public static void main (String[ ] args)
    {
    JFrame f = new Event4( );
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    f.setSize(250,170);
    f.setVisible(true);
    }


    public Event4( )
    {
    super("Event Demo 4");
    getContentPane( ).setLayout(new FlowLayout( ));
    myButton = new JButton("Example");
    myButton.addActionListener(this);
    getContentPane( ).add(myButton);
    }


    public void actionPerformed(ActionEvent event)
    {
    System.out.println("Button clicked");
    }
    }



    From what I understand now, ActionListener is an interface, so if I were to remove implements ActionListener, then I would what, get a compiler error when the compiler got to myButton.addActionListener(this) because without the implements Actionlistener, then this is now an Event4 object, or class. That's what I think the answer is for question number one.

    For question number 2, I am not sure what they are asking. Do they want me to name each thing, like where you add the button, because you can't really react to anything if you don't have a button to react to? Or is the answer simply ActionListener? Or does it include the actionPerformed part too. I don't like these questions that are so vague. This is for a quiz/ review, and i really need help understanding this stuff. I really appreciate your help. This forum is really amazing.

Similar Threads

  1. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  2. jbutton
    By Patea2000 in forum NetBeans
    Replies: 1
    Last Post: 03-16-2011, 08:56 AM
  3. Regarding JButton
    By sneha_v27 in forum AWT / Swing
    Replies: 4
    Last Post: 02-18-2011, 02:17 PM
  4. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 09:53 PM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 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
  •