Class ColorPanel

All Implemented Interfaces:
ItemListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, ChangeListener

public class ColorPanel extends JPanel implements ItemListener, ChangeListener
Panel used by ColorDialog to present color-selection controls.

This panel embeds a simplified JColorChooser, retaining its standard swatch and RGB chooser panels, along with an MDI-specific preview area that shows:

  • an optional "No Color" checkbox,
  • the newly selected color, and
  • the original color.

In this refactored version, transparency is controlled only through the chooser's built-in alpha support. The legacy custom transparency slider has been removed.

Author:
heddle
See Also:
  • Field Details

    • minw

      protected static int minw
      The minimum width of the panel.
    • minh

      protected static int minh
      The minimum height of the panel.
    • nocolorcb

      protected JCheckBox nocolorcb
      Checkbox used to select "No Color" when that option is enabled.
    • oldcolorlabel

      protected JLabel oldcolorlabel
      Label used to preview the original color.
    • newcolorlabel

      protected JLabel newcolorlabel
      Label used to preview the currently selected color.
    • previewPanel

      protected JPanel previewPanel
      The custom preview panel shown beneath the chooser.
    • colorChooser

      protected JColorChooser colorChooser
      The embedded Swing color chooser.
  • Constructor Details

    • ColorPanel

      public ColorPanel()
      Creates a new color panel.

      The chooser is simplified to the standard swatch and RGB panels. Swatches provide convenient one-click access to common colors, while RGB provides precise component and alpha entry. The preview area is custom and includes the old/new color samples and the optional "No Color" control.

  • Method Details

    • createPreviewPanel

      protected JPanel createPreviewPanel()
      Creates the preview panel shown at the bottom of the chooser.

      The layout is intentionally close to the original MDI appearance, except that the legacy transparency slider is omitted.

      Returns:
      the preview panel
    • getMinimumSize

      public Dimension getMinimumSize()
      Returns the minimum size of the panel.
      Overrides:
      getMinimumSize in class JComponent
      Returns:
      the minimum size
    • getPreferredSize

      public Dimension getPreferredSize()
      Returns the preferred size of the panel.
      Overrides:
      getPreferredSize in class JComponent
      Returns:
      the preferred size
    • getInsets

      public Insets getInsets()
      Returns slightly expanded outer insets for the panel.
      Overrides:
      getInsets in class JComponent
      Returns:
      the panel insets
    • itemStateChanged

      public void itemStateChanged(ItemEvent e)
      Responds to changes in the "No Color" checkbox.
      Specified by:
      itemStateChanged in interface ItemListener
      Parameters:
      e - the item event
    • setNoColor

      public void setNoColor(boolean nocol)
      Sets whether "No Color" is selected.
      Parameters:
      nocol - true to select "No Color"
    • noColor

      public boolean noColor()
      Checks whether "No Color" is selected.
      Returns:
      true if "No Color" is selected
    • enableNoColor

      public void enableNoColor(boolean anc)
      Enables or disables the user's ability to choose "No Color".
      Parameters:
      anc - true to enable the "No Color" option
    • enableTransparency

      public void enableTransparency(boolean anc)
      Enables or disables transparency selection.

      Since the custom transparency slider has been removed, this method now controls transparency by normalizing the chooser's color to fully opaque when transparency is not allowed.

      Parameters:
      anc - true if transparency selection is allowed
    • setColor

      public void setColor(Color c)
      Sets the current color and updates the preview.
      Parameters:
      c - the color to set; may be null
    • stateChanged

      public void stateChanged(ChangeEvent e)
      Responds to chooser color changes.

      Any explicit chooser action clears "No Color" and updates the new-color preview.

      Specified by:
      stateChanged in interface ChangeListener
      Parameters:
      e - the change event
    • isNoColorSelected

      public boolean isNoColorSelected()
      Checks whether "No Color" is currently selected.
      Returns:
      true if "No Color" is selected
    • getColor

      public Color getColor()
      Gets the currently selected color.

      If "No Color" is selected, this method returns a fully transparent color for compatibility with earlier behavior. In normal dialog use, ColorDialog.getColor() converts that state to null.

      Returns:
      the selected color, or a fully transparent color when "No Color" is selected