JRadioButton is selecting the last one

306 views Asked by At

my question is:- When i have multiple JRadioButtons it only selects the last one to be put into my database table. I have tried alot and i can't figure out why it only insets the last RadioButton. Even when i click a different button no matter what it always selects the last One which is Gluten free Crust. Please help me out.Here is my part of the code that has to do with this Questions.

JButton btnFinish = new JButton("Finish");
    btnFinish.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            try{
                close();
                String query = "insert into Exam2 (FullName, Address, Phonenumber, PizzaSize, CrustType, Toppings, Quantity) values (?,?,?,?,?,?,?)";
                    java.sql.PreparedStatement pst= con.prepareStatement(query);


                    pst.setString(1, txtName.getText());
                    pst.setString(2, txtAddress.getText());
                    pst.setString(3, txtNumber.getText());
                    pst.setString(4, rdbtnSmall.getText());
                    pst.setString(4, rdbtnMedium.getText());
                    pst.setString(4, rdbtnLarge.getText());
                    pst.setString(5, rdbtnHandTossed.getText());
                    pst.setString(5, rdbtnHandmadePan.getText());
                    pst.setString(5, rdbtnBrooklynStyle.getText());
                    pst.setString(5, rdbtnGlutenFreeCrust.getText());
                    pst.setString(6, rdbtnPepperoni.getText());
                    pst.setString(6, rdbtnItalianSausage.getText());
                    pst.setString(6, rdbtnBeef.getText());
                    pst.setString(6, rdbtnHam.getText());
                    pst.setString(6, rdbtnBacon.getText());
                    pst.setString(6, rdbtnOlives.getText());
                    pst.setString(6, rdbtnMushrooms.getText());
                    pst.setString(6, rdbtnOnions.getText());
                    pst.setString(7, textField.getText());



                    pst.execute();
                    close();




                }catch(Exception i) {
                        System.err.println("Exception: " + i.getMessage());
                        JOptionPane.showMessageDialog(null, "There has been an error connecting to the database");
                    }



        }
    });

    JSeparator separator = new JSeparator();

    JLabel lblPizzaSizeAnd = new JLabel("Pizza Size And Crust");
    lblPizzaSizeAnd.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

    rdbtnSmall = new JRadioButton("Small(10\")");

    rdbtnMedium = new JRadioButton("Medium(12\")");

    rdbtnLarge = new JRadioButton("Large(14\")");

    JSeparator separator_1 = new JSeparator();

    JSeparator separator_2 = new JSeparator();
    separator_2.setOrientation(SwingConstants.VERTICAL);

    rdbtnHandmadePan = new JRadioButton("HandMade Pan");
    rdbtnHandmadePan.setToolTipText("Two layers of cheese, toppings to the edge, and a crust that bakes up golden and crispy with a buttery taste.");

    rdbtnHandTossed = new JRadioButton("Hand Tossed");
    rdbtnHandTossed.setToolTipText("Garlic-seasoned crust with a rich, buttery taste.");

    rdbtnBrooklynStyle = new JRadioButton("Brooklyn Style");
    rdbtnBrooklynStyle.setToolTipText("Hand stretched to be big, thin, and perfectly foldable.");

    rdbtnGlutenFreeCrust = new JRadioButton("Gluten Free Crust");
    rdbtnGlutenFreeCrust.setToolTipText("Domino's pizza made with a Gluten Free Crust.");

    group = new ButtonGroup();
    group.add(rdbtnHandmadePan);
    group.add(rdbtnHandTossed);
    group.add(rdbtnBrooklynStyle);
    group.add(rdbtnGlutenFreeCrust);

    JSeparator separator_3 = new JSeparator();

    JLabel lblToopings = new JLabel("Toppings");
    lblToopings.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

    rdbtnPepperoni = new JRadioButton("Pepperoni");

    rdbtnItalianSausage = new JRadioButton("Italian Sausage");

    rdbtnBeef = new JRadioButton("Beef");

    rdbtnHam = new JRadioButton("Ham");

    rdbtnBacon = new JRadioButton("Bacon");

    rdbtnOlives = new JRadioButton("Olives");

    rdbtnMushrooms = new JRadioButton("Mushrooms");

    rdbtnOnions = new JRadioButton("Onions");

    JLabel lblQuantity = new JLabel("Quantity");

    textField = new JTextField();
    textField.setColumns(10);

Thanks!!

1

There are 1 answers

5
MadProgrammer On BEST ANSWER

You are assigning multiple values to a single column...

pst.setString(4, rdbtnSmall.getText());
pst.setString(4, rdbtnMedium.getText());
pst.setString(4, rdbtnLarge.getText());

This means that only the last value you set will actually be used. You should be check which buttons are selected and only adding the selected ones...

You can use the ButtonGroup to more easily get the selected button from the group...

ButtonModel model = bg.getSelection();
String selection = model == null ? "Nothing" : model.getActionCommand();

And for a runnable example...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ButtonGroup bg;

        public TestPane() {

            JRadioButton red = new JRadioButton("Red");
            red.setActionCommand("Red");
            JRadioButton blue = new JRadioButton("Blue");
            blue.setActionCommand("Blue");
            JRadioButton green = new JRadioButton("Green");
            green.setActionCommand("Green");

            bg = new ButtonGroup();
            bg.add(red);
            bg.add(blue);
            bg.add(green);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;
            add(red, gbc);
            add(blue, gbc);
            add(green, gbc);

            gbc.anchor = GridBagConstraints.CENTER;
            JButton select = new JButton("OK");
            add(select, gbc);

            select.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ButtonModel model = bg.getSelection();
                    String selection = model == null ? "Nothing" : model.getActionCommand();
                    JOptionPane.showMessageDialog(TestPane.this, "You selected " + selection);
                }
            });

        }

    }

}

Small database example

ButtonModel model = sizeButtonGroup.getSelection(); 
String size = model == null ? "Small" : model.getActionCommand();

pst.setString(4, size);

// Repeat for other groups/columns