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

Thread: Buttons don't work on using JRadioButton

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Question Buttons don't work on using JRadioButton

    I'm trying to implement simple sample about using JRadioButton and i don't know Why some Buttons works and others not
    That is my code...

    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
     
     
    public class RadioButtonFrame extends JFrame {
     
    	private static final long serialVersionUID = -4257293733892482733L;
    	private JTextField textField = null;
     
    	private Font plainFont = null;
    	private Font italicFont = null;
    	private Font boldFont = null;
    	private Font italic_bold_Font = null;
     
    	private JRadioButton plainRadioButton ;
    	private JRadioButton italicRadioButton;
    	private JRadioButton boldRadioButton;
    	private JRadioButton italicboldRadioButton;
     
    	private ButtonGroup radioGroup ;
     
    	public RadioButtonFrame()
    	{
    		super("Testing RadioButton");
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300, 100);
    		setLayout(new FlowLayout());
     
    		textField = new JTextField("Watch the font style change",25);
    		add(textField);
     
    		plainRadioButton = new JRadioButton("Plain",true);
    		italicRadioButton = new JRadioButton("Italic",false);
    		boldRadioButton = new JRadioButton("Bold",false);
    		italicboldRadioButton = new JRadioButton("Bold/Italic",false);
     
    		add(plainRadioButton);
    		add(italicRadioButton);
    		add(boldRadioButton);
    		add(italicboldRadioButton);
     
    		//Create Logical relationship between JRadioButton.
    		radioGroup = new ButtonGroup();
    		radioGroup.add(plainRadioButton);
    		radioGroup.add(italicRadioButton);
    		radioGroup.add(boldRadioButton);
    		radioGroup.add(italicboldRadioButton);
     
    		//Create font objects.
    		plainFont = new Font("Serif", Font.PLAIN, 14);
    		italicFont = new Font("Serif", Font.ITALIC, 14);
    		boldFont = new Font("Serif", Font.BOLD, 14);
    		italic_bold_Font = new Font("Serif", Font.BOLD + Font.ITALIC  , 14);
    		textField.setFont(plainFont);
     
    		//Register events for JRadioButton.
    		plainRadioButton.addItemListener(new RadioButtonHandler(plainFont));
    		italicboldRadioButton.addItemListener(new RadioButtonHandler(italicFont));
    		boldRadioButton.addItemListener(new RadioButtonHandler(boldFont));
    		italicboldRadioButton.addItemListener(new RadioButtonHandler(italic_bold_Font));
     
    		setVisible(true);
    	}
     
    	private class RadioButtonHandler implements ItemListener{
    		private Font font = null ;
     
    		public RadioButtonHandler(Font f){
    			font = f ;
    		}
     
    		@Override
    		public void itemStateChanged(ItemEvent e) {
    			textField.setFont(font);
    		}
     
    	}
     
    	public static void main(String[] args) throws InterruptedException, InvocationTargetException {
    		SwingUtilities.invokeAndWait(new Runnable() {
     
    			@Override
    			public void run() {
    				new RadioButtonFrame();
    			}
    		});
     
    	}
     
    }

    Another thing about Generated Code for Class what's the benefit about this
    Is this like CLSID in COM Objects in C++ programming.


  2. #2
    Junior Member
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: problem in JRadioButton

    For the first thing:
    You register events for italicboldRadioButton twice,but never register events for italicRadioButton.So some Buttons works and others not.

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: problem in JRadioButton

    Thanks a lot for your replay....
    but what do you talking about i can't see it...???

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: problem in JRadioButton

    This bit I believe

    		//Register events for JRadioButton.
    		plainRadioButton.addItemListener(new RadioButtonHandler(plainFont));
    		[b]italicboldRadioButton.addItemListener(new RadioButtonHandler(italicFont));[/b]
    		boldRadioButton.addItemListener(new RadioButtonHandler(boldFont));
    		[b]italicboldRadioButton.addItemListener(new RadioButtonHandler(italic_bold_Font));[/b]

    The normal italic radio button is never given an event listener.

    // Json

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: problem in JRadioButton

    Thanks a lot for hint.
    i was tired.