Java Code Examples for javax.swing.JColorChooser

The following examples show how to use javax.swing.JColorChooser. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jdk8u-jdk   Source File: Test4759934.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (CMD_DIALOG.equals(command)) {
        JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
        dialog.setLocation(200, 0);
        show(dialog, CMD_CHOOSER);
    }
    else if (CMD_CHOOSER.equals(command)) {
        Object source = event.getSource();
        Component component = (source instanceof Component)
                ? (Component) source
                : null;

        JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
    }
}
 
Example 2
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            JColorChooser colorChooser = new JColorChooser();
            colorChooser.setDragEnabled(true);
            panel.setBorder(BorderFactory.createTitledBorder("JColorChoosers"));
            panel.add(colorChooser);
            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
        }
    });
}
 
Example 3
Source Project: iBioSim   Source File: EditPreferences.java    License: Apache License 2.0 6 votes vote down vote up
private static JButton createColorButton(Color color) {
	JButton colorButton = new JButton();
	colorButton.setPreferredSize(new Dimension(30, 20));
	colorButton.setBorder(BorderFactory.createLineBorder(Color.darkGray));
	colorButton.setBackground(color);
	colorButton.setForeground(color);
	colorButton.setUI(new MetalButtonUI());
	//colorButton.setActionCommand("" + i);
	colorButton.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			//int i = Integer.parseInt(e.getActionCommand());
			Color newColor = JColorChooser.showDialog(Gui.frame, "Choose Color", ((JButton) e.getSource()).getBackground());
			if (newColor != null) {
				((JButton) e.getSource()).setBackground(newColor);
				((JButton) e.getSource()).setForeground(newColor);
			}
		}
	});
	return colorButton;
}
 
Example 4
Source Project: blog-codes   Source File: EditorActions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
	if (e.getSource() instanceof mxGraphComponent)
	{
		mxGraphComponent graphComponent = (mxGraphComponent) e
				.getSource();
		Color newColor = JColorChooser.showDialog(graphComponent,
				mxResources.get("background"), null);

		if (newColor != null)
		{
			graphComponent.getViewport().setOpaque(true);
			graphComponent.getViewport().setBackground(newColor);
		}

		// Forces a repaint of the outline
		graphComponent.getGraph().repaint();
	}
}
 
Example 5
Source Project: openjdk-jdk8u   Source File: Test4759934.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (CMD_DIALOG.equals(command)) {
        JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
        dialog.setLocation(200, 0);
        show(dialog, CMD_CHOOSER);
    }
    else if (CMD_CHOOSER.equals(command)) {
        Object source = event.getSource();
        Component component = (source instanceof Component)
                ? (Component) source
                : null;

        JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
    }
}
 
Example 6
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            JColorChooser colorChooser = new JColorChooser();
            colorChooser.setDragEnabled(true);
            panel.setBorder(BorderFactory.createTitledBorder("JColorChoosers"));
            panel.add(colorChooser);
            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
        }
    });
}
 
Example 7
Source Project: openjdk-jdk8u   Source File: Test4177735.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    JColorChooser chooser = new JColorChooser();
    AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
    chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });

    JDialog dialog = show(chooser);
    pause(DELAY);

    dialog.dispose();
    pause(DELAY);

    Test4177735 test = new Test4177735();
    SwingUtilities.invokeAndWait(test);
    if (test.count != 0) {
        throw new Error("JColorChooser leaves " + test.count + " threads running");
    }
}
 
Example 8
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            JColorChooser colorChooser = new JColorChooser();
            colorChooser.setDragEnabled(true);
            panel.setBorder(BorderFactory.createTitledBorder("JColorChoosers"));
            panel.add(colorChooser);
            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
        }
    });
}
 
Example 9
Source Project: openjdk-8   Source File: Test4193384.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void test(Color[] colors) {
    JLabel label = new JLabel("Preview Panel"); // NON-NLS: simple label

    JColorChooser chooser = new JColorChooser();
    chooser.setPreviewPanel(label);

    float[] hsb = new float[3];
    for (int i = 0; i < colors.length; i++) {
        Color color = colors[i];
        // Make sure sure that there wasn't a regression
        // in java.awt.Color and the conversion methods
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
        if (!color.equals(Color.getHSBColor(hsb[0], hsb[1], hsb[2]))) {
            throw new Error("color conversion is failed");
        }
        // 4193384 regression test
        if (!color.equals(new JColorChooser(color).getColor())) {
            throw new Error("constructor sets incorrect initial color");
        }
        // 4200976 regression test
        chooser.setColor(color);
        if (!color.equals(label.getForeground())) {
            throw new Error("a custom preview panel doesn't handle colors");
        }
    }
}
 
