Results 1 to 17 of 17
  1. #1
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Help in Radion Buttons

    Hello,
    How do I deselect a radio button when I click my reset button?
    I tried radiobutton.setSelected(false); and seems to be not the right code.
    I also tried buttongroup.setselection and its not working or I just dont know how to use it?

    Help in Radion Buttons-tanong.png

  2. #2
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    Help in Radion Buttons-tanong-again.png
    Here is what I really did :D

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help in Radion Buttons

    If you're using Java 1.6 or 1.7, you could always call the ButtonGroup#clearSelection() method.

    In the future, please post your real code here using code tags, not images, as it makes it much easier for us to copy, paste and test. Welcome to the java-forums, by the way!

  4. #4
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    Java Code:
    package rockpaperscissorsGUI;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    import javax.swing.Box;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    
    
    public class RockPaperScissors implements ActionListener {
    
    	JFrame frame;
    	JPanel panel , radio1panel , radio2panel;
    	JLabel player1 , player2;
    	JRadioButton rock , paper,scissors , rock1 , paper1 , scissors1;
    	JButton winner , reset;
    	Box box;
    	JTextField name1,name2;
    	
    	
    public RockPaperScissors(){
    	frame = new JFrame("Rock, Paper, Scissors JUDGE!");
    	panel = new JPanel();
    	player1 = new JLabel("Player 1");
    	player2 = new JLabel("Player 2");
    	rock = new JRadioButton("Rock");
    	paper = new JRadioButton("Paper");
    	scissors = new JRadioButton ("Scissors");
    	rock1 = new JRadioButton("Rock");
    	paper1 = new JRadioButton("Paper");
    	scissors1 = new JRadioButton("Scissors");
    	winner = new JButton("Check the Winner");
    	reset = new JButton("Reset");
    	name1 = new JTextField("Player 1");
    	name2 = new JTextField("Player 2");
    	//name1.setSize(10,10);
    	//name2.setSize(10,10);
    	 //Group the radio buttons. for player 1
        ButtonGroup group = new ButtonGroup();
        group.add(rock);
        group.add(paper);
        group.add(scissors);
        
        // group the radio buttons for player 2
        ButtonGroup group2 = new ButtonGroup();
        group2.add(rock1);
        group2.add(paper1);
        group2.add(scissors1);
        
        //group in a panel
       
      
        
        
        frame.add(panel);
        //panel.add(player1);
        
        panel.add(name1);
        panel.add(rock);
        panel.add(paper);
        panel.add(scissors);
        /*
        panel.add(radio2panel);
        radio2panel.add(rock1);
        radio2panel.add(paper1);
        radio2panel.add(scissors1);
        
        panel.add(radio1panel);
        radio1panel.add(rock);
        radio1panel.add(paper);
        radio1panel.add(scissors);*/
        //panel.add(player2);
        panel.add(name2);
        panel.add(rock1);
        panel.add(paper1);
        panel.add(scissors1);
        
       
        panel.add(winner);
        winner.addActionListener(this);
        panel.add(reset);
        reset.addActionListener(this);
        rock.setMnemonic(KeyEvent.VK_R);
        paper.setMnemonic(KeyEvent.VK_P);
        scissors.setMnemonic(KeyEvent.VK_S);
        rock1.setMnemonic(KeyEvent.VK_O);
        paper1.setMnemonic(KeyEvent.VK_A);
        scissors1.setMnemonic(KeyEvent.VK_C);
        winner.setMnemonic(KeyEvent.VK_W);
        frame.setSize(300,150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    	
    	}
    public static void main(String[] args){
    	
    	new RockPaperScissors();
    	
    }
    
    @Override
    public void actionPerformed(ActionEvent event) {
    	String player1name , player2name;
    	player1name = name1.getText();
    	player2name = name2.getText();
    	if (event.getSource()==winner)
    		{
    			if ((rock.isSelected()) && (paper1.isSelected()))
    					JOptionPane.showMessageDialog(null, "The winner is "+ player2name + "! \nCongratulations!"  
    							,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else if ((rock.isSelected()) && (scissors1.isSelected()))
    			JOptionPane.showMessageDialog(null, "The winner is "+ player1name + "! \nCongratulations!"  
    					,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else if ((rock.isSelected()) && (rock1.isSelected()))
    			JOptionPane.showMessageDialog(null, "It's a tie! \nCongratulations!"  
    					,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			
    			else if ((paper.isSelected()) && (rock1.isSelected()))
    				JOptionPane.showMessageDialog(null, "The winner is "+ player1name + "! \nCongratulations!"  
    						,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else if ((paper.isSelected()) && (scissors1.isSelected()))
    				JOptionPane.showMessageDialog(null, "The winner is "+ player2name + "! \nCongratulations!"  
    						,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else if ((paper.isSelected()) && (paper1.isSelected()))
    				JOptionPane.showMessageDialog(null, "It's a tie! \nCongratulations!"  
    						,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			
    			else if ((scissors.isSelected()) && (rock1.isSelected()))
    				JOptionPane.showMessageDialog(null, "The winner is "+ player2name + "! \nCongratulations!"  
    						,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else if ((scissors.isSelected()) && (scissors1.isSelected()))
    				JOptionPane.showMessageDialog(null, "It's a tie! \nCongratulations!"  
    						,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else if ((scissors.isSelected()) && (paper1.isSelected()))
    				JOptionPane.showMessageDialog(null, "The winner is "+ player1name + "! \nCongratulations!"  
    						,"Rock Paper and Scissors Judge", JOptionPane.INFORMATION_MESSAGE);
    			else JOptionPane.showMessageDialog(null, "Please Select from the options! \n Thank you"  
    					,"Rock Paper and Scissors Judge", JOptionPane.ERROR_MESSAGE);
    		}
    	if (event.getSource()==reset)
    		{name1.setText("Player 1");
    		name2.setText("Player 2");
    		/*
    		rock.setSelected(false);
    		rock1.setSelected(false);
    		paper.setSelected(false);
    		paper1.setSelected(false);
    		scissors1.setSelected(false);
    		scissors.setSelected(false);*/
    		//to be continued
    		//group.setSelection
    		
    		//group.getSelectedJRadioButton();
    		/*if (scissors.isSelected())
    			scissors.setSelected(false);*/
    		
    		}
    	
    	
    }
    }
    sorry I don't know how to use code tags so I copied the whole code
    I'm also new to GUI and I mad a judge for rock paper and scissors..
    Is there another way besides clearSelection?
    Last edited by Fubarable; 07-22-2012 at 01:39 AM. Reason: code tags added

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help in Radion Buttons

    Quote Originally Posted by tjt View Post
    sorry I don't know how to use code tags so I copied the whole code
    I've edited your post and have added code tags. To do this yourself in the future, simply highlight your code and press the code button at the top of the editor window, either that or surround your code with [code] [/code] tags.

    I'm also new to GUI and I mad a judge for rock paper and scissors..
    Is there another way besides clearSelection?
    First please tell us why you're dead set against using clearSelection(). It's the obvious solution to your problem, so why use a kludge without a good reason, and if you have a good reason, then please let us know?

  6. #6
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    Quote Originally Posted by Fubarable View Post
    I've edited your post and have added code tags. To do this yourself in the future, simply highlight your code and press the code button at the top of the editor window, either that or surround your code with [code] [/code] tags.
    Thanks :D

    and clearSelection seems to be not working or did I use the code incorrectly ??? or should I update my java?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help in Radion Buttons

    Quote Originally Posted by tjt View Post
    and clearSelection seems to be not working or did I use the code incorrectly ??? or should I update my java?
    ???

    It's hard to say anything since you don't show us how you tried to use it or what errors or problems it caused. Consider giving us more information.

  8. #8
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    Help in Radion Buttons-here.png
    here.. I'm really new to GUI I really don't know what to do.. :D

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help in Radion Buttons

    This has nothing to do with Swing, nothing to do with ButtonGroups, and all to do with your variable being out of scope. Your current ButtonGroup variable, group, is declared in the class's constructor and is thus only visible inside of the constructor. Declare it in the class as a non-static field, make sure that you don't re-declare it in the constructor and then you'll be able to use it throughout the class.

  10. #10
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    hmm can you show it via coding and where to place it in my code?.. I can't understand what you're saying.. I'm sorry i'm really new :D Thank you

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help in Radion Buttons

    Simply take ButtonGroup group = new ButtonGroup() outside of the constructor and put it in the class, that's it. If you declare it in the constructor, ButtonGroup group, then it is only visible in the scope in which it was declared, here the constructor. You need it visible outside of the constructor. Please google and read up on variable scope as you really need to fully understand this concept, not just the fix for this program.

  12. #12
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    Ok thanks

  13. #13
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    Now, it's working thanks..

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help in Radion Buttons

    You're welcome, but more importantly, do you understand the problem, because it's sure to come up again in a different program?

  15. #15
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Default Re: Help in Radion Buttons

    hmm just a little bit. but i'll read more about GUI can you recommend any links or ebooks??? I'm using this link to study about GUI Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) but I get more of the answers to my questions here :D

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

    Default Re: Help in Radion Buttons

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

  17. #17
    tjt
    tjt is offline Member
    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Help....add actionListener to buttons
    By jmeats77 in forum New To Java
    Replies: 9
    Last Post: 05-24-2012, 02:11 AM
  2. Help with Buttons
    By wld4ubabay in forum New To Java
    Replies: 20
    Last Post: 05-17-2010, 09:13 AM
  3. Where To Get Pop Up Buttons
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2009, 05:05 AM
  4. Applet buttons
    By h3ckf1r3 in forum Java Applets
    Replies: 6
    Last Post: 09-22-2008, 10:15 PM
  5. How to use SWT Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:44 PM

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
  •