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 7 of 7

Thread: ActionListener problem...please help

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation ActionListener problem...please help

    screenshot.33.jpg

    The auto correct keeps telling me to add an @Override, but the button does not show in the test when i do and when i don't. Why is the listener getting an error and not showing in the above frame. Appreciate help, thanks!


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: ActionListener problem...please help

    Please post the code you are having problems with here on the forum, not images of code.
    Why is the listener getting an error
    Also copy the full text of the compiler error messages and paste it here.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener problem...please help

    Okay Thanks!

    package swing;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class Swing {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    JFrame frame = new JFrame("Test");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(300,300);
    JPanel panel = new JPanel();
    JButton button = new JButton("click here");
    panel.add(button);
    button.addActionListener (new Action ());
    //adds the whole actionlistener
    } //fixed llegal start of expression problem
    //actionlistener part
    static class Action implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    JFrame frame2 = new JFrame("Clicked");
    frame2.setVisible(true);
    frame2.setSize(200,200);
    JLabel label = new JLabel( "You Clicked me!");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);




    }
    }
    }

    The compiler doesn't give an error. It just doesnt show or do the things mentioned in the ActionListener. The warning by the actionListener says:
    Add @Override Annotation

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: ActionListener problem...please help

    You added your JButton to the JPanel but not the JPanel to the JFrame. Along with posting the error message, please also post the code in text so i can copy paste it into my IDE.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: ActionListener problem...please help

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ActionListener problem...please help

    Thanks. Here's the code!

    [code=java]
    package swing;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class Swing {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    JFrame frame = new JFrame("Test");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(300,300);
    JPanel panel = new JPanel();
    JButton button = new JButton("click here");
    panel.add(button);
    button.addActionListener (new Action ());
    //adds the whole actionlistener
    } //fixed llegal start of expression problem
    //actionlistener part
    static class Action implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    JFrame frame2 = new JFrame("Clicked");
    frame2.setVisible(true);
    frame2.setSize(200,200);
    JLabel label = new JLabel( "You Clicked me!");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);




    }
    }
    }
    [/code]

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener problem...please help

    Sorry, didn't post in other one. There really isn't a error message. What is does is by the line

    public void actionPerformed (ActionEvent e) {

    What happens is when i do add @Override or not, it doesn't work either way. The frame2 doesn't pop out from the frame1.

Similar Threads

  1. Problem Regarding ActionListener
    By sahilradotra in forum Java Theory & Questions
    Replies: 6
    Last Post: April 3rd, 2012, 11:42 AM
  2. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 04:43 PM
  3. Problem with actionlistener/drop-down-menu (for a queue)
    By blueroselara in forum AWT / Java Swing
    Replies: 17
    Last Post: January 25th, 2012, 11:59 PM
  4. ActionListener help
    By hello_world in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 28th, 2011, 12:45 AM
  5. Simple Actionlistener problem
    By olemagro in forum AWT / Java Swing
    Replies: 5
    Last Post: October 11th, 2010, 11:46 AM

Tags for this Thread