title: How can I get my Color Icon to display on my GUI?
body: <p>I'm trying to create a GUI where I can create custom colors. I have a feature that lets the user preview the color before submitting the color. I can't get the Icon to display at all. The actual problem is in my ColorFrame class. The color Icon(a JLabel, color) is created in the actionperformed() method.</p>
  
  <p>The code that creates the Icon(The ButtonListener class):</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  import java.awt.Label;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.BoxLayout;
  import javax.swing.Icon;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  
  public class ColorFrame extends JFrame implements ActionListener {
  
  private JPanel colorPanel;
  private JPanel labelPanel;
  private JPanel buttonPanel;
  
  private JLabel labelRed;
  private JLabel labelGreen;
  private JLabel labelBlue;
  private JLabel color;
  
  private JTextField redField;
  private JTextField greenField;
  private JTextField blueField;
  
  private JButton preview;
  private JButton submit;
  
  
  private int redInt;
  private int blueInt;
  private int greenInt;
  
  
  private int width = 30;
  private int height = 30;
  
  public ColorFrame(int x, int y)
  {
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes all frames for some reason
      this.pack();
      this.setLocation(x, y);
  
      buttonPanel = new JPanel();
      this.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
      preview = new JButton("Preview Color");
      preview.addActionListener(new ButtonListener());
      buttonPanel.add(preview);
      createColorPanel();
      createLabelPanel();
  
      this.setVisible(true);
  
  }
  
  class ButtonListener implements ActionListener{
  
      @Override
      public void actionPerformed(ActionEvent arg0) {
          //in the gui, there are three jtextfieilds that represent color values. the first is red, the second is green, and the last is blue.
          redInt = Integer.parseInt(redField.getText());
          greenInt = Integer.parseInt(greenField.getText());
          blueInt = Integer.parseInt(blueField.getText());
  
          color = new JLabel(new ColorIcon(redInt,greenInt,blueInt));
          buttonPanel.add(color,BorderLayout.SOUTH);
          print(redInt,greenInt, blueInt);
  
      }
  
  }
  
  
  private void createColorPanel()
  {
      colorPanel = new JPanel(new GridBagLayout());
  
      GridBagConstraints c = new GridBagConstraints();
  
      //creates the three textfields to input rgb values.
      //the first is r, second, b third g
      redField = new JTextField(2);
      c.fill = GridBagConstraints.VERTICAL;
      c.weightx = 0.5;
      c.gridx = 1;
      c.gridy = 0;
      c.insets = new Insets(10,0,0,10);
      colorPanel.add(redField,c);
  
      greenField = new JTextField(2);
      c.fill = GridBagConstraints.VERTICAL;
      c.weightx = 0.0;
      c.gridx = 1;
      c.gridy = 1;
      colorPanel.add(greenField,c);
  
      blueField = new JTextField(2);
      c.fill = GridBagConstraints.VERTICAL;
      c.weightx = 0.5;
      c.gridx = 1;
      c.gridy = 2;
      colorPanel.add(blueField,c);
  
      redField.addActionListener(this);
      greenField.addActionListener(this);
      blueField.addActionListener(this);
  
  
  
      this.add(colorPanel, BorderLayout.EAST);
  }
  
  
  public void actionPerformed(ActionEvent arg0) {
      redInt = Integer.parseInt(redField.getText());
      greenInt = Integer.parseInt(greenField.getText());
      blueInt = Integer.parseInt(blueField.getText());
  
      // creates the color icon. It works sometimes, but not every time.
      color = new JLabel(new ColorIcon(redInt,greenInt,blueInt));
      buttonPanel.add(color);
      print(redInt,greenInt, blueInt);
  
  }
  
  
  private void print(int a, int b, int c)
  {
      // just to see if the actionperformed() method works.
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
  }
  
  
  public Dimension getPreferredSize() {
      return new Dimension(300,300);
  }
  
  
  public static void main(String[] args)
  {
      ColorFrame frame = new ColorFrame(200,200); 
  }
  
  
  }
  </code></pre>
  
  <p>Color Icon class:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Component;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.geom.Rectangle2D;
  
  import javax.swing.Icon;
  
  
  public class ColorIcon implements Icon{
  
  
  
  private final int size = 30;
  private int red;
  private int green;
  private int blue;
  
  
  
  public ColorIcon(int r, int g, int b)
  {
      this.red = r;
      this.green = g;
      this.blue = b;
  }
  
  
  @Override
  public int getIconHeight() {
      // TODO Auto-generated method stub
      return size;
  }
  
  @Override
  public int getIconWidth() {
      // TODO Auto-generated method stub
      return size;
  }
  
  @Override
  public void paintIcon(Component c, Graphics g, int x, int y) {
      // TODO Auto-generated method stub
  
      Graphics2D g2 = (Graphics2D) g;
      Rectangle2D.Double square = new Rectangle2D.Double(50, 50, size, size);
      g2.setColor(new Color(red,green,blue));
      g2.fill(square);
  }
  
  }
  </code></pre>