mercredi 17 mai 2017

JavaSwing replacing the CheckBox with a ComboBox

I know that that's not the way to do it but I have to submit the GUI Applet after couple of hours and I just can't fix it myself. I've done everything else except adding a ComboBox with a "Male" & "Female". How can I just replace the CheckBox with an uneditable combo box and still make the "Send" button show the choice? That's my code:

    package MyApplet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JButton;

public class MyApplet extends JApplet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected JTextField mTxtName;
    //protected JTextField mTxtEmail;

    protected JRadioButton mRdbone;
    protected JRadioButton mRdbtwo;
    protected JRadioButton mRdbthree;
    protected JRadioButton mRdbfour;
    protected JRadioButton mRdbfive;
    protected ButtonGroup mRdbGroup;

    protected JCheckBox mChbSport;
    protected JCheckBox mChbTV;
    protected JCheckBox mChbMusic;

    protected JButton mBtnSend;
    protected JButton mBtnClear;

    /**
     * Create the applet.
     */
    public MyApplet() {

        mTxtName = new JTextField();
        //mTxtEmail = new JTextField();

        mRdbone = new JRadioButton("10-20");    
        mRdbtwo = new JRadioButton("21-30");
        mRdbthree = new JRadioButton("31-40");  
        mRdbfour = new JRadioButton("41-50");
        mRdbfive = new JRadioButton("51-"); 

        mRdbGroup = new ButtonGroup();

        mChbSport = new JCheckBox("Sport"); 
        mChbTV = new JCheckBox("TV");
        mChbMusic = new JCheckBox("Music");

        mBtnSend = new JButton("Send");
        mBtnClear = new JButton("Clear");

    }

    @Override
    public void init()
    {
        super.init();

        getContentPane().setLayout(null);

        JLabel lblEnterName = new JLabel("Enter name:");
        lblEnterName.setBounds(12, 12, 92, 15);
        getContentPane().add(lblEnterName);

        //JLabel lblEnterEmail = new JLabel("Enter e-mail:");
        //lblEnterEmail.setBounds(12, 39, 92, 15);
        //getContentPane().add(lblEnterEmail);

        JLabel lblEnterYourGender = new JLabel("Enter your age:");
        lblEnterYourGender.setBounds(12, 80, 140, 15);
        getContentPane().add(lblEnterYourGender);

        JLabel lblEnterYourHobby = new JLabel("You are:");
        lblEnterYourHobby.setBounds(12, 167, 140, 15);
        getContentPane().add(lblEnterYourHobby);

        mTxtName.setBounds(160, 10, 114, 19);
        getContentPane().add(mTxtName);
        mTxtName.setColumns(10);

        //mTxtEmail.setBounds(160, 37, 114, 19);
        //getContentPane().add(mTxtEmail);
        //mTxtEmail.setColumns(10);

        mRdbone.setBounds(160, 40, 149, 23);
        getContentPane().add(mRdbone);
        mRdbone.setActionCommand("10-20");
        mRdbone.setSelected(true);

        mRdbtwo.setBounds(160, 60, 149, 23);
        getContentPane().add(mRdbtwo);
        mRdbtwo.setActionCommand("21-30");

        mRdbthree.setBounds(160, 80, 149, 23);
        getContentPane().add(mRdbthree);
        mRdbthree.setActionCommand("31-40");

        mRdbfour.setBounds(160, 100, 149, 23);
        getContentPane().add(mRdbfour);
        mRdbfour.setActionCommand("41-50");

        mRdbfive.setBounds(160, 120, 149, 23);
        getContentPane().add(mRdbfive);
        mRdbfive.setActionCommand("50-");

        mRdbGroup.add(mRdbone);
        mRdbGroup.add(mRdbtwo);
        mRdbGroup.add(mRdbthree);
        mRdbGroup.add(mRdbfour);
        mRdbGroup.add(mRdbfive);

        mChbSport.setBounds(160, 136, 129, 23);
        getContentPane().add(mChbSport);

        mChbTV.setBounds(160, 163, 129, 23);
        getContentPane().add(mChbTV);

        mChbMusic.setBounds(160, 190, 129, 23);
        getContentPane().add(mChbMusic);

        mBtnSend.setBounds(35, 227, 117, 25);
        getContentPane().add(mBtnSend);

        mBtnClear.setBounds(160, 227, 117, 25);
        getContentPane().add(mBtnClear);

        mBtnSend.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                OnBtnSendClicked(e);  
            }
        });

        mBtnClear.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                OnBtnClearClicked(e);  
            }
        });

        this.getRootPane().setDefaultButton(mBtnSend);
    }

    protected void CLearControls()
    {
        mTxtName.setText("");
        //mTxtEmail.setText("");

        mRdbone.setSelected(true);

        mChbSport.setSelected(false);
        mChbTV.setSelected(false);
        mChbMusic.setSelected(false);

    }

    protected boolean Validate()
    {
        if (mTxtName.getText().trim().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Please enter name");
            mTxtName.requestFocus();
            return false;
        }

        //if (mTxtEmail.getText().trim().isEmpty())
        //{
        //  JOptionPane.showMessageDialog(this, "Please enter E-mail");
        //  mTxtEmail.requestFocus();
        //  return false;
        //}

        if (mRdbGroup.getSelection() == null )
        {
            JOptionPane.showMessageDialog(this, "Please choose you age group");
            mRdbone.requestFocus();
            return false;
        }

        return true;
    }

    protected void OnBtnSendClicked(ActionEvent e)
    {
        if (!Validate())
            return;

        String strMessage = "";
        strMessage += ("Name: "+ mTxtName.getText() + "\n");
        //strMessage += ("E-mail: " + mTxtEmail.getText() + "\n");
        strMessage += ("Gender: " + mRdbGroup.getSelection().getActionCommand() + "\n");

        String strHobies = "";
        strHobies += mChbSport.isSelected() ? "Sport " : "";
        strHobies += mChbTV.isSelected() ? "TV " : "";
        strHobies += mChbMusic.isSelected() ? "Music " : "";
        if (strHobies.isEmpty())
            strHobies = "None";

        strMessage += ("Hobies: " + strHobies + "\n");

        JOptionPane.showMessageDialog(this, strMessage);
    }

    protected void OnBtnClearClicked(ActionEvent e)
    {
        CLearControls();

        mTxtName.requestFocus();
    }

}

GUI




Aucun commentaire:

Enregistrer un commentaire