Basically, I am having trouble figuring out how to work my if statements. The only one that works is the final statement and that is due to it not having the

if("#".equals(e.getActionCommand()))

statement. So, how should I go about allowing the user to select the quantity in the combobox and in turn use my if statements to retrieve that number and return the correct price in the textfield? Thank you for any help.

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

public class BCard extends JFrame
{
    private JPanel panel;
    //I commented these out until I can fix the problems in the code//

    //private JPanel panel2;
    //private JPanel panel3;
    //private JPanel panel4;
   // private JPanel panel5;
    //private JPanel panel6;
    //private JPanel panel7;
    private JLabel msgLbl;
    private JLabel lbl;
    private JLabel lbl2;
    private JRadioButton oneB;
    private JRadioButton bothB;
    private JRadioButton glossB;
    private JRadioButton matteB;
    private JRadioButton twoDB;
    private JRadioButton sevenB;
    private JRadioButton wksB;
    private JRadioButton reqB;
    private JRadioButton dontB;
    private JTextField bCTotal;
    private String[] bC = {"100", "250", "500", "1000", "2000"};
    private JComboBox bCQty;
    private ButtonGroup radioButtonGroupSd;
    private ButtonGroup radioButtonGroupQual;
    private ButtonGroup radioButtonGroupDel;
    private ButtonGroup radioButtonGroupDes;
    private final int WINDOW_WIDTH = 300;
    private final int WINDOW_HEIGHT = 200;
    boolean value = true;

    public BCard()
    {
        setTitle("Business Cards");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildBCPanel();
        add(panel);
        //add(panel2);
        //add(panel3);
        //add(panel4);
        //add(panel5);
        //add(panel6);
        //add(panel7);
        setVisible(value);
    }

     private void buildBCPanel()
    {
        msgLbl = new JLabel("Select the criteria that fits your needs:");
        //Number of cards
        lbl = new JLabel("Quantity:");
        bCQty = new JComboBox(bC);
        //One or both sides printed
        oneB = new JRadioButton("One side");
        bothB = new JRadioButton("Both sides");
        //types of cards
        glossB = new JRadioButton("Gloss");
        matteB = new JRadioButton("Matte");
        //shipping time
        twoDB = new JRadioButton("Next Day");
        sevenB = new JRadioButton("Seven Days");
        wksB = new JRadioButton("Up to Two Weeks");
        // Need us to design card?
        reqB = new JRadioButton("Request Design");
        dontB = new JRadioButton("Don't Require");
        //Price quote
        lbl2 = new JLabel("Your total may be $");
        bCTotal = new JTextField(10);
        bCTotal.setEditable(false);


        radioButtonGroupSd = new ButtonGroup();
        radioButtonGroupSd.add(oneB);
        radioButtonGroupSd.add(bothB);
        radioButtonGroupQual = new ButtonGroup();
        radioButtonGroupQual.add(glossB);
        radioButtonGroupQual.add(matteB);
        radioButtonGroupDel = new ButtonGroup();
        radioButtonGroupDel.add(twoDB);
        radioButtonGroupDel.add(sevenB);
        radioButtonGroupDel.add(wksB);
        radioButtonGroupDes = new ButtonGroup();
        radioButtonGroupDes.add(reqB);
        radioButtonGroupDes.add(dontB);


        bCQty.addActionListener(new ComboListener());
        //I commented out these, so that I can fix one problem at a time//

        //oneB.addActionListener(new RadioButtonListener());
       // bothB.addActionListener(new RadioButtonListener());
        //glossB.addActionListener(new RadioButtonListener());
        //matteB.addActionListener(new RadioButtonListener());
        //twoDB.addActionListener(new RadioButtonListener2());
        //sevenB.addActionListener(new RadioButtonListener2());
        //wksB.addActionListener(new RadioButtonListener2());
        //reqB.addActionListener(new RadioButtonListener());
        //dontB.addActionListener(new RadioButtonListener());

        panel = new JPanel();
        panel.add(msgLbl);
        //panel2 = new JPanel();
        panel.add(lbl);
        panel.add(bCQty);
        //panel3 = new JPanel();
        panel.add(oneB);
        panel.add(bothB);
        //panel4 = new JPanel();
        panel.add(glossB);
        panel.add(matteB);
       // panel5 = new JPanel();
        panel.add(twoDB);
        panel.add(sevenB);
        panel.add(wksB);
       // panel6 = new JPanel();
        panel.add(reqB);
        panel.add(dontB);
        //panel7 = new JPanel();
        panel.add(lbl2);
        panel.add(bCTotal);
    }

