Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6354181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:32:09+00:00 2026-05-24T22:32:09+00:00

I have created a simulation that performs a number of calculations and then stores

  • 0

I have created a simulation that performs a number of calculations and then stores a point consisting of an x and y coordinate into a Point Array List.

Then I have a for loop that iterates through each point and draws that point onto the GUI.
Here is my for loop that executes at the end of the simulation and the drawPoint method:

//Iterates through each point in Point Array List
for(Point i: PointArray)
 {
      drawPoint(g, i, black); //Draw Point
 }

//Draws point onto panel
public void drawPoint(Graphics g, Point PointArray, Color color)
{
     Graphics2D g2d = (Graphics2D)g;
     g2d.setStroke(new BasicStroke(2f));
     g.setColor(color); //g2d.setColor(Color.black); 
     g2d.drawOval((int)PointArray.a, (int)PointArray.b, 2, 2);
} 

I want to implement a JComboBox so that the user can specify what color they want the plot to be colored when drawn. I have created different color objects to be used for this.

In my actionPerformed method, I also have code that handles the JButton events that start, stop and erase the simulation.
This is what I have for my actionPerformed method:

 public void actionPerformed(ActionEvent e) 
 {
       Object source = e.getSource(); 

       JComboBox cb = (JComboBox)e.getSource();
       String colorName = (String)cb.getSelectedItem();

       //Get Graphics on Drawing Panel
       Graphics g = dPanel.getGraphics();

       //if JButton source == start, do something

       //if JButton source == stop, do something

       //If JButton source == erase, do something

       if(colorName == "Default")
       {
            g.setColor(black); 
       }

       if(colorName == "Red")
       {
            g.setColor(startColor); 
       }

       if(colorName == "Green")
       {
            g.setColor(forestGreen); 
       }
 }

I am getting the following error:
Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JComboBox
at SimulationGUI.actionPerformed(SimulationGUI.java:332)

So my question is, is what I want to do even possible, and if it is (being that my implementation does not work), what would be a way to accomplish this?

EDIT:

Here is my new action listener for the JComboBox:

    colorBox.addActionListener(new ActionListener()               
    {                                                         
        public void actionPerformed(ActionEvent e)               
        {                                                        
                 JComboBox cb = (JComboBox)e.getSource();
                 String colorName = (String)cb.getSelectedItem();

            Graphics g = dPanel.getGraphics();

            if(colorName.equals("Default"))
            {
                g.setColor(black);
            }

            if(colorName.equals("Red"))
            {
                g.setColor(startColor);
            }

            if(colorName.equals("Green"))
            {
                g.setColor(forestGreen);
            }

            if(colorName.equals("Blue"))
            {
                g.setColor(eraseColor);
            }                    
        }                                                        
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:32:10+00:00Added an answer on May 24, 2026 at 10:32 pm

    Use separate ActionListeners for the ComboBox and your buttons. Right now your main problem is you are casting here expecting a JComboBox:

    JComboBox cb = (JComboBox)e.getSource();
    

    But this will fail when the button is clicked because a JButton is not a JComboBox. Doing this cast would be fine if it was in an ActionListener only dealing with JComboBoxes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this problem: I have created a bash script that performs some tasks.
I have a third party library that I'm attempting to incorporate into a simulation.
The Situation (Ignore this it is boring): I have reports that I created using
I have a simulation that reads large binary data files that we create (10s
I have a class (simulation) which creates an instance of another class (GUI). Inside
this is my situation: I have two org.w3c.dom.Document created from two xml files. What
I have a situation in BIRT reporting. A report I created is insisting on
Okay, here's the situation: We have a table of about 50 columns (created by
I have created the following C library for reading an image: typedef struct {
I have a Spring/JPA/Groovy/Hibernate stack, (note: not grails ), and I'm finding that I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.