2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.event.ActionListener;
27 import javax.swing.JColorChooser;
28 import javax.swing.JComponent;
29 import javax.swing.JDialog;
32 * A helper class that shows a JColorChooser and passes the selected colour back
35 public class JalviewColourChooser
37 public interface ColourChooserListener
39 void colourSelected(Color c);
47 * Shows a colour chooser dialog with the given parent component, title, and
48 * (optionally) initially selected colour. The chosen colour is passed back to
49 * the listener. There is no action if the dialog is cancelled.
53 * @param initialColour
56 public static void showColourChooser(Component parent, String title,
57 Color initialColour, ColourChooserListener listener)
59 JColorChooser colorChooser = new JColorChooser();
60 if (initialColour != null)
62 colorChooser.setColor(initialColour);
64 ActionListener onChoose = evt -> listener
65 .colourSelected(colorChooser.getColor());
66 ActionListener onCancel = evt -> listener.cancel();
67 JDialog dialog = JColorChooser.createDialog(parent, title, true,
68 colorChooser, onChoose, onCancel);
69 dialog.setVisible(true);
73 * A convenience method that shows a colour chooser, with initial colour the
74 * background of the given 'paintable', and updates its background colour and
75 * repaints it after a colour selection is made
81 public static void showColourChooser(Component parent, String title,
84 ColourChooserListener listener = new ColourChooserListener()
87 public void colourSelected(Color c)
89 paintable.setBackground(c);
93 JalviewColourChooser.showColourChooser(parent, title,
94 paintable.getBackground(), listener);