Class ColorChooserPanel
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JPanel
-
- com.coswald.jtalker.gui.ColorChooserPanel
-
- All Implemented Interfaces:
Colorizer,Initializable,ImageObserver,MenuContainer,Serializable,Accessible
public class ColorChooserPanel extends JPanel implements Initializable, Colorizer
A panel that allows the selection of one color. This is an redesign of the
JColorChooser. This isn't because theJColorChooseris flawed; in fact, we re-use much of the design of theJColorChooser. We just felt that only one color system needed to be present, and that there was no way to visually detect what color had been selected. We fixed this by only showing one color system at a time, and by removing a select few parts of theJColorChooser. The parts we removed from the chooser were the label and text field that allowed you to manually enter a specific value. We felt this busied the screen, and was unnecessary. The main purpose of this GUI is to get aColor, and we do so via thegetColormethod. However, we also allow the addition ofColorUpdaters, which will automatically update the color of the givenColorizerwhen the underlyingJColorChoosers color has been updated. Note: For some reason, you must add this component as the main component of the UI. This is not a huge deal, but it will not display unless it is positioned atBorderLayout.CENTERor is a main component of aGridBagLayout. When you display the panel, it will look like this:
- Since:
- JTalker 0.1.5
- Version:
- 0.0.1
- Author:
- C. William Oswald
- See Also:
JColorChooser, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classColorChooserPanel.ColorUpdaterUpdates any color using the underlyingchooser'scurrent color.-
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
Fields Modifier and Type Field Description protected JColorChoosercolorChooserTheJColorChooserused to get theColor.-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
Constructor Summary
Constructors Constructor Description ColorChooserPanel()Constructs aColorChooerPanelwith the default color system.ColorChooserPanel(int colorSystem)Constructs aColorChooserPanelwith the color system.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddChangeListener(ChangeListener cl)Adds a change listener to the underlyingJColorChooser.voidaddColorUpdater(Colorizer colorizer)Adds aColorUpdaterto thisColorChooserPanel.ColorgetColor()Returns the chosen color.voidinit()Adds all of the graphical and utility components to this panel.voidremoveChangeListener(ChangeListener cl)Removes a change listener to the underlyingJColorChooser.voidsetColor(Color c)Sets theColorof the underlyingJColorChooser.-
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
-
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
-
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Field Detail
-
colorChooser
protected JColorChooser colorChooser
TheJColorChooserused to get theColor. Note that this panel never actually displays the underlyingJColorChooser; we only use its components within this panel.
-
-
Constructor Detail
-
ColorChooserPanel
public ColorChooserPanel(int colorSystem)
Constructs aColorChooserPanelwith the color system. Note that this does not complete the construction; theinitmethod must be called.- Parameters:
colorSystem- The coloring system to use when selecting a color.- See Also:
GUIConstants.COLOR_SYSTEM
-
ColorChooserPanel
public ColorChooserPanel()
Constructs aColorChooerPanelwith the default color system. This is currently set to 3. See that class for the possible values and what they mean.
-
-
Method Detail
-
init
public void init()
Adds all of the graphical and utility components to this panel. This will add all of the graphical components, as well as the ability for the color up top to change when the underlyingJColorChooserchanges its color. A big thanks to stack overflow for this question; I have since lost the link to the code (we have heavily modified it since). We are very proud of the fact that this class just redraws aJColorChooserthe way we want to; however, in order to do so, we had to make sure the systems we used had a compatible way to do so. This is why the zeroeth version of the panels returned byJColorChooserspanelsmethod (which allows this hack to work) is not included in the given color systems; it is just too different from the other panels. Due to the fact that each panels components are drawn in the same manner, we are allowed to extract the data from thoseAbstractColorChooserPanelusing theContainer.getComponents()method. This will also set the width of the slider to 25. That was the only GUI element which needed a size set; everything else is already defined by theJColorChooser.- Specified by:
initin interfaceInitializable
-
setColor
public void setColor(Color c)
Sets theColorof the underlyingJColorChooser. This is a wrapper method for thesetColormethod of theJColorChooserclass.- Specified by:
setColorin interfaceColorizer- Parameters:
c- The new color.- See Also:
JColorChooser.setColor(Color)
-
addChangeListener
public void addChangeListener(ChangeListener cl)
Adds a change listener to the underlyingJColorChooser.- Parameters:
cl- The new change listener.- See Also:
ColorSelectionModel.addChangeListener(ChangeListener)
-
addColorUpdater
public void addColorUpdater(Colorizer colorizer)
Adds aColorUpdaterto thisColorChooserPanel. This is just a way to avoid ugly code like this:
ccp.addChangeListener(ccp.new ColorUpdater(ssp::setColor));- Parameters:
colorizer- The colorizer make aColorUpdaterout of.
-
removeChangeListener
public void removeChangeListener(ChangeListener cl)
Removes a change listener to the underlyingJColorChooser.- Parameters:
cl- The old change listener.- See Also:
ColorSelectionModel.removeChangeListener(ChangeListener)
-
getColor
public Color getColor()
Returns the chosen color.- Returns:
- The chosen color.
-
-