I have the following code where I am trying to put an icon on the redButton but what I have done is not correct and I'm not sure what to do. There is no where in my book that explains how to do this and I keep getting confused looking at website because that all say something different.

import java.awt.BorderLayout;
   
      import java.awt.Color;
   
      import java.awt.GridLayout;
   
import javax.swing.ImageIcon;
      import javax.swing.JFrame;
   
      import javax.swing.JLabel;
   
      import javax.swing.JPanel;
   
      import javax.swing.JButton;
   
      import java.awt.event.ActionListener;
  
      import java.awt.event.ActionEvent;
      
import javax.swing.Icon;
  
      public class ColorViewer extends JFrame
  
      {
  
      public ColorViewer()
  
      {
  
      colorPanel = new JPanel();
  
      add(colorPanel, BorderLayout.CENTER);
  
      createControlPanel();
  
      setSampleColor();
  
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
  
      }
  
      public void createControlPanel()
  
      {
  
      class ColorListener implements ActionListener
  
      {
  
      public void actionPerformed(ActionEvent e)
  
      {
          Object o = e.getSource();
          
          if (o.equals(redButton))
      
          {
      
          colorPanel.setBackground(Color.RED);
          }
      
          else if (o.equals(greenButton))
      
          {
      
          colorPanel.setBackground(Color.GREEN);
      
          }
     
          else
      
          colorPanel.setBackground(Color.BLUE);
  
      setSampleColor();
  
      }
  
      }
  
       
  
      ActionListener listener = new ColorListener();
      
      Icon icon = new ImageIcon("images.jpeg");
      
      redButton.setIcon(icon);  
      redButton = new JButton("Red");
  
      redButton.addActionListener(listener);
  
      greenButton = new JButton("Green");
  
      greenButton.addActionListener(listener);
   blueButton = new JButton("Blue");
  
      blueButton.addActionListener(listener);
        JPanel controlPanel = new JPanel();
  
      controlPanel.setLayout(new GridLayout(1, 3));
  
      
  
      controlPanel.add(redButton);
  
       
  
      
  
      controlPanel.add(greenButton);
  
  
      controlPanel.add(blueButton);
  
      add(controlPanel, BorderLayout.SOUTH);
  
      }
  
      public void setSampleColor()
  
      {
  
      colorPanel.repaint();
  
      }
  
  
      private JPanel colorPanel;
  
      private JButton redButton;
  
      private JButton greenButton;
  
      private JButton blueButton;
  
      private static final int FRAME_WIDTH = 300;
  
      private static final int FRAME_HEIGHT = 400;
        }

Never mind, I jumped the gun asking for help...after trying several more things I have figured it out.

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.