Create Radio Button with Text Field in Java

Posted By: Java Examples - 8:39 PM

Share

& Comment

In this program, you will learn how to set the radio buttons in a group so that only one can be selected at a time.
This program shows five radio buttons with labeled by plain,bold,italic,boldItalic .This program also show a label which contains the text "Font style changes" but when you click on any radio button from a Button Group the text of the selected radio button is shown on the label and a message box will be shown with message holds the selected radio button label. This is done through the generating event for the different-different radio buttons.
Following are the screen shots for the result of the given program:
getActionCommand():
This is the method of the ActionEvent class which returns the source title in string of the generated event.


Java version of Radio Button is given bellow :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author ACHCHUTHAN
 */
public class RadioButtonTest extends JFrame {

    private JTextField field;
    private JRadioButton plain, bold, italic, boldItalic;
    private Font plainFont, boldFont, italicFont, boldItalicFont;
    private ButtonGroup radioGroup;

    RadioButtonTest() {
        super("Testing Radiobutton");
        Container c = getContentPane();
        c.setLayout(new FlowLayout());

        field = new JTextField("Font style changes", 20);
        c.add(field);

        plain = new JRadioButton("plain", true);
        c.add(plain);

        bold = new JRadioButton("bold", false);
        c.add(bold);

        italic = new JRadioButton("italic", false);
        c.add(italic);

        boldItalic = new JRadioButton("boldItalic", false);
        c.add(boldItalic);

        radioGroup = new ButtonGroup();
        radioGroup.add(plain);
        radioGroup.add(bold);
        radioGroup.add(italic);
        radioGroup.add(boldItalic);

        plainFont = new Font("serif", Font.PLAIN, 14);
        boldFont = new Font("serif", Font.BOLD, 14);
        italicFont = new Font("serif", Font.ITALIC, 14);
        boldItalicFont = new Font("serif", Font.BOLD + Font.ITALIC, 14);
        field.setFont(plainFont);

        plain.addItemListener(new RadioButtonHandler(plainFont));
        bold.addItemListener(new RadioButtonHandler(boldFont));
        italic.addItemListener(new RadioButtonHandler(italicFont));
        boldItalic.addItemListener(new RadioButtonHandler(boldItalicFont));
        setSize(275, 150);
        setVisible(true);

    }

    public class RadioButtonHandler implements ItemListener {
        private Font font;
        public RadioButtonHandler(Font f) {
            font = f;
        }

        public void itemStateChanged(ItemEvent e) {
            field.setFont(font);
        }
    }

    public static void main(String args[]) {
        RadioButtonTest b = new RadioButtonTest();
        b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Output of this program :


About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

0 comments :

Post a Comment

Thank you for vising Java90.blogspot.com

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .