I have been playing about with some code tonight and trying to add things to it, so i know it wont make sense when talking about cats and dogs, females and males and music, I run this code and the GUI comes up I click add CD and the required fields appear but i get the error

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox

I think this has something to do with the actionlistener looking for a combobox when it has a jbutton but i am unsure how to fix it I have tried casting the jbuttons but doesnt seem to work, if someone could show me how to fix this issue i would be greatful, melting

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;

import javax.swing.*;

public class mainInterface extends JFrame implements ActionListener

{
    
     private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton jButton6;
    private JButton jButton7;
    private JButton jButton8;
    private JPanel jPanel1;
    private String[] petStrings = {"Bird","Cat","Dog"};
    private String[] ownerString = {"Male","Female","Other"};
    private String[] colourString = {"Red","White","Blue"};

    private JTextField text1;
    private JTextField text2;
    private JTextField text3;
    private JTextField text4;
    private JTextField text5;
    private JTextField text6;
    private JTextField text7;
    private JTextField text8;
    private JTextField text9;

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;

    private JPanel bottomPanel;
    private JPanel topPanel;               //  declaring panels
    private JPanel holdAll;

    private JPanel one;
    private JPanel two;
    private JPanel three;
    private JPanel four;
    private JPanel five;
    private JPanel six;
    private JPanel seven;
    private JPanel ten;
    private JComboBox petList;
    private JComboBox ownerList;
    private JComboBox colourList;
    