Example 10
Source Project: dragonwell8_jdk   Source File: Test4193384.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void test(Color[] colors) {
    JLabel label = new JLabel("Preview Panel"); // NON-NLS: simple label

    JColorChooser chooser = new JColorChooser();
    chooser.setPreviewPanel(label);

    float[] hsb = new float[3];
    for (int i = 0; i < colors.length; i++) {
        Color color = colors[i];
        // Make sure sure that there wasn't a regression
        // in java.awt.Color and the conversion methods
        Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
        if (!color.equals(Color.getHSBColor(hsb[0], hsb[1], hsb[2]))) {
            throw new Error("color conversion is failed");
        }
        // 4193384 regression test
        if (!color.equals(new JColorChooser(color).getColor())) {
            throw new Error("constructor sets incorrect initial color");
        }
        // 4200976 regression test
        chooser.setColor(color);
        if (!color.equals(label.getForeground())) {
            throw new Error("a custom preview panel doesn't handle colors");
        }
    }
}
 
Example 11
Source Project: marathonv5   Source File: ColorEditor.java    License: Apache License 2.0 6 votes vote down vote up
public ColorEditor() {
    // Set up the editor (from the table's point of view),
    // which is a button.
    // This button brings up the color chooser dialog,
    // which is the editor from the user's point of view.
    button = new JButton();
    button.setActionCommand(EDIT);
    button.addActionListener(this);
    button.setBorderPainted(false);

    // Set up the dialog that the button brings up.
    colorChooser = new JColorChooser();
    dialog = JColorChooser.createDialog(button, "Pick a Color", true, // modal
            colorChooser, this, // OK button handler
            null); // no CANCEL button handler
}
 
Example 12
Source Project: jdk8u_jdk   Source File: Test4759934.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (CMD_DIALOG.equals(command)) {
        JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
        dialog.setLocation(200, 0);
        show(dialog, CMD_CHOOSER);
    }
    else if (CMD_CHOOSER.equals(command)) {
        Object source = event.getSource();
        Component component = (source instanceof Component)
                ? (Component) source
                : null;

        JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
    }
}
 
Example 13
public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (CMD_DIALOG.equals(command)) {
        JDialog dialog = new JDialog(this.frame, "Dialog"); // NON-NLS: dialog title
        dialog.setLocation(200, 0);
        show(dialog, CMD_CHOOSER);
    }
    else if (CMD_CHOOSER.equals(command)) {
        Object source = event.getSource();
        Component component = (source instanceof Component)
                ? (Component) source
                : null;

        JColorChooser.showDialog(component, "ColorChooser", Color.BLUE); // NON-NLS: title
    }
}
 
Example 14
/**
 * Allow the user the opportunity to select a Paint object.  For now, we
 * just use the standard color chooser - all colors are Paint objects, but
 * not all Paint objects are colors (later we can implement a more general
 * Paint chooser).
 */
public void attemptPaintSelection() {
    Paint p = this.titlePaint.getPaint();
    Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
    Color c = JColorChooser.showDialog(
        this, localizationResources.getString("Title_Color"), defaultColor
    );
    if (c != null) {
        this.titlePaint.setPaint(c);
    }
}
 
Example 15
Source Project: jdk8u_jdk   Source File: Test4177735.java    License: GNU General Public License v2.0 5 votes vote down vote up
static JDialog show(JColorChooser chooser) {
    JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
    dialog.setVisible(true);
    // block till displayed
    Point point = null;
    while (point == null) {
        try {
            point = dialog.getLocationOnScreen();
        }
        catch (IllegalStateException exception) {
            pause(DELAY);
        }
    }
    return dialog;
}
 
Example 16
/**
 * Handle a grid paint selection.
 */
protected void attemptGridPaintSelection() {
    Color c;
    c = JColorChooser.showDialog(this, localizationResources.getString(
            "Grid_Color"), Color.blue);
    if (c != null) {
        this.gridPaintSample.setPaint(c);
    }
}
 
