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

Thread: Action Listener in a static method

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Action Listener in a static method

    So I am trying to make a button that opens a new frame.

    The problem is that my main method is static so I can't put an action listener in the main part. I assume that I can make another class to make the action listener. So far I have this:

    public class VertexAdd extends JPanel implements ActionListener {
    	JButton button;
     
    	public VertexAdd() {
    		super(new BorderLayout());
    		[U]Main.AddVert.addActionListener(this);[/U]
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		createAndShowGUI();
    	}
     
    	private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JLabel emptyLabel = new JLabel("");
            emptyLabel.setPreferredSize(new Dimension(175, 100));
            frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    }

    It doesn't work right now and I am not sure how to make it so the action listener works. The button I want to attach it to is in my main class called 'AddVert'


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Action Listener in a static method

    What do you mean by "doesn't work right now"? Are you getting an exception? If so, please post any exception messages/stack traces.

    Also, if your VertexAdd class isn't used for anything other than being an ActionListener you don't need to extend the JPanel class.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Action Listener in a static method

    There isn't an exception. Its just that nothing is happening. The frame should pop up once I press the button but it does nothing.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Action Listener in a static method

    Quote Originally Posted by Trunk Monkeey View Post
    There isn't an exception. Its just that nothing is happening. The frame should pop up once I press the button but it does nothing.
    Thread moved to a more appropriate location.

    Please post an SSCCE. We can only guess whether you are adding listeners, where you are adding listeners, or what is a part of what.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Action Listener in a static method

    Your program design is a bit strange. It suggests that your Main class has static GUI components which you should almost never do. I suggest you re-think your design, that you get rid of everything static other than the main method, the createAndShowGUI method and perhaps some constants -- that's it.

Similar Threads

  1. Action.Listener exception.
    By Christopherx in forum Exceptions
    Replies: 2
    Last Post: September 1st, 2011, 12:36 PM
  2. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  3. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  4. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM

Tags for this Thread