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);
27 * Shows a colour chooser dialog with the given parent component, title, and
28 * (optionally) initially selected colour. The chosen colour is passed back to
29 * the listener. There is no action if the dialog is cancelled.
33 * @param initialColour
36 public static void showColourChooser(Component parent, String title,
37 Color initialColour, ColourChooserListener listener)
39 JColorChooser colorChooser = new JColorChooser();
40 if (initialColour != null)
42 colorChooser.setColor(initialColour);
44 ActionListener onChoose = evt -> listener
45 .colourSelected(colorChooser.getColor());
46 ActionListener onCancel = evt -> listener.cancel();
47 JDialog dialog = JColorChooser.createDialog(parent, title, true,
48 colorChooser, onChoose, onCancel);
49 dialog.setVisible(true);
53 * A convenience method that shows a colour chooser, with initial colour the
54 * background of the given 'paintable', and updates its background colour and
55 * repaints it after a colour selection is made
61 public static void showColourChooser(Component parent, String title,
64 ColourChooserListener listener = new ColourChooserListener()
67 public void colourSelected(Color c)
69 paintable.setBackground(c);
73 JalviewColourChooser.showColourChooser(parent, title,
74 paintable.getBackground(), listener);