4 import java.awt.Component;
5 import java.awt.event.ActionListener;
7 import javax.swing.JColorChooser;
8 import javax.swing.JComponent;
9 import javax.swing.JDialog;
12 * A helper class that shows a JColorChooser and passes the selected colour back
15 public class JalviewColourChooser
17 public interface ColourChooserListener
19 void colourSelected(Color c);
21 default void cancel() {};
25 * Shows a colour chooser dialog with the given parent component, title, and
26 * (optionally) initially selected colour. The chosen colour is passed back to
27 * the listener. There is no action if the dialog is cancelled.
31 * @param initialColour
34 public static void showColourChooser(Component parent, String title,
35 Color initialColour, ColourChooserListener listener)
37 JColorChooser colorChooser = new JColorChooser();
38 if (initialColour != null)
40 colorChooser.setColor(initialColour);
42 ActionListener onChoose = evt -> listener.colourSelected(colorChooser.getColor());
43 ActionListener onCancel = evt -> listener.cancel();
44 JDialog dialog = JColorChooser.createDialog(parent, title, true, colorChooser,
46 dialog.setVisible(true);
50 * A convenience method that shows a colour chooser, with initial colour the
51 * background of the given 'paintable', and updates its background colour and
52 * repaints it after a colour selection is made
58 public static void showColourChooser(Component parent, String title,
61 ColourChooserListener listener = new ColourChooserListener()
64 public void colourSelected(Color c)
66 paintable.setBackground(c);
70 JalviewColourChooser.showColourChooser(parent, title,
71 paintable.getBackground(), listener);