Example 17
Source Project: openstock   Source File: DefaultPlotEditor.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Allow the user to change the background paint.
 */
private void attemptBackgroundPaintSelection() {
    Color c;
    c = JColorChooser.showDialog(this, localizationResources.getString(
            "Background_Color"), Color.blue);
    if (c != null) {
        this.backgroundPaintSample.setPaint(c);
    }
}
 
Example 18
Source Project: openjdk-jdk8u   Source File: Test4234761.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    JColorChooser chooser = new JColorChooser(COLOR);
    JDialog dialog = Test4177735.show(chooser);

    PropertyChangeListener listener = new Test4234761();
    chooser.addPropertyChangeListener("color", listener); // NON-NLS: property name

    JTabbedPane tabbedPane = (JTabbedPane) chooser.getComponent(0);
    tabbedPane.setSelectedIndex(1); // HSB tab index

    if (!chooser.getColor().equals(COLOR)) {
        listener.propertyChange(null);
    }
    dialog.dispose();
}
 
Example 19
protected void selectColor() {
  ResourceManager rm = ResourceManager.all(FilePropertyEditor.class);
  String title = rm.getString("ColorPropertyEditor.title");
  Color selectedColor = JColorChooser.showDialog(editor, title, color);

  if (selectedColor != null) {
    Color oldColor = color;
    Color newColor = selectedColor;
    label.setValue(newColor);
    color = newColor;
    firePropertyChange(oldColor, newColor);
  }
}
 
Example 20
Source Project: jdk8u-dev-jdk   Source File: Test4165217.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    JColorChooser chooser = new JColorChooser();
    chooser.setColor(new Color(new Random().nextInt()));

    Color before = chooser.getColor();
    Color after = copy(chooser).getColor();

    if (!after.equals(before)) {
        throw new Error("color is changed after serialization");
    }
}
 
Example 21
public static void main(String[] args) {
    JColorChooser chooser = new JColorChooser(COLOR);
    JDialog dialog = Test4177735.show(chooser);

    PropertyChangeListener listener = new Test4234761();
    chooser.addPropertyChangeListener("color", listener); // NON-NLS: property name

    JTabbedPane tabbedPane = (JTabbedPane) chooser.getComponent(0);
    tabbedPane.setSelectedIndex(1); // HSB tab index

    if (!chooser.getColor().equals(COLOR)) {
        listener.propertyChange(null);
    }
    dialog.dispose();
}
 
Example 22
protected Color showMoreColorsDialog() {
    JColorChooser colorChooser = new JColorChooser(getSelectedColor());
    AbstractColorChooserPanel[] oldChooserPanels = colorChooser.getChooserPanels();
    AbstractColorChooserPanel[] newChooserPanels = new AbstractColorChooserPanel[oldChooserPanels.length + 1];
    System.arraycopy(oldChooserPanels, 0, newChooserPanels, 1, oldChooserPanels.length);
    newChooserPanels[0] = new MyAbstractColorChooserPanel();
    colorChooser.setChooserPanels(newChooserPanels);
    ColorTracker colorTracker = new ColorTracker(colorChooser);
    JDialog dialog = JColorChooser.createDialog(this, "Select Colour", true, colorChooser, colorTracker, null);
    dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);
    return colorTracker.getColor();
}
 
Example 23
Source Project: astor   Source File: DefaultPlotEditor.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allow the user to change the background paint.
 */
private void attemptBackgroundPaintSelection() {
    Color c;
    c = JColorChooser.showDialog(this, localizationResources.getString(
            "Background_Color"), Color.blue);
    if (c != null) {
        this.backgroundPaintSample.setPaint(c);
    }
}
 
Example 24
Source Project: hottub   Source File: Test6348456.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void init() {
    JButton button = new JButton("Swap models");
    button.addActionListener(this);

    this.chooser = new JColorChooser(Color.RED);
    this.chooser.setSelectionModel(WHITE);

    add(BorderLayout.NORTH, button);
    add(BorderLayout.CENTER, this.chooser);
}
 
