Here my code.

package formsign;

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

public class textF extends JFrame{

        private JTextField fname;
    private JTextField lname;
    private JPasswordField pass;
    private JTextField country;
    private JTextField zip;
    private JTextField month;
    private JTextField day;
    private JTextField year;
    private JTextField phone;
    private JRadioButton male;
    private JRadioButton female;
    private ButtonGroup gender;
    private JButton sign;

        public textF()
    {
        super("Form");
        setLayout(new FlowLayout());
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel name = new JLabel("Name:");
        add(name);
        fname = new JTextField("First Name",13);
        add(fname);
        lname = new JTextField("Last Name",13);

        add(lname); 
        JLabel pas = new JLabel("Password:");
        add(pas);
        pass = new JPasswordField("Enter Password");
        add(pass);
        JLabel count = new JLabel("Country:");
        add(count);
        country = new JTextField("Enter Your Country");
        add(country);
        JLabel zi = new JLabel("Zip:");
        add(zi);
        zip = new JTextField("zip");
        add(zip);
        JLabel date = new JLabel("Date of Birth:");
        add(date);
        month = new JTextField("Month",2);
        add(month);
        day = new JTextField("Day",2);
        add(day);
        year = new JTextField("Year",4);
        add(year);
        JLabel ph = new JLabel("Phone Number:");
        add(ph);
        phone = new JTextField("Enter your Phone");
        add(phone);
        JLabel ge = new JLabel("Gender:");
        add(ge);

        male = new JRadioButton("Male",true);
                male.getActionCommand();
        female = new JRadioButton("Female",false);
                female.getActionCommand();
        gender = new ButtonGroup();
        add(male);
        add(female);
                gender.add(male);
        gender.add(female);


        sign = new JButton("SignUp");
        add(sign);

        theHandler handler = new theHandler();
        fname.addActionListener(handler);
        lname.addActionListener(handler);
        pass.addActionListener(handler);
        country.addActionListener(handler);
        zip.addActionListener(handler);
        month.addActionListener(handler);
        day.addActionListener(handler);
        year.addActionListener(handler);
        phone.addActionListener(handler);
        male.addActionListener(handler);
        female.addActionListener(handler);
        sign.addActionListener(handler);

    }

        private class theHandler implements ActionListener,ItemListener {

        public void actionPerformed(ActionEvent event)
        {
            String fn = "",ln = "",pas = "",country1 = "",zip1 = "",month1 = "",day1 = "",year1 = "",phone1 = "",gender1 = "";


                fn = fname.getText();
                                ln = lname.getText();
                                pas = pass.getText();
                                country1 = country.getText();
                                zip1 = zip.getText();
                                month1 = month.getText();
                                day1 = day.getText();
                                year1 = year.getText();
                                phone1 = phone.getText();



            if(event.getSource()==sign)
            {
                System.out.println("Name: " + fn + " " + ln);
                System.out.println("Password: " + pas);
                System.out.println("Country: " + country1);
                System.out.println("ZIP: " + zip1);
                System.out.println("D.O.B: " + day1 + "-" + month1 + "-" + year1);
                System.out.println("Phone: " + phone1);
                System.out.println("Gender: " + gender.getSelection().getActionCommand());
            }



        }

        @Override
        public void itemStateChanged(ItemEvent arg0) {
            // TODO Auto-generated method stub

                }
        }
}

Recommended Answers

All 2 Replies

Here's the secret. ActionCommand defaults to the button's text but only if you ask the button itself. An explicit ActionCommand is actually stored in the Model but if you query the model there's no default value there.
group.getSelection() returns the model, so doesn't see the default ActionCommand, only an explicit one.
So you need to set the action commands for your radio buttons explicitly.

(Yes, that's a really stupid design mistake in the Java API)

I'm think that is designated for Swing Action

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.