Search code examples
javaswingjcomboboxlisteners

Using JComboBox to setColor of plot drawn


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

Solution

  • 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.