Example 25
Source Project: openjdk-8-source   Source File: Test6199676.java    License: GNU General Public License v2.0 5 votes vote down vote up
public synchronized void run() {
    if (this.chooser == null) {
        this.chooser = new JColorChooser();

        JFrame frame = new JFrame(getClass().getName());
        frame.add(this.chooser);
        frame.setVisible(true);
    }
    else if (this.updated) {
        if (isShowing(this.chooser.getPreviewPanel())) {
            exit("custom preview panel is showing");
        }
        exit(null);
    }
    else {
        Component component = this.chooser.getPreviewPanel();
        if (component == null) {
            component = getPreview(this.chooser);
        }
        if (!isShowing(component)) {
            exit("default preview panel is not showing");
        }
        this.updated = true;
        this.chooser.setPreviewPanel(new JPanel());
    }
    LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
    LookAndFeelInfo info = infos[++this.index % infos.length];
    try {
        UIManager.setLookAndFeel(info.getClassName());
    }
    catch (Exception exception) {
        exit("could not change L&F");
    }
    SwingUtilities.updateComponentTreeUI(this.chooser);
    SwingUtilities.invokeLater(this);
}
 
Example 26
/**
 * Allow the user the opportunity to select a Paint object.  For now, we
 * just use the standard color chooser - all colors are Paint objects, but
 * not all Paint objects are colors (later we can implement a more general
 * Paint chooser).
 */
public void attemptPaintSelection() {
    Paint p = this.titlePaint.getPaint();
    Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
    Color c = JColorChooser.showDialog(
        this, localizationResources.getString("Title_Color"), defaultColor
    );
    if (c != null) {
        this.titlePaint.setPaint(c);
    }
}
 
Example 27
Source Project: MeteoInfo   Source File: FrmLabelSet.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
private void jLabel_ColorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel_ColorMouseClicked
    // TODO add your handling code here:
    Color color = JColorChooser.showDialog(this, null, _color);
    if (color != null) {
        _color = color;
        updateLabelSet();
        updateLabelsFontColor();
        _mapView.paintLayers();
    }
}
 
Example 28
Source Project: astor   Source File: DefaultTitleEditor.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Allow the user the opportunity to select a Paint object.  For now, we
 * just use the standard color chooser - all colors are Paint objects, but
 * not all Paint objects are colors (later we can implement a more general
 * Paint chooser).
 */
public void attemptPaintSelection() {
    Paint p = this.titlePaint.getPaint();
    Color defaultColor = (p instanceof Color ? (Color) p : Color.blue);
    Color c = JColorChooser.showDialog(
        this, localizationResources.getString("Title_Color"), defaultColor
    );
    if (c != null) {
        this.titlePaint.setPaint(c);
    }
}
 
Example 29
Source Project: openjdk-jdk8u   Source File: Test6199676.java    License: GNU General Public License v2.0 5 votes vote down vote up
public synchronized void run() {
    if (this.chooser == null) {
        this.chooser = new JColorChooser();

        JFrame frame = new JFrame(getClass().getName());
        frame.add(this.chooser);
        frame.setVisible(true);
    }
    else if (this.updated) {
        if (isShowing(this.chooser.getPreviewPanel())) {
            exit("custom preview panel is showing");
        }
        exit(null);
    }
    else {
        Component component = this.chooser.getPreviewPanel();
        if (component == null) {
            component = getPreview(this.chooser);
        }
        if (!isShowing(component)) {
            exit("default preview panel is not showing");
        }
        this.updated = true;
        this.chooser.setPreviewPanel(new JPanel());
    }
    LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
    LookAndFeelInfo info = infos[++this.index % infos.length];
    try {
        UIManager.setLookAndFeel(info.getClassName());
    }
    catch (Exception exception) {
        exit("could not change L&F");
    }
    SwingUtilities.updateComponentTreeUI(this.chooser);
    SwingUtilities.invokeLater(this);
}
 
Example 30
Source Project: SIMVA-SoS   Source File: DefaultChartEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Allows the user the opportunity to select a new background paint.  Uses
 * JColorChooser, so we are only allowing a subset of all Paint objects to
 * be selected (fix later).
 */
private void attemptModifyBackgroundPaint() {
    Color c;
    c = JColorChooser.showDialog(this, localizationResources.getString(
            "Background_Color"), Color.blue);
    if (c != null) {
        this.background.setPaint(c);
    }
}