Mega Code Archive

 
Categories / Java Tutorial / Swing
 

JColorChooser with custom preview panel

To turn off the preview panel, change the previewPanel to a not-null component. When the property is set to null, the default preview panel for the look and feel is shown. Setting the property to an empty JPanel serves the purpose of not showing the preview panel. import java.awt.Font; import javax.swing.BorderFactory; import javax.swing.JColorChooser; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; public class JColorChooserWithCustomPreviewPanel {   public static void main(String[] a) {     final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);     previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));     previewLabel.setSize(previewLabel.getPreferredSize());     previewLabel.setBorder(BorderFactory.createEmptyBorder(0,0,1,0));          JColorChooser colorChooser = new JColorChooser();     colorChooser.setPreviewPanel(previewLabel);     JDialog d = colorChooser.createDialog(null,"",true,colorChooser,null,null);          d.setVisible(true);   } }