3 import jalview.gui.JalviewColourChooser.ColourChooserListener;
6 import java.awt.Component;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
10 import javax.swing.JColorChooser;
11 import javax.swing.JComponent;
12 import javax.swing.JDialog;
13 import javax.swing.JPanel;
16 * A helper class that shows a JColorChooser and passes the selected colour back
19 public class JalviewColourChooser
21 public interface ColourChooserListener
23 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 al = new ActionListener()
47 public void actionPerformed(ActionEvent e)
49 listener.colourSelected(colorChooser.getColor());
52 JDialog dialog = JColorChooser.createDialog(parent, title, true,
53 colorChooser, al, null);
54 dialog.setVisible(true);
58 * A convenience method that shows a colour chooser, with initial colour the
59 * background of the given 'paintable', and updates its background colour and
60 * repaints it after a colour selection is made
66 public static void showColourChooser(Component parent, String title,
69 ColourChooserListener listener = new ColourChooserListener()
72 public void colourSelected(Color c)
74 paintable.setBackground(c);
78 JalviewColourChooser.showColourChooser(parent, title,
79 paintable.getBackground(), listener);