Results 1 to 2 of 2
  1. #1
    dvanwesh is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Getting action twice when I select and deselect a radio button

    Here is the part of my code
    Java Code:
    JRadioButton Dollar;
    Dollar.addItemListener(new ItemListener(){
    			 @SuppressWarnings("unused")
    			public void Dollaraction(ItemEvent i4){}
    
    			@Override
    			public void itemStateChanged(ItemEvent e4) {
    				// TODO Auto-generated method stub
    				  String str3 =(String)plist.getSelectedItem();
    				   for(int l=0;l<st2.size();l++)
    					{  if(str3.equals(st2.get(l).pname))
    						  {  
    if((st2.get(l).f1-st2.get(l).price)>=0)
     {
     st2.get(l).quant--;
     t1.setText("You got your "+st2.get(l).pname+ " ,your change is $"+Round((st2.get(l).f1-st2.get(l).price),2));
     break;
     }
    				  c2.cname="dollar";
    		 		   c2.cvalue=c2.cvalue("dollar",c2.cvalue);
    		 		  st2.get(l).f1=Round(st2.get(l).f1+c2.cvalue,2);
    t1.setText("total amount entered is $"+st2.get(l).f1+" ,Balance amount remaining is $"+Round((st2.get(l).price-st2.get(l).f1),2));
    		 		   }}
    			}}
    		 );
    when i select Dollar Radiobutton this Item listener is executed and even when I deselect the Dollar Radiobutton and select other radio button, Dollar.Itemlistener is getting executing...
    what needs to be done to stop executing this item listener for second time
    Last edited by Fubarable; 03-17-2012 at 10:24 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Getting action twice when I select and deselect a radio button

    Your code is very hard to understand, what with your very "creative" indentation making it extremely hard to read. Contrast with this:

    Java Code:
          JRadioButton Dollar;
          Dollar.addItemListener(new ItemListener() {
    
             @SuppressWarnings("unused")
             public void Dollaraction(ItemEvent i4) {
             }
    
             @Override
             public void itemStateChanged(ItemEvent e4) {
                // TODO Auto-generated method stub
                String str3 = (String) plist.getSelectedItem();
                for (int l = 0; l < st2.size(); l++) {
                   if (str3.equals(st2.get(l).pname)) {
                      if ((st2.get(l).f1 - st2.get(l).price) >= 0) {
                         st2.get(l).quant--;
                         t1.setText("You got your " + st2.get(l).pname
                               + " ,your change is $"
                               + Round((st2.get(l).f1 - st2.get(l).price), 2));
                         break;
                      }
                      c2.cname = "dollar";
                      c2.cvalue = c2.cvalue("dollar", c2.cvalue);
                      st2.get(l).f1 = Round(st2.get(l).f1 + c2.cvalue, 2);
                      t1.setText("total amount entered is $" + st2.get(l).f1
                            + " ,Balance amount remaining is $"
                            + Round((st2.get(l).price - st2.get(l).f1), 2));
                   }
                }
             }
          });
    Next, your identifier names are very non-intuitive making it very hard to understand the purpose of the variables and methods.

    Consider creating a much simpler but complete example that demonstrates your problem. By complete I mean that the code compiles and runs for us, but simpler in that it has only code necessary for compilation, running, and to demonstrate the problem, that's it.

    For example, this small program shows that typically a JRadioButton's item listener only gets called once when the radiobutton is clicked:

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Foo {
       public static void main(String[] args) {
          JRadioButton radioButton = new JRadioButton("radio button");
          radioButton.addItemListener(new ItemListener() {
             
             @Override
             public void itemStateChanged(ItemEvent e) {
                System.out.println("state change. New state is selected: " + 
                      (e.getStateChange() == ItemEvent.SELECTED));
             }
          });
          JOptionPane.showMessageDialog(null, radioButton);
       }
    }
    Last edited by Fubarable; 03-17-2012 at 10:43 PM.

Similar Threads

  1. Which event for as they select the radio button?
    By N00Bie in forum New To Java
    Replies: 9
    Last Post: 02-18-2011, 02:44 AM
  2. Replies: 2
    Last Post: 02-03-2011, 05:19 AM
  3. Select one of X radio buttons
    By Adrien in forum AWT / Swing
    Replies: 1
    Last Post: 02-25-2010, 06:30 PM
  4. How can I catch the radio button action in jsp?
    By srinivas2828 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-18-2009, 04:05 PM
  5. Deselect Radio Buttons
    By linux1man in forum AWT / Swing
    Replies: 7
    Last Post: 03-21-2009, 04:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •