Results 1 to 4 of 4
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Issues with ActionListener.

    I am trying to implement action listener on two buttons in JFrame, but the issue is one of the two button is performing both the functions; but i've not configured it to do so. Please find the sample code:-

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class MyChangingCirlce implements ActionListener{
       	JButton colorButton, labelButton;
    	JLabel myLabel;
    	MyDrawPanel mdp;
    	JFrame frame;
       public static void main(String [] args)
       {
    		MyChangingCirlce mcc = new MyChangingCirlce();
    		mcc.createFrame();
       } //end of main
    
       public void createFrame()
       {
    	frame = new JFrame();
    	JButton colorButton = new JButton("Changing Colors");
    	JButton labelButton = new JButton("Change Label");
    	myLabel = new JLabel("I'm a label");
    	mdp = new MyDrawPanel();
    
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    
    	frame.getContentPane().add(BorderLayout.CENTER, mdp);	//adding the panel
    	frame.getContentPane().add(BorderLayout.SOUTH,colorButton);	//color change button
    	frame.getContentPane().add(BorderLayout.EAST,labelButton);	// change label
    	frame.getContentPane().add(BorderLayout.WEST,myLabel);
    	colorButton.addActionListener(this);
    	labelButton.addActionListener(this);	//provide a way to ev-source to call back the int
    	frame.setSize(300,300);
    	frame.setVisible(true);
    	
       } // end of createFrame Method
    
    
       	public void actionPerformed(ActionEvent e)
    	{
    	if(e.getSource()== colorButton)
    	{
    	frame.repaint();		
    	}
    	else
    	{
    	myLabel.setText("That's it");
    	}
    
    	}	//end of interface method...
    
    
    }
    My labelButton is performing both the action only 1 time; i.e it changes the color of the circle along with the label text.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyDrawPanel extends JPanel{
    
    	public void paintComponent(Graphics g)
    	{
    		int red = (int) (Math.random() * 255);
    		int green = (int) (Math.random() * 255);
    		int blue= (int) (Math.random() * 255);
    		Color randomColor = new Color(red,green,blue);
    		g.setColor(randomColor);
    		g.fillOval(20,70,100,100);
    	}
    }
    Last edited by ankiit; 07-25-2012 at 09:27 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Issues with ActionListener.

    Add some println's in the ActionListener. Is the if statement ever evaluated to true? If no, why not? What are the values of the instance variables you are testing in that piece of code? Again, use some println's to debug (Hint: where do you instantiate the colorButton and labelButton variables?).

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Issues with ActionListener.

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Issues with ActionListener.

    1) You shouldn't be setting random colors (or implementing any kind of program logic) in a painting method override, since you have absolutely no control over how often and how many times that method will be called.

    Create a Color variable as a class field, update it in the actionPerformed(...) and call repaint().

    2) Repaint the component that needs repainting. Why should the frame repaint a contained component that isn't invalidated?

    3) Using java.util.Random is recemmended over Math.random().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Actionlistener q
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-16-2012, 09:17 PM
  2. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 04:59 PM
  3. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 11:04 PM
  4. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 07:05 PM
  5. Please Help With ActionListener
    By Daman12 in forum New To Java
    Replies: 29
    Last Post: 10-26-2011, 08:43 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
  •