Removing the Preview Panel from a JColorChooser Dialog - Java Swing

Java examples for Swing:JColorChooser

Description

Removing the Preview Panel from a JColorChooser Dialog

Demo Code



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {
  public static void main(String[] argv) {
    JColorChooser chooser = new JColorChooser();
    chooser.setPreviewPanel(new JPanel());
  }/*from   ww  w  .  j  a v a 2 s. co m*/
}

class MyPreviewPanel extends JComponent {
  Color curColor;

  public MyPreviewPanel(JColorChooser chooser) {
    curColor = chooser.getColor();

    ColorSelectionModel model = chooser.getSelectionModel();
    model.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        ColorSelectionModel model = (ColorSelectionModel) evt.getSource();
        curColor = model.getSelectedColor();
      }
    });

    // Set a preferred size
    setPreferredSize(new Dimension(50, 50));
  }

  // Paint current color
  public void paint(Graphics g) {
    g.setColor(curColor);
    g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
  }
}

Related Tutorials