Java JColorChooser .createDialog (Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener)

Syntax

JColorChooser.createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) has the following syntax.

public static JDialog createDialog(Component c,   String title,   boolean modal,   JColorChooser chooserPane,   ActionListener okListener,   ActionListener cancelListener)    throws HeadlessException

Example

In the following code shows how to use JColorChooser.createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) method.


/*from   w w  w  . j  ava2  s .c o m*/

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class Main {

  public static void main(String[] a) {

    final JColorChooser colorChooser = new JColorChooser();
    final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
    previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    previewLabel.setSize(previewLabel.getPreferredSize());
    previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0));
    colorChooser.setPreviewPanel(previewLabel);

    ActionListener okActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("OK Button");
        System.out.println(colorChooser.getColor());
      }
    };

    ActionListener cancelActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Cancel Button");
      }
    };

    final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true,
        colorChooser, okActionListener, cancelActionListener);

    dialog.setVisible(true);
  }
}