     private class ComboListener implements ActionListener
     {
        public void actionPerformed (ActionEvent e)
        {
            //turn double into string so it will show up in the JTextField
            double oneHun = 8.95;
            double twoFif = 11.95;
            double fiveHun = 12.95;
            double oneThou = 13.95;
            double twoThou = 27.95;
            String oneHunS = Double.toString(oneHun);
            String twoFifS = Double.toString(twoFif);
            String fiveHunS = Double.toString(fiveHun);
            String oneThouS = Double.toString(oneThou);
            String twoThouS = Double.toString(twoThou);
            //bCTotal.setText(oneHunS);

            if("100".equals(e.getActionCommand()))
            {
              //JTextField bCTotal = (JTextField)e.getSource();
            bCTotal.setText(oneHunS);
              }
            else if("250".equals(e.getActionCommand()))
            {
            //JTextField bCTotal = (JTextField)e.getSource();
            bCTotal.setText(twoFifS);
              }
            else if("500".equals(e.getActionCommand()))
            {
            //JTextField bCTotal = (JTextField)e.getSource();
            bCTotal.setText(fiveHunS);
              }
            else if("1000".equals(e.getActionCommand()))
            {
            //JTextField bCTotal = (JTextField)e.getSource();
            bCTotal.setText(oneThouS);
              }
            else
            {
                //JTextField bCTotal = (JTextField)e.getSource();
            bCTotal.setText(twoThouS);
            }

        }
            
      
     }

       

    public static void main(String[] args)
     {
        BCard bCard = new BCard();


     }
}

Recommended Answers

All 2 Replies

Your code do not woks as you expected because this line of your code

e.getActionCommand() ;

Always returns comboBoxChanged;
” that’s why the branche excuted in you if statement is always the last else:

else {
                //JTextField bCTotal = (JTextField)e.getSource();
                bCTotal.setText(twoThouS);
            }

You should use instead getItemAt with getSelectedIndex.
The ComboListener class may look like this:

private class ComboListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            //turn double into string so it will show up in the JTextField

            double oneHun = 8.95;
            double twoFif = 11.95;
            double fiveHun = 12.95;
            double oneThou = 13.95;
            double twoThou = 27.95;
            String oneHunS = Double.toString(oneHun);
            String twoFifS = Double.toString(twoFif);
            String fiveHunS = Double.toString(fiveHun);
            String oneThouS = Double.toString(oneThou);
            String twoThouS = Double.toString(twoThou);
            //bCTotal.setText(oneHunS);
            JComboBox comboBox=(JComboBox) e.getSource();
            String s =(String) comboBox.getItemAt(comboBox.getSelectedIndex());

            if ("100".equals(s)) {
                // JTextField bCTotal = (JTextField)e.getSource();
                bCTotal.setText(oneHunS);
            } else if ("250".equals(s)) {
                //JTextField bCTotal = (JTextField)e.getSource();
                bCTotal.setText(twoFifS);
            } else if ("500".equals(s)) {
                //JTextField bCTotal = (JTextField)e.getSource();
                bCTotal.setText(fiveHunS);
            } else if ("1000".equals(s)) {
                //JTextField bCTotal = (JTextField)e.getSource();
                bCTotal.setText(oneThouS);
            } else {
                //JTextField bCTotal = (JTextField)e.getSource();
                bCTotal.setText(twoThouS);
            }

        }
    }

Hope it helps

Thanks for the help. I already figured out a different method of getting it to work. But I tried out yours also, and it worked perfectly. I appreciate the help.

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.