Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By milovan

Thread: Need help with action listener please!!!

  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Need help with action listener please!!!

    My goal is to create a program that will incrementally increase or decrease the counter. My issue is that the counter will start at 0 whether i have decreased or increased the counter already. The other issue is the counter where continue where i left off even if i have reset it to 0. Here is what i have along with a screen shot of the GUI and some results:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MyMenu extends JFrame
    {
    	//attributes
    	int num;
    	JTextArea area;
    	JMenuItem inc, dec, reset, exit;
    	
    	public MyMenu()
    	{
    		JFrame myMenu = new JFrame("My Menu");//creates frame
    		myMenu.setLocation(200, 200);//sets location of frame
    		myMenu.setSize(200, 400);//sets size of frame
    	
    		JMenuBar mbar = new JMenuBar();//create menu bar
    		myMenu.setJMenuBar(mbar);//add menu bar to program
    		
    		JMenu count = new JMenu("Count");//create the menu bar item count
    		mbar.add(count);//add count menu item to menu bar
    		
    		inc = new JMenuItem("Inc");//create inc menu item to add to menu bar item
    		count.add(inc);//add inc item to count item
    		
    		dec = new JMenuItem("Dec");//create dec menu item to add to menu bar item
    		count.add(dec);//add dec item to count item
    		
    		reset = new JMenuItem("Reset");//create reset menu item to add to menu bar item
    		count.add(reset);//add reset item to count item
    		
    		exit = new JMenuItem("Exit");//create exit menu item to add menu bar item
    		count.add(exit);//add exit item to count item
    		
    		JTextArea area = new JTextArea(20, 15);//create a text area that is 20 rows and 15 columns
    		area.setEditable(false);//make text area uneditable
    		num = 0;
    		area.setText(""+num);//assign a value to the text field
    		myMenu.add(area);//add text area to Frame
    		
    		MyListenerCnt inc1 = new MyListenerCnt(inc, area, num);
    		MyListenerCnt dec1 = new MyListenerCnt(dec, area, num);
    		MyListenerCnt reset1 = new MyListenerCnt(reset, area, num);
    		MyListenerCnt exit1 = new MyListenerCnt(exit, area, num);
    		
    		myMenu.setVisible(true);//make the frame visible
    		myMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	
    	public static void main(String[] args)
    	{
    		MyMenu testMenu = new MyMenu();
    	}
    	
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MyListenerCnt implements ActionListener
    {
    	//Attributes
    	int num;
    	JMenuItem menuItem;
    	JTextArea area;
    	
    	public MyListenerCnt(JMenuItem _menuItem, JTextArea _area, int _num)
    	{
    		menuItem = _menuItem;
    		menuItem.addActionListener(this);
    		area = _area;
    		num = _num;
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getActionCommand().equals("Inc"))
    			{
    			num++;
    			area.append("\n"+num);
    			}
    		else if (e.getActionCommand().equals("Dec"))
    			{
    				num--;
    				area.append("\n"+num);
    			}
    		else if (e.getActionCommand().equals("Reset"))
    			{
    				num = 0;
    				area.setText(""+num);
    			}
    		else if (e.getActionCommand().equals("Exit"))
    			{
    				System.exit(0);
    			}
    
    	}
    }
    Screenshot:
    Need help with action listener please!!!-exampleresults.jpg

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default Re: Need help with action listener please!!!

    Well your program is doing just what you program it to do. For each of menu items you have instantiate a new MyListenerCnt object, each object has its own num variable starting with 0 and each actionPerformed method in every object append its own calculated num number to one JTextArea. You should think of your logic again...

    And you should go through actions and action listeners once again
    Last edited by milovan; 01-16-2013 at 11:10 PM.
    ndsmith20 likes this.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with action listener please!!!

    What you are saying definitely makes sense but I honestly am not sure what to do to fix it, i apologize for sounding stupid.

  4. #4
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need help with action listener please!!!

    I got it!!! I added all the buttons to the one paramaterized constructor in the MyListnerCt program, then added a listener item to each one. Now it is working as designed.

Similar Threads

  1. action listener
    By skuskusas in forum New To Java
    Replies: 4
    Last Post: 09-04-2012, 07:13 PM
  2. Can anyone help me with an action listener?
    By mdCollins1 in forum New To Java
    Replies: 5
    Last Post: 03-21-2012, 04:07 AM
  3. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  4. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 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
  •