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

Thread: JRadioButton Problems

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question JRadioButton Problems

    Hi,

    The code below compiles without any errors, but the JRadioButton's dont seem to respond when any are selected.

    In the actionsPerformed() method, if JButton 'next' is clicked:

    -then proceed to assign data from JTextFields
    -if either one of the JRadioButton's are selected, then..
    -assign data to variables
    -end of method.

    The JButton 'next' works correctly, but the JRadioButton's 'easy', 'mild' and 'hard' don't respond (no data are assigned to the variables 'questions' and 'level').

    Thanks in advanced.

    public void actionPerformed(ActionEvent e)
    {	
    	//if JButton 'next' is clicked..
    	if(e.getSource() == next)
    	{
    		//Get details from textfield..
    		name = String.valueOf(nameField.getText());
    		topic = String.valueOf(topicField.getText());
     
    		//also if JRadioButton 'easy' is selected..
    		if(e.getSource() == easy)
    		{
    			questions = layQuestions.getEasyQuestionList();
    			level = "Easy";
    		}
    		//or if JRadioButton 'mild' is selected..
    		else if(e.getSource() == mild)
    		{
    			questions = layQuestions.getEasyQuestionList();
    			level = "Mild";
    		}
    		//or if JRadioButton 'hard' is selected..
    		else if(e.getSource() == hard)
    		{
    			questions = layQuestions.getHardQuestionList();
    			level = "Hard";
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JRadioButton Problems

    Can you make and post a small complete program that compiles, executes and shows the problem?

    If the source of the event is next, how can the source be any of the other components?
    Only one if test will be true.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: JRadioButton Problems

    You can't have it both ways -- you can't have the ActionEvent's source be two different objects at the same time. What you should do is get the ButtonGroup object and find out from it which ButtonModel is the one that has been selected.

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JRadioButton Problems

    Thanks for the replies.

    So how would I go about using ButtonModel ? Would it be inside the actionPerformed() method, or in a separate method?

    Thanks

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JRadioButton Problems

    What have you tried? There are several examples in the API doc on how to use JRadioButtons.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: JRadioButton Problems

    Thanks for your help, I've got them working now

Similar Threads

  1. How to select default value of jRadioButton inside jTable
    By tomrey in forum AWT / Java Swing
    Replies: 1
    Last Post: February 27th, 2013, 06:12 PM
  2. [SOLVED] Listen to two JRadioButton + ons JButton to produce output
    By voltaire in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2010, 03:46 PM
  3. [SOLVED] JRadioButton Event Handling Help?
    By CS313e in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2010, 11:56 AM
  4. Replies: 0
    Last Post: February 23rd, 2010, 02:12 PM
  5. Buttons don't work on using JRadioButton
    By neo_2010 in forum AWT / Java Swing
    Replies: 4
    Last Post: July 11th, 2009, 11:37 AM