0

This Java code is using Swing. I am trying to generate a form so that Swing basic would be covered in it. It has label, button, textfield but radio button is not working. Please tell me the errors in it. Is it because I am not adding radio buttons in panel?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Swingtrial extends JFrame {

    final String[] sList
        = {
            "JANUARY",
            "FEBRUARY",
            "MARCH",
            "APRIL",
            "MAY",
            "JUNE",
            "JULY",
            "AUGUST",
            "SEPTEMBER",
            "OCTOBER",
            "NOVEMBER",
            "DECEMBER",};

    public Swingtrial() {
        callGUI();
    }

    public void callGUI() {
        int s[];
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        setTitle("TRIAL FORM");

        panel.setLayout(null);
        panel.setSize(200, 200);
        JLabel lab = new JLabel("Name");
        lab.setBounds(200, 60, 100, 25);
        panel.add(lab);

        JTextField name = new JTextField("first name");
        name.setBounds(275, 60, 100, 25);
        panel.add(name);

        JTextField name1 = new JTextField("middle name");
        name1.setBounds(380, 60, 100, 25);
        panel.add(name1);

        JTextField name2 = new JTextField("last name");
        name2.setBounds(500, 60, 150, 25);
        panel.add(name2);

        JLabel age = new JLabel("Date of Birth");
        age.setBounds(200, 100, 150, 25);
        panel.add(age);

        JComboBox date = new JComboBox();
        date.setBounds(275, 100, 50, 25);
        date.setBackground(Color.LIGHT_GRAY);

        panel.add(date);
        for (int i = 1; i <= 31; i++) {
            extracted(date, i);
        }

        JComboBox drop = new JComboBox();
        drop.setBounds(350, 100, 85, 25);
        drop.setBackground(Color.LIGHT_GRAY);
        panel.add(drop);
        for (int iCtr = 0; iCtr < sList.length; iCtr++) {
            drop.addItem(sList[iCtr]);
        }

        JComboBox yr = new JComboBox();
        yr.setBounds(455, 100, 80, 25);
        yr.setBackground(Color.LIGHT_GRAY);
        panel.add(yr);
        for (int i = 1900; i <= 2014; i++) {
            extracted(yr, i);  // not understood?????? :(  
        }

        JLabel add = new JLabel("address");

        add.setBounds(200, 150, 100, 25);
        panel.add(add);

        JTextField address = new JTextField("Adress");
        address.setBounds(280, 150, 200, 40);
        panel.add(address);

        JRadioButton male = new JRadioButton("male");
        JRadioButton female = new JRadioButton("Female");
        ButtonGroup bG = new ButtonGroup();
        add(male);
        bG.add(female);
        this.setSize(100, 200);
        this.setLayout(new FlowLayout());
        this.add(male);
        this.add(female);
        male.setSelected(true);
        this.setVisible(true);

        JButton but = new JButton("ommm");
        but.setBounds(50, 60, 80, 30);
        panel.add(but);
        but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
    }

    private void extracted(JComboBox month, int i) {
        month.addItem(i);
    }

    public static void main(String args[]) {
        Swingtrial mainFrame = new Swingtrial();
        mainFrame.setVisible(true);
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Eager
  • 1
  • 2
  • 1
    Just because you have a 'Query about Java' does not mean the [tag:jquery] **tag** has any relevance to it. Please read the tag pop-ups carefully before applying them to a question. – Andrew Thompson Jan 11 '14 at 10:41
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 11 '14 at 10:43
  • 1
    BTW - that's a lot of code to just get two check-boxes on-screen. ;) I realize you must be looking to expand the GUI later with the other controls. But I'd like to stress that it is better to do it using layouts. Would you be willing to provide provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height, so we can show you how? It would need to be on a separate question, since this one has been asked and answered, and the layout is a separate matter.. – Andrew Thompson Jan 11 '14 at 11:00
  • @AndrewThompson,i m only a week old in java programming.and my program was not suppose to give just 2 radio button,it was to build the whole form including radio buttons.if u analyse my code u shall understand that – Eager Jan 11 '14 at 18:05
  • *"if u analyse my"* What is your budget? OK that was just kidding around. My point is your not paying me enough to do so.. If you cannot provide a drawing or ASCII art, good luck with it. – Andrew Thompson Jan 11 '14 at 18:21

1 Answers1

3

Add male to the ButtonGroup:

The error is contained in the commented line:

    JRadioButton male = new JRadioButton("male");
    JRadioButton female = new JRadioButton("Female");
    ButtonGroup bG = new ButtonGroup();
    add(male);   //this adds the component to the JFrame not the button group
    bG.add(female);

And can be fixed using:

bG.add(male);
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189