Hi all, I cant get my brain around the logic of making a check box add a specific color when it is checked. Also, when the other boxes are check, it will add that color to the selected color as well. here is my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class ColorViewerFrame extends JFrame
{  
   public ColorViewerFrame()
   {  
      colorPanel = new JPanel();
      
      add(colorPanel, BorderLayout.CENTER);
      createControlPanel();
      //setSampleColor();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   public void createControlPanel()
   {
      class ColorListener implements ChangeListener
      {  
         public void stateChanged(ChangeEvent event)
         {  
            //setSampleColor();
         }
      }   

      ChangeListener listener = new ColorListener();

      redCheckBox = new JCheckBox("Red");
      redCheckBox.addChangeListener(listener);

      greenCheckBox = new JCheckBox("Green");
      greenCheckBox.addChangeListener(listener);

      blueCheckBox = new JCheckBox("Blue");
      blueCheckBox.addChangeListener(listener);
      
      JPanel controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      controlPanel.add(redCheckBox);
      controlPanel.add(greenCheckBox);
      controlPanel.add(blueCheckBox);

      add(controlPanel, BorderLayout.SOUTH);      
   }
   
   /**
      Reads the check box values and sets the panel to
      the selected color.
    
   public void setSampleColor()
   {  
      // Read CheckBox values
   
      if(redCheckBox.isSelected())
    	  colorPanel.setBackground(255, 0, 0);
      if(redCheckBox.isSelected() ++ greenCheckBox.isSelected())
    	  colorPanel.setBackground(255, 225, 0);
      if(redCheckBox.isSelected() ++ blueCheckBox.isSelected())
    	  colorPanel.setBackground(255, 0, 255);
      if(greenCheckBox.isSelected())
    	  colorPanel.setBackground(0, 255, 0);
      
      if(blueCheckBox.isSelected())
    	  colorPanel.setBackground(0, 0, 255);

      // Set panel background to selected color
      
      //colorPanel.setBackground(0, 0, 0);
      colorPanel.repaint();   
   }
   */
   private JPanel colorPanel;
   private JCheckBox redCheckBox;
   private JCheckBox greenCheckBox;
   private JCheckBox blueCheckBox;
import javax.swing.JFrame;

// new class
public class ColorViewer
{  
   public static void main(String[] args)
   {  
      ColorViewerFrame frame = new ColorViewerFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

any help would be super!
Cheers!

well two things come to mind, you have a listener and i think that means you need to have your class implement it. i.e.

public class MyClass implements ActionListener {

you can also use your extend as well. if i remember right extend is inheritance and implement is an interface it's using.

once you do this i guess you can use the action event method, which is called when an ation happens to the thing you implemented, i.e checking.

public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {

this is a code example above from: http://java.sun.com/docs/books/tutorial/uiswing/components/button.html.

i think "disable" would be the title next to the checkbox. so you change that to "blue" and they check blue and your blue variable goes to 1, it happens again it goes to 0.

Mike

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.