    public mainInterface() 
    {
        jButton1 = new JButton("Add CD");
        jButton2 = new JButton("Add Video");
        jButton3 = new JButton("Total Play Time");
        jButton4 = new JButton("Create Playlist");
        jButton5 = new JButton("Show Library");
        jButton6 = new JButton("Quit");
        jButton7 = new JButton("Buy");
        jButton8 = new JButton("Reset");
        petList = new JComboBox(petStrings);
        ownerList = new JComboBox(ownerString);
        colourList = new JComboBox(colourString);
        

        topPanel = new JPanel();
        holdAll  = new JPanel();
        bottomPanel = new JPanel();

        one = new JPanel();
        two = new JPanel();
        three = new JPanel();
        four = new JPanel();
        five = new JPanel();
        six = new JPanel();
        seven = new JPanel();
        ten = new JPanel();
        text9 = new JTextField();
        text1 = new JTextField(15);
        label1 = new JLabel("Title: ");
        text2 = new JTextField(15);
        label2 = new JLabel("Artist: ");
        text3 = new JTextField(15);
        label3 = new JLabel("Length: ");
        text4 = new JTextField(15);
        label4 = new JLabel("Num of Tracks: ");
        label5 = new JLabel("Welcome to Kevins Jukebox");
        label9 = new JLabel("You are");
        
        

        int flag = 0;
        drawApp(flag);

        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);
        jButton4.addActionListener(this);                           
        jButton5.addActionListener(this);
        jButton6.addActionListener(this);
        jButton7.addActionListener(this);
        jButton8.addActionListener(this);
        petList.addActionListener(this);
        ownerList.addActionListener(this);
        colourList.addActionListener(this);


        
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    
    public void drawApp(int flag)
    {
        topPanel.setLayout(new FlowLayout());
            topPanel.add(jButton1);
            topPanel.add(jButton2);
            topPanel.add(jButton3);
            topPanel.add(jButton4);
            topPanel.add(jButton5);
            topPanel.add(jButton6);
            topPanel.add(petList);
            
            
        bottomPanel.add(label5);
        

        holdAll.setLayout(new BorderLayout());
            holdAll.add(topPanel, BorderLayout.NORTH);
            holdAll.add(bottomPanel, BorderLayout.CENTER);

        if (flag == 0)
            bottomPanel.add(label5);
        else
            bottomPanel.remove(label5);

        if (flag == 1)
        {
            
            one.add(label1);
            one.add(text1);
            bottomPanel.add(one);
            two.add(label2);
            two.add(text2);
            bottomPanel.add(two);
            three.add(label3);
            three.add(text3);
            bottomPanel.add(three);
            four.add(label4);
            four.add(text4);
            bottomPanel.add(four);

    
            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
    
        }
            
        if (flag == 2)
        {

            one.add(label1);
            one.add(text1);
            bottomPanel.add(one);
            two.add(label2);
            two.add(text2);
            bottomPanel.add(two);
            three.add(label3);
            three.add(text3);
            bottomPanel.add(three);
            four.add(label4);
            four.add(text4);
            bottomPanel.add(four);
            bottomPanel.add(jButton7);

            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
        }
            
        if (flag == 3)
        {

            one.add(label1);
            one.add(text1);
            bottomPanel.add(one);
            two.add(label2);
            two.add(text2);
            bottomPanel.add(two);
            three.add(label3);
            three.add(text3);
            bottomPanel.add(three);
            four.add(label4);
            four.add(text4);
            bottomPanel.add(four);
            bottomPanel.add(jButton7);

            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
        }
            
        if (flag == 4)
        {

            one.add(label1);
            one.add(text1);
            bottomPanel.add(one);
            two.add(label2);
            two.add(text2);
            bottomPanel.add(two);
            three.add(label3);
            three.add(text3);
            bottomPanel.add(three);
            four.add(label4);
            four.add(text4);
            bottomPanel.add(four);
            bottomPanel.add(jButton7);

            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
        }
            
         if(flag == 8)
        {
            five.add(colourList);
            topPanel.add(five);
            topPanel.setBackground(Color.red);
        }
            if(flag == 9)
        {
            six.add(ownerList);
            topPanel.add(six);
        }
        if(flag == 10)
        {
            
            seven.add(label9);
            seven.add(text9);
            six.setVisible(false);
            bottomPanel.add(seven);
        }
        
            
        if (flag == 6)
        {

           System.exit(0);
        }
       
       
        getContentPane().add(holdAll, BorderLayout.NORTH); // places everything on the frame
        this.validate();

    }
    public void actionPerformed(ActionEvent e)
    {
        
        if (e.getSource() == jButton1)                              
        {
            
            int flag = 1;
            drawApp(flag);
        }
        if(e.getSource() == jButton2)
        {
            int flag = 0;
            drawApp(flag);
        }
        if(e.getSource() == jButton3)
        {
            int flag = 3;
            drawApp(flag);
        }
        if(e.getSource() == jButton4)
        {
            int flag = 4;
            drawApp(flag);
        }
        if(e.getSource() == jButton5)
        {
            int flag = 5;
            drawApp(flag);
        }
        if(e.getSource() == jButton6)
        {
            int flag = 6;
            drawApp(flag);
        }
        if(e.getSource() == jButton7)
        {
            int flag = 0;
            drawApp(flag);
        }
       
        JComboBox cb = (JComboBox)e.getSource();
        String petName = (String)cb.getSelectedItem();
       
        if(petName.equals("Dog"))
        {
            int flag = 8;
             drawApp(flag);
        }
        if(petName.equals("Cat"))
        {
            int flag = 9;
             drawApp(flag);
        }
     
        if(petName.equals("Female"))
        {
            text9.setText(petName);
            int flag = 10;
            drawApp(flag);
        }     
        
    }
    
   
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                mainInterface myApplication = new mainInterface();

                myApplication.setLocation(100, 100);                  
        
                myApplication.setSize(700, 400);                       

                myApplication.setTitle("Kevin's Jukebox");

                myApplication.setVisible(true);


            }
        });
    }
}

i get the error
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox

Please include the text of the error message that shows the source line number.

What is there about the error message you don't understand? It says you are trying to cast an object to another type that is not allowed.
You can't turn a button into a combobox.

Reason this error is coming is u have button with action listener too
You simply have to use if code to enclose line 301 statement ,

if(source is equal to combo box then)

Right now even a button is pressed that code is executed which is creating a cast problem from button to combo box... Hope this help.

The issue with doing that is, I have 3 combobox lists and was running them in under line 301, by doing the if statement for e.getSource() == petList, i am now blocking ownerList and colourList

then use ||(Or Statement) and use them as well.....

for JComboBox you have to change Listener from ActionListener to the ItemListener, I know that official tuturial descibed ActionListener,

if you want to make some changes to inside, back to the JComboBox then look for ActionListener, for output from JComboBox to the GUI is almost better ItemListener

Another solution is to have a separate listener for the combo boxes.
Or use the instanceOf statement to detect if the object is a combo box.

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.