This discussion is archived
4 Replies Latest reply: Nov 30, 2006 11:47 PM by 791266 RSS

ClassCastException

807607 Newbie
Currently Being Moderated
I am trying to get an integer number from the selected item in a combo box but I am getting this exception error. My code is shown below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyBaseConverter extends JFrame implements ActionListener 
{
     private JLabel lblNumber,lblBase;
     private JTextField txtNumber,lblAnswer;
     private JPanel pCentre;
     private JComboBox cmbBase;
     private JButton btnConvert;
     
     
     MyBaseConverter()
     {
          Container c = getContentPane();
          c.setBackground(Color.LIGHT_GRAY);
          
          pCentre = new JPanel();
          pCentre.setBackground(Color.GRAY);
                    
          lblNumber = new JLabel("Please Enter Integer");
          lblNumber.setFont(new Font("Serif",Font.BOLD,14));
          
          txtNumber = new JTextField();
          txtNumber.setColumns(4);
          txtNumber.setFont(new Font("Serif",Font.BOLD,14));
          
          String [] nums ={"2","3","4","5","6","7","8","9","11","12","13","14","15","16","17","18","19","20"};
          
          lblBase = new JLabel("Please Enter New Base");
          lblBase.setFont(new Font("Serif",Font.BOLD,14));
          
          cmbBase = new JComboBox(nums);
          cmbBase.setFont(new Font("Serif",Font.BOLD,14));
          
          btnConvert = new JButton("Convert");
          btnConvert.setFont(new Font("Serif",Font.BOLD,14));
          btnConvert.addActionListener(this);
          
          lblAnswer = new JTextField();
          lblAnswer.setColumns(20);
          lblAnswer.setFont(new Font("Serif",Font.BOLD,14));
          
          pCentre.add(lblNumber);
          pCentre.add(txtNumber);
          pCentre.add(lblBase);
          pCentre.add(cmbBase);
          pCentre.add(btnConvert);
          pCentre.add(lblAnswer);
          c.add(pCentre);
     }
     
     
     public void actionPerformed(ActionEvent e)
     {     
          if(e.getSource() == btnConvert)
          {
               int no = Integer.parseInt(txtNumber.getText().trim());
               int bs = ((Integer)cmbBase.getSelectedItem()).intValue();               
               lblAnswer.setText(""+bs);
          }
     
     }//End of ActionPerformed
     
     public static void main (String args[])
     {     
          MyBaseConverter mbc = new MyBaseConverter();
          mbc.setSize(600,400);
          //mbc.setLocation(0,0);
          mbc.setTitle("My Number Base Converter");
          mbc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          mbc.setVisible(true);
     }
     
}//End of Class
There errors that are showing on the compiler are also listed below, I know it is due to the way I am am trying to cast the combobox item but am unsure how to resolve the situation.
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String
     at MyBaseConverter.actionPerformed(MyBaseConverter.java:60)
     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
     at java.awt.Component.processMouseEvent(Unknown Source)
     at javax.swing.JComponent.processMouseEvent(Unknown Source)
     at java.awt.Component.processEvent(Unknown Source)
     at java.awt.Container.processEvent(Unknown Source)
     at java.awt.Component.dispatchEventImpl(Unknown Source)
     at java.awt.Container.dispatchEventImpl(Unknown Source)
     at java.awt.Component.dispatchEvent(Unknown Source)
     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
     at java.awt.Container.dispatchEventImpl(Unknown Source)
     at java.awt.Window.dispatchEventImpl(Unknown Source)
     at java.awt.Component.dispatchEvent(Unknown Source)
     at java.awt.EventQueue.dispatchEvent(Unknown Source)
     at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
     at java.awt.EventDispatchThread.run(Unknown Source)