Results 1 to 5 of 5

Thread: Finding the radio button that have been clicked [*Resolved*]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved Finding the radio button that have been clicked [*Resolved*]

    Hello,

    I have a user interface using swing. I have 3 radio buttons grouped (Economy, Business, First). I want to find which one the user has clicked. I have grouped the radio buttons and placed them inside a panel. I have added addItemListener to each one. I have used to the code below to print out the radio button that have been clicked. I want to print out either Economy, Business or First, depending on which one the user clicked.

    All this code does is print out a load of rubbish, each time a radio button is clicked. Not sure why.

    VB Code:
    1. public void itemStateChanged(ItemEvent event)
    2.     {
    3.         String item = "";
    4.        
    5.         if ( event.getStateChange() == ItemEvent.SELECTED )
    6.         {  
    7.             item = event.getItem().toString();
    8.                        
    9.             System.out.println("Radio Button: " + item);
    10.         }      
    11.     }

    Thanks in advance,

    Steve
    Last edited by steve_rm; Sep 4th, 2006 at 06:34 AM.
    steve

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Finding the radio button that have been clicked

    One way is to add an action command to the radio button where you create it. Once you have the action command set, then you can reference it:

    in the init (or where ever you are creating the button):
    Code:
    JRadioButton jrb = new JRadioButton("Test");
    jrb.setActionCommand("Test");
    in the listener thingy, change your code to this:
    Code:
    		if ( event.getStateChange() == ItemEvent.SELECTED )
    		{	
    			System.out.println("Radio Button: " + event.getActionCommand());
    		}
    The code has not been tested, but I believe it will work. There are several other ways of doing this, but I think this is the easiest for you.


    NOTE: You should (in my opinion) be using an actionlistener on the radio buttons as opposed to the item listener.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Finding the radio button that have been clicked

    Hello System_Error,

    Thanks for your reply. Unfortunity the code did not work, there was a compile error:
    cannot resolve symbol
    symbol : method getActionCommand ()

    I think that this is because I am using it in the itemStateChanged method. Possible?

    Is there another way to solve this?

    Thanks in advance,

    Steve
    steve

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Finding the radio button that have been clicked

    Use an actionlistener rather than an itemlistener.... You should have done that in the first place.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved Re: Finding the radio button that have been clicked [* Resolved *]

    Hello,

    Thanks for your help. This is the solution that worked.

    rdoReturn.addActionListener(this);

    public void actionPerformed(ActionEvent action)
    {
    System.out.println("Radio Button in action: " + action.getActionCommand() );
    }

    Many thanks.
    steve

Posting Permissions

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



Click Here to Expand Forum to Full Width