Changing the preview panel in a JColorChooser

Use the method setPreviewPanel to set your custom preview panel. Setting it to new JPanel() will remove the preview panel as a panel by default has no size. This example will create a new JPanel, set the size, and use it as the preview panel.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("JColorChooser Demonstration");
 
      getContentPane().setLayout(new FlowLayout()); 
      final JPanel panel = new JPanel();
      panel.setPreferredSize(new Dimension(100, 30));
      final JColorChooser colorChooser = new JColorChooser();
 
      colorChooser.setPreviewPanel(panel);
 
      getContentPane().add(colorChooser);
 
      colorChooser.getSelectionModel().addChangeListener(
         new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
               Color newColor = colorChooser.getColor();
               panel.setBackground(newColor);
            }
         }    
      );
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}