1 /* 2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.swing; 27 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.io.*; 31 import java.util.*; 32 33 import javax.swing.colorchooser.*; 34 import javax.swing.plaf.ColorChooserUI; 35 import javax.accessibility.*; 36 37 import sun.swing.SwingUtilities2; 38 39 40 /** 41 * <code>JColorChooser</code> provides a pane of controls designed to allow 42 * a user to manipulate and select a color. 43 * For information about using color choosers, see 44 * <a 45 href="http://java.sun.com/docs/books/tutorial/uiswing/components/colorchooser.html">How to Use Color Choosers</a>, 46 * a section in <em>The Java Tutorial</em>. 47 * 48 * <p> 49 * 50 * This class provides three levels of API: 51 * <ol> 52 * <li>A static convenience method which shows a modal color-chooser 53 * dialog and returns the color selected by the user. 54 * <li>A static convenience method for creating a color-chooser dialog 55 * where <code>ActionListeners</code> can be specified to be invoked when 56 * the user presses one of the dialog buttons. 57 * <li>The ability to create instances of <code>JColorChooser</code> panes 58 * directly (within any container). <code>PropertyChange</code> listeners 59 * can be added to detect when the current "color" property changes. 60 * </ol> 61 * <p> 62 * <strong>Warning:</strong> Swing is not thread safe. For more 63 * information see <a 64 * href="package-summary.html#threading">Swing's Threading 65 * Policy</a>. 66 * <p> 67 * <strong>Warning:</strong> 68 * Serialized objects of this class will not be compatible with 69 * future Swing releases. The current serialization support is 70 * appropriate for short term storage or RMI between applications running 71 * the same version of Swing. As of 1.4, support for long term storage 72 * of all JavaBeans<sup><font size="-2">TM</font></sup> 73 * has been added to the <code>java.beans</code> package. 74 * Please see {@link java.beans.XMLEncoder}. 75 * 76 * 77 * @beaninfo 78 * attribute: isContainer false 79 * description: A component that supports selecting a Color. 80 * 81 * 82 * @author James Gosling 83 * @author Amy Fowler 84 * @author Steve Wilson 85 */ 86 public class JColorChooser extends JComponent implements Accessible { 87 88 /** 89 * @see #getUIClassID 90 * @see #readObject 91 */ 92 private static final String uiClassID = "ColorChooserUI"; 93 94 private ColorSelectionModel selectionModel; 95 96 private JComponent previewPanel = ColorChooserComponentFactory.getPreviewPanel(); 97 98 private AbstractColorChooserPanel[] chooserPanels = new AbstractColorChooserPanel[0]; 99 100 private boolean dragEnabled; 101 102 /** 103 * The selection model property name. 104 */ 105 public static final String SELECTION_MODEL_PROPERTY = "selectionModel"; 106 107 /** 108 * The preview panel property name. 109 */ 110 public static final String PREVIEW_PANEL_PROPERTY = "previewPanel"; 111 112 /** 113 * The chooserPanel array property name. 114 */ 115 public static final String CHOOSER_PANELS_PROPERTY = "chooserPanels"; 116 117 118 /** 119 * Shows a modal color-chooser dialog and blocks until the 120 * dialog is hidden. If the user presses the "OK" button, then 121 * this method hides/disposes the dialog and returns the selected color. 122 * If the user presses the "Cancel" button or closes the dialog without 123 * pressing "OK", then this method hides/disposes the dialog and returns 124 * <code>null</code>. 125 * 126 * @param component the parent <code>Component</code> for the dialog 127 * @param title the String containing the dialog's title 128 * @param initialColor the initial Color set when the color-chooser is shown 129 * @return the selected color or <code>null</code> if the user opted out 130 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 131 * returns true. 132 * @see java.awt.GraphicsEnvironment#isHeadless 133 */ 134 public static Color showDialog(Component component, 135 String title, Color initialColor) throws HeadlessException { 136 137 final JColorChooser pane = new JColorChooser(initialColor != null? 138 initialColor : Color.white); 139 140 ColorTracker ok = new ColorTracker(pane); 141 JDialog dialog = createDialog(component, title, true, pane, ok, null); 142 143 dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose()); 144 145 dialog.show(); // blocks until user brings dialog down... 146 147 return ok.getColor(); 148 } 149 150 151 /** 152 * Creates and returns a new dialog containing the specified 153 * <code>ColorChooser</code> pane along with "OK", "Cancel", and "Reset" 154 * buttons. If the "OK" or "Cancel" buttons are pressed, the dialog is 155 * automatically hidden (but not disposed). If the "Reset" 156 * button is pressed, the color-chooser's color will be reset to the 157 * color which was set the last time <code>show</code> was invoked on the 158 * dialog and the dialog will remain showing. 159 * 160 * @param c the parent component for the dialog 161 * @param title the title for the dialog 162 * @param modal a boolean. When true, the remainder of the program 163 * is inactive until the dialog is closed. 164 * @param chooserPane the color-chooser to be placed inside the dialog 165 * @param okListener the ActionListener invoked when "OK" is pressed 166 * @param cancelListener the ActionListener invoked when "Cancel" is pressed 167 * @return a new dialog containing the color-chooser pane 168 * @exception HeadlessException if GraphicsEnvironment.isHeadless() 169 * returns true. 170 * @see java.awt.GraphicsEnvironment#isHeadless 171 */ 172 public static JDialog createDialog(Component c, String title, boolean modal, 173 JColorChooser chooserPane, ActionListener okListener, 174 ActionListener cancelListener) throws HeadlessException { 175 176 Window window = JOptionPane.getWindowForComponent(c); 177 ColorChooserDialog dialog; 178 if (window instanceof Frame) { 179 dialog = new ColorChooserDialog((Frame)window, title, modal, c, chooserPane, 180 okListener, cancelListener); 181 } else { 182 dialog = new ColorChooserDialog((Dialog)window, title, modal, c, chooserPane, 183 okListener, cancelListener); 184 } 185 dialog.getAccessibleContext().setAccessibleDescription(title); 186 return dialog; 187 } 188 189 /** 190 * Creates a color chooser pane with an initial color of white. 191 */ 192 public JColorChooser() { 193 this(Color.white); 194 } 195 196 /** 197 * Creates a color chooser pane with the specified initial color. 198 * 199 * @param initialColor the initial color set in the chooser 200 */ 201 public JColorChooser(Color initialColor) { 202 this( new DefaultColorSelectionModel(initialColor) ); 203 204 } 205 206 /** 207 * Creates a color chooser pane with the specified 208 * <code>ColorSelectionModel</code>. 209 * 210 * @param model the <code>ColorSelectionModel</code> to be used 211 */ 212 public JColorChooser(ColorSelectionModel model) { 213 selectionModel = model; 214 updateUI(); 215 dragEnabled = false; 216 } 217 218 /** 219 * Returns the L&F object that renders this component. 220 * 221 * @return the <code>ColorChooserUI</code> object that renders 222 * this component 223 */ 224 public ColorChooserUI getUI() { 225 return (ColorChooserUI)ui; 226 } 227 228 /** 229 * Sets the L&F object that renders this component. 230 * 231 * @param ui the <code>ColorChooserUI</code> L&F object 232 * @see UIDefaults#getUI 233 * 234 * @beaninfo 235 * bound: true 236 * hidden: true 237 * description: The UI object that implements the color chooser's LookAndFeel. 238 */ 239 public void setUI(ColorChooserUI ui) { 240 super.setUI(ui); 241 } 242 243 /** 244 * Notification from the <code>UIManager</code> that the L&F has changed. 245 * Replaces the current UI object with the latest version from the 246 * <code>UIManager</code>. 247 * 248 * @see JComponent#updateUI 249 */ 250 public void updateUI() { 251 setUI((ColorChooserUI)UIManager.getUI(this)); 252 } 253 254 /** 255 * Returns the name of the L&F class that renders this component. 256 * 257 * @return the string "ColorChooserUI" 258 * @see JComponent#getUIClassID 259 * @see UIDefaults#getUI 260 */ 261 public String getUIClassID() { 262 return uiClassID; 263 } 264 265 /** 266 * Gets the current color value from the color chooser. 267 * By default, this delegates to the model. 268 * 269 * @return the current color value of the color chooser 270 */ 271 public Color getColor() { 272 return selectionModel.getSelectedColor(); 273 } 274 275 /** 276 * Sets the current color of the color chooser to the specified color. 277 * The <code>ColorSelectionModel</code> will fire a <code>ChangeEvent</code> 278 * @param color the color to be set in the color chooser 279 * @see JComponent#addPropertyChangeListener 280 * 281 * @beaninfo 282 * bound: false 283 * hidden: false 284 * description: The current color the chooser is to display. 285 */ 286 public void setColor(Color color) { 287 selectionModel.setSelectedColor(color); 288 289 } 290 291 /** 292 * Sets the current color of the color chooser to the 293 * specified RGB color. Note that the values of red, green, 294 * and blue should be between the numbers 0 and 255, inclusive. 295 * 296 * @param r an int specifying the amount of Red 297 * @param g an int specifying the amount of Green 298 * @param b an int specifying the amount of Blue 299 * @exception IllegalArgumentException if r,g,b values are out of range 300 * @see java.awt.Color 301 */ 302 public void setColor(int r, int g, int b) { 303 setColor(new Color(r,g,b)); 304 } 305 306 /** 307 * Sets the current color of the color chooser to the 308 * specified color. 309 * 310 * @param c an integer value that sets the current color in the chooser 311 * where the low-order 8 bits specify the Blue value, 312 * the next 8 bits specify the Green value, and the 8 bits 313 * above that specify the Red value. 314 */ 315 public void setColor(int c) { 316 setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF); 317 } 318 319 /** 320 * Sets the <code>dragEnabled</code> property, 321 * which must be <code>true</code> to enable 322 * automatic drag handling (the first part of drag and drop) 323 * on this component. 324 * The <code>transferHandler</code> property needs to be set 325 * to a non-<code>null</code> value for the drag to do 326 * anything. The default value of the <code>dragEnabled</code> 327 * property 328 * is <code>false</code>. 329 * 330 * <p> 331 * 332 * When automatic drag handling is enabled, 333 * most look and feels begin a drag-and-drop operation 334 * when the user presses the mouse button over the preview panel. 335 * Some look and feels might not support automatic drag and drop; 336 * they will ignore this property. You can work around such 337 * look and feels by modifying the component 338 * to directly call the <code>exportAsDrag</code> method of a 339 * <code>TransferHandler</code>. 340 * 341 * @param b the value to set the <code>dragEnabled</code> property to 342 * @exception HeadlessException if 343 * <code>b</code> is <code>true</code> and 344 * <code>GraphicsEnvironment.isHeadless()</code> 345 * returns <code>true</code> 346 * 347 * @since 1.4 348 * 349 * @see java.awt.GraphicsEnvironment#isHeadless 350 * @see #getDragEnabled 351 * @see #setTransferHandler 352 * @see TransferHandler 353 * 354 * @beaninfo 355 * description: Determines whether automatic drag handling is enabled. 356 * bound: false 357 */ 358 public void setDragEnabled(boolean b) { 359 if (b && GraphicsEnvironment.isHeadless()) { 360 throw new HeadlessException(); 361 } 362 dragEnabled = b; 363 } 364 365 /** 366 * Gets the value of the <code>dragEnabled</code> property. 367 * 368 * @return the value of the <code>dragEnabled</code> property 369 * @see #setDragEnabled 370 * @since 1.4 371 */ 372 public boolean getDragEnabled() { 373 return dragEnabled; 374 } 375 376 /** 377 * Sets the current preview panel. 378 * This will fire a <code>PropertyChangeEvent</code> for the property 379 * named "previewPanel". 380 * 381 * @param preview the <code>JComponent</code> which displays the current color 382 * @see JComponent#addPropertyChangeListener 383 * 384 * @beaninfo 385 * bound: true 386 * hidden: true 387 * description: The UI component which displays the current color. 388 */ 389 public void setPreviewPanel(JComponent preview) { 390 391 if (previewPanel != preview) { 392 JComponent oldPreview = previewPanel; 393 previewPanel = preview; 394 firePropertyChange(JColorChooser.PREVIEW_PANEL_PROPERTY, oldPreview, preview); 395 } 396 } 397 398 /** 399 * Returns the preview panel that shows a chosen color. 400 * 401 * @return a <code>JComponent</code> object -- the preview panel 402 */ 403 public JComponent getPreviewPanel() { 404 return previewPanel; 405 } 406 407 /** 408 * Adds a color chooser panel to the color chooser. 409 * 410 * @param panel the <code>AbstractColorChooserPanel</code> to be added 411 */ 412 public void addChooserPanel( AbstractColorChooserPanel panel ) { 413 AbstractColorChooserPanel[] oldPanels = getChooserPanels(); 414 AbstractColorChooserPanel[] newPanels = new AbstractColorChooserPanel[oldPanels.length+1]; 415 System.arraycopy(oldPanels, 0, newPanels, 0, oldPanels.length); 416 newPanels[newPanels.length-1] = panel; 417 setChooserPanels(newPanels); 418 } 419 420 /** 421 * Removes the Color Panel specified. 422 * 423 * @param panel a string that specifies the panel to be removed 424 * @return the color panel 425 * @exception IllegalArgumentException if panel is not in list of 426 * known chooser panels 427 */ 428 public AbstractColorChooserPanel removeChooserPanel( AbstractColorChooserPanel panel ) { 429 430 431 int containedAt = -1; 432 433 for (int i = 0; i < chooserPanels.length; i++) { 434 if (chooserPanels[i] == panel) { 435 containedAt = i; 436 break; 437 } 438 } 439 if (containedAt == -1) { 440 throw new IllegalArgumentException("chooser panel not in this chooser"); 441 } 442 443 AbstractColorChooserPanel[] newArray = new AbstractColorChooserPanel[chooserPanels.length-1]; 444 445 if (containedAt == chooserPanels.length-1) { // at end 446 System.arraycopy(chooserPanels, 0, newArray, 0, newArray.length); 447 } 448 else if (containedAt == 0) { // at start 449 System.arraycopy(chooserPanels, 1, newArray, 0, newArray.length); 450 } 451 else { // in middle 452 System.arraycopy(chooserPanels, 0, newArray, 0, containedAt); 453 System.arraycopy(chooserPanels, containedAt+1, 454 newArray, containedAt, (chooserPanels.length - containedAt - 1)); 455 } 456 457 setChooserPanels(newArray); 458 459 return panel; 460 } 461 462 463 /** 464 * Specifies the Color Panels used to choose a color value. 465 * 466 * @param panels an array of <code>AbstractColorChooserPanel</code> 467 * objects 468 * 469 * @beaninfo 470 * bound: true 471 * hidden: true 472 * description: An array of different chooser types. 473 */ 474 public void setChooserPanels( AbstractColorChooserPanel[] panels) { 475 AbstractColorChooserPanel[] oldValue = chooserPanels; 476 chooserPanels = panels; 477 firePropertyChange(CHOOSER_PANELS_PROPERTY, oldValue, panels); 478 } 479 480 /** 481 * Returns the specified color panels. 482 * 483 * @return an array of <code>AbstractColorChooserPanel</code> objects 484 */ 485 public AbstractColorChooserPanel[] getChooserPanels() { 486 return chooserPanels; 487 } 488 489 /** 490 * Returns the data model that handles color selections. 491 * 492 * @return a <code>ColorSelectionModel</code> object 493 */ 494 public ColorSelectionModel getSelectionModel() { 495 return selectionModel; 496 } 497 498 499 /** 500 * Sets the model containing the selected color. 501 * 502 * @param newModel the new <code>ColorSelectionModel</code> object 503 * 504 * @beaninfo 505 * bound: true 506 * hidden: true 507 * description: The model which contains the currently selected color. 508 */ 509 public void setSelectionModel(ColorSelectionModel newModel ) { 510 ColorSelectionModel oldModel = selectionModel; 511 selectionModel = newModel; 512 firePropertyChange(JColorChooser.SELECTION_MODEL_PROPERTY, oldModel, newModel); 513 } 514 515 516 /** 517 * See <code>readObject</code> and <code>writeObject</code> in 518 * <code>JComponent</code> for more 519 * information about serialization in Swing. 520 */ 521 private void writeObject(ObjectOutputStream s) throws IOException { 522 s.defaultWriteObject(); 523 if (getUIClassID().equals(uiClassID)) { 524 byte count = JComponent.getWriteObjCounter(this); 525 JComponent.setWriteObjCounter(this, --count); 526 if (count == 0 && ui != null) { 527 ui.installUI(this); 528 } 529 } 530 } 531 532 533 /** 534 * Returns a string representation of this <code>JColorChooser</code>. 535 * This method 536 * is intended to be used only for debugging purposes, and the 537 * content and format of the returned string may vary between 538 * implementations. The returned string may be empty but may not 539 * be <code>null</code>. 540 * 541 * @return a string representation of this <code>JColorChooser</code> 542 */ 543 protected String paramString() { 544 StringBuffer chooserPanelsString = new StringBuffer(""); 545 for (int i=0; i<chooserPanels.length; i++) { 546 chooserPanelsString.append("[" + chooserPanels[i].toString() 547 + "]"); 548 } 549 String previewPanelString = (previewPanel != null ? 550 previewPanel.toString() : ""); 551 552 return super.paramString() + 553 ",chooserPanels=" + chooserPanelsString.toString() + 554 ",previewPanel=" + previewPanelString; 555 } 556 557 ///////////////// 558 // Accessibility support 559 //////////////// 560 561 protected AccessibleContext accessibleContext = null; 562 563 /** 564 * Gets the AccessibleContext associated with this JColorChooser. 565 * For color choosers, the AccessibleContext takes the form of an 566 * AccessibleJColorChooser. 567 * A new AccessibleJColorChooser instance is created if necessary. 568 * 569 * @return an AccessibleJColorChooser that serves as the 570 * AccessibleContext of this JColorChooser 571 */ 572 public AccessibleContext getAccessibleContext() { 573 if (accessibleContext == null) { 574 accessibleContext = new AccessibleJColorChooser(); 575 } 576 return accessibleContext; 577 } 578 579 /** 580 * This class implements accessibility support for the 581 * <code>JColorChooser</code> class. It provides an implementation of the 582 * Java Accessibility API appropriate to color chooser user-interface 583 * elements. 584 */ 585 protected class AccessibleJColorChooser extends AccessibleJComponent { 586 587 /** 588 * Get the role of this object. 589 * 590 * @return an instance of AccessibleRole describing the role of the 591 * object 592 * @see AccessibleRole 593 */ 594 public AccessibleRole getAccessibleRole() { 595 return AccessibleRole.COLOR_CHOOSER; 596 } 597 598 } // inner class AccessibleJColorChooser 599 } 600 601 602 /* 603 * Class which builds a color chooser dialog consisting of 604 * a JColorChooser with "Ok", "Cancel", and "Reset" buttons. 605 * 606 * Note: This needs to be fixed to deal with localization! 607 */ 608 class ColorChooserDialog extends JDialog { 609 private Color initialColor; 610 private JColorChooser chooserPane; 611 private JButton cancelButton; 612 613 public ColorChooserDialog(Dialog owner, String title, boolean modal, 614 Component c, JColorChooser chooserPane, 615 ActionListener okListener, ActionListener cancelListener) 616 throws HeadlessException { 617 super(owner, title, modal); 618 initColorChooserDialog(c, chooserPane, okListener, cancelListener); 619 } 620 621 public ColorChooserDialog(Frame owner, String title, boolean modal, 622 Component c, JColorChooser chooserPane, 623 ActionListener okListener, ActionListener cancelListener) 624 throws HeadlessException { 625 super(owner, title, modal); 626 initColorChooserDialog(c, chooserPane, okListener, cancelListener); 627 } 628 629 protected void initColorChooserDialog(Component c, JColorChooser chooserPane, 630 ActionListener okListener, ActionListener cancelListener) { 631 //setResizable(false); 632 633 this.chooserPane = chooserPane; 634 635 Locale locale = getLocale(); 636 String okString = UIManager.getString("ColorChooser.okText", locale); 637 String cancelString = UIManager.getString("ColorChooser.cancelText", locale); 638 String resetString = UIManager.getString("ColorChooser.resetText", locale); 639 640 Container contentPane = getContentPane(); 641 contentPane.setLayout(new BorderLayout()); 642 contentPane.add(chooserPane, BorderLayout.CENTER); 643 644 /* 645 * Create Lower button panel 646 */ 647 JPanel buttonPane = new JPanel(); 648 buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER)); 649 JButton okButton = new JButton(okString); 650 getRootPane().setDefaultButton(okButton); 651 okButton.getAccessibleContext().setAccessibleDescription(okString); 652 okButton.setActionCommand("OK"); 653 okButton.addActionListener(new ActionListener() { 654 public void actionPerformed(ActionEvent e) { 655 hide(); 656 } 657 }); 658 if (okListener != null) { 659 okButton.addActionListener(okListener); 660 } 661 buttonPane.add(okButton); 662 663 cancelButton = new JButton(cancelString); 664 cancelButton.getAccessibleContext().setAccessibleDescription(cancelString); 665 666 // The following few lines are used to register esc to close the dialog 667 Action cancelKeyAction = new AbstractAction() { 668 public void actionPerformed(ActionEvent e) { 669 ((AbstractButton)e.getSource()).fireActionPerformed(e); 670 } 671 }; 672 KeyStroke cancelKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); 673 InputMap inputMap = cancelButton.getInputMap(JComponent. 674 WHEN_IN_FOCUSED_WINDOW); 675 ActionMap actionMap = cancelButton.getActionMap(); 676 if (inputMap != null && actionMap != null) { 677 inputMap.put(cancelKeyStroke, "cancel"); 678 actionMap.put("cancel", cancelKeyAction); 679 } 680 // end esc handling 681 682 cancelButton.setActionCommand("cancel"); 683 cancelButton.addActionListener(new ActionListener() { 684 public void actionPerformed(ActionEvent e) { 685 hide(); 686 } 687 }); 688 if (cancelListener != null) { 689 cancelButton.addActionListener(cancelListener); 690 } 691 buttonPane.add(cancelButton); 692 693 JButton resetButton = new JButton(resetString); 694 resetButton.getAccessibleContext().setAccessibleDescription(resetString); 695 resetButton.addActionListener(new ActionListener() { 696 public void actionPerformed(ActionEvent e) { 697 reset(); 698 } 699 }); 700 int mnemonic = SwingUtilities2.getUIDefaultsInt("ColorChooser.resetMnemonic", locale, -1); 701 if (mnemonic != -1) { 702 resetButton.setMnemonic(mnemonic); 703 } 704 buttonPane.add(resetButton); 705 contentPane.add(buttonPane, BorderLayout.SOUTH); 706 707 if (JDialog.isDefaultLookAndFeelDecorated()) { 708 boolean supportsWindowDecorations = 709 UIManager.getLookAndFeel().getSupportsWindowDecorations(); 710 if (supportsWindowDecorations) { 711 getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG); 712 } 713 } 714 applyComponentOrientation(((c == null) ? getRootPane() : c).getComponentOrientation()); 715 716 pack(); 717 setLocationRelativeTo(c); 718 719 this.addWindowListener(new Closer()); 720 } 721 722 public void show() { 723 initialColor = chooserPane.getColor(); 724 super.show(); 725 } 726 727 public void reset() { 728 chooserPane.setColor(initialColor); 729 } 730 731 class Closer extends WindowAdapter implements Serializable{ 732 public void windowClosing(WindowEvent e) { 733 cancelButton.doClick(0); 734 Window w = e.getWindow(); 735 w.hide(); 736 } 737 } 738 739 static class DisposeOnClose extends ComponentAdapter implements Serializable{ 740 public void componentHidden(ComponentEvent e) { 741 Window w = (Window)e.getComponent(); 742 w.dispose(); 743 } 744 } 745 746 } 747 748 class ColorTracker implements ActionListener, Serializable { 749 JColorChooser chooser; 750 Color color; 751 752 public ColorTracker(JColorChooser c) { 753 chooser = c; 754 } 755 756 public void actionPerformed(ActionEvent e) { 757 color = chooser.getColor(); 758 } 759 760 public Color getColor() { 761 return color; 762 } 763 }