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

Thread: Accessing non-static methods from action listener in another class?

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

    Default Accessing non-static methods from action listener in another class?

    I've created three classes (two JPanels and a JFrame) per the specifications of a homework assignment, but unfortunately, all my notes from the last week were lost in a computer crash. I know there's a simple solution, but I've been spending hours trying to remember what to look up.

    Here's the frame (it's working fine):

    import java.awt.*;
    import java.awt.image.*;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class FrameClass extends JFrame
    {
     
    	public FrameClass (int width, int height)
    	{
    		super();
     
    		this.setTitle("Assignment 05");
    		this.setSize(width,height);
    		this.setLocationRelativeTo(null);
     
    		this.setLayout(new BorderLayout(10, 2));
     
    		this.add(new graphicspanel(), BorderLayout.CENTER);
    		this.add(new controlspanel(), BorderLayout.EAST);
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setVisible(true);
    	}
     
    	public static void main(String[] args) 
    	{
    		FrameClass fc = new FrameClass(750, 450);
    	}
    }

    Here's my first panel, which is also working fine (I can't make the timer or the start and stop methods static because I need to call a non-static method (repaint) from inside the timer later in the assignment):

    package Assignment5;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.ArrayList;
     
    import javax.swing.*;
     
    import ParticlePackage.particle;
    import MathVector.*;
     
    public class graphicspanel extends JPanel 
    {
    	private int width;
    	private int height;
     
    	public graphicspanel ()
    	{
    		super();
     
    		this.setBackground(Color.gray);  
     
    	}
     
    	Timer timer = new Timer
    			(100, new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					System.out.println("tick");
    				}
    			});
     
    	public void start() {timer.start();}
    	public void stop()  {timer.stop();}	
    }

    And finally, I desperately need a walkthrough of how to call the start and stop methods when the buttons in the following panel are pressed without making the methods static. The solution my professor gave us involved using the JFrame class somehow, but I've spent hours trying to recall what to look up with no luck.

    package Assignment5;
     
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;
     
    import javax.swing.*;
     
    public class controlspanel extends JPanel
    {
    	public controlspanel()
    	{
    		setLayout(new GridLayout(10,1,10,2));
     
    		JButton button1 = new JButton("Start");
    		button1.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent arg0)
    					{
    						System.out.println("Start");
    						start();
    					}
    				}
    				);
    		add(button1);
     
    		JButton button2 = new JButton("Stop");
    		button2.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent arg0)
    					{
    						System.out.println("Stop");
    						stop();
    					}
    				}
    				);		
    		add(button2);
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Accessing non-static methods from action listener in another class?

    If you pass references to instances of the classes you can use them to call methods in the classes.

    If you make the class an inner class it will have access to methods in the enclosing class.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Accessing non-static methods from action listener in another class?

    I can't make any of these inner classes, but I tried adding the following code to pass a reference:

    (and of course calling g.start() and g.stop() in my action listeners)

    public class controlspanel extends JPanel
    {
    	private graphicspanel g;
     
    	public controlspanel(graphicspanel g)
    	{
    		this.g = g;
    	}
     
    	public controlspanel()
    	{ ... 
            }
    }

    I am now getting the following error when running the frame in Eclipse, even though the code appears to compile correctly:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Assignment5.controlspanel$1.actionPerformed(contro lspanel.java:32)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Accessing non-static methods from action listener in another class?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Assignment5.controlspanel$1.actionPerformed(contro lspanel.java:32)
    There is a variable with a null value on line 32. Look at line 32 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 32 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Action Listener in a static method
    By Trunk Monkeey in forum AWT / Java Swing
    Replies: 4
    Last Post: October 18th, 2012, 01:41 PM
  2. Action Listener Error
    By dookie1293 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 22nd, 2011, 09:34 PM
  3. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  4. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM