-1

I'm pretty lost on this. I have no idea how to get my program to work. Basically, when an option is selected a price should show up in the text box automatically. Thank you all for your help.

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JComboBox cannot be cast to javax.swing.JTextField
at BCard$ComboListener.actionPerformed(BCard.java:164)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:567)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:603)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:816)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:480)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

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

public class BCard extends JFrame
{
    private JPanel panel;
    //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:");
        lbl = new JLabel("Quantity:");
        bCQty = new JComboBox(bC);
        oneB = new JRadioButton("One side");
        bothB = new JRadioButton("Both sides");
        glossB = new JRadioButton("Gloss");
        matteB = new JRadioButton("Matte");
        twoDB = new JRadioButton("Next Day");
        sevenB = new JRadioButton("Seven Days");
        wksB = new JRadioButton("Up to Two Weeks");
        reqB = new JRadioButton("Request Design");
        dontB = new JRadioButton("Don't Require");
        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());
        //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)
        {
            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);

            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();


     }
}

Edited by Nick Evan: Added code-tags

2
Contributors
4
Replies
9
Views
6 Years
Discussion Span
Last Post by lwschjang
0

e.getSource() returns the component that generated the event, which in your case is the combo box. You cannot cast that JComboBox reference to a JTextField.

0

e.getSource() returns the component that generated the event, which in your case is the combo box. You cannot cast that JComboBox reference to a JTextField.

Ok, I got you. I commented out the

JTextField bCTotal = (JTextField)e.getSource();

in each if statement. When I did that and selected a quantity, 27.95 showed up in the textfield. I realize that was do to the fact that the final if statement did not have the statement

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

in it. So I am figuring that I need to find a different way to go about finding the quantity. Any help on how I should go about this would be great.

0

You can get the selected object from the combo box directly with getSelectedObject()If you already have a reference to combo box, you can use it directly, otherwise you can cast the event source (JComboBox)e.getSource() and use that.

0

You can get the selected object from the combo box directly with getSelectedObject()If you already have a reference to combo box, you can use it directly, otherwise you can cast the event source (JComboBox)e.getSource() and use that.

So would that look something like this:

if(e.getSelectedItem() == 100)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.