Example usage for javax.swing JColorChooser getColor

List of usage examples for javax.swing JColorChooser getColor

Introduction

In this page you can find the example usage for javax.swing JColorChooser getColor.

Prototype

public Color getColor() 

Source Link

Document

Gets the current color value from the color chooser.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JColorChooser chooser = new JColorChooser();

    // Set the selected color
    chooser.setColor(Color.red);//w  w  w .  j a  v  a2 s .  c o m

    // Get current selected color
    Color color = chooser.getColor();

}

From source file:Main.java

public static void main(String[] argv) {
    final JColorChooser chooser = new JColorChooser();
    ActionListener okListener = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Color newColor = chooser.getColor();
        }//  w  w  w  .java2 s.c o  m
    };

    ActionListener cancelListener = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Color newColor = chooser.getColor();
        }
    };

    boolean modal = false;

    JDialog dialog = JColorChooser.createDialog(null, "Dialog Title", modal, chooser, okListener,
            cancelListener);

    dialog.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            Color newColor = chooser.getColor();
        }
    });
}

From source file:ListeningJColorChooserSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));

    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());

    ColorSelectionModel model = colorChooser.getSelectionModel();
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            Color newForegroundColor = colorChooser.getColor();
            label.setForeground(newForegroundColor);
        }//www.  j  a v  a  2s . c o m
    };
    model.addChangeListener(changeListener);

    frame.add(colorChooser, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JColorChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
    label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
    frame.add(label, BorderLayout.SOUTH);

    final JColorChooser colorChooser = new JColorChooser(label.getBackground());
    colorChooser.setBorder(BorderFactory.createTitledBorder("Pick Color for java2s.com"));

    ColorSelectionModel model = colorChooser.getSelectionModel();
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            Color newForegroundColor = colorChooser.getColor();
            label.setForeground(newForegroundColor);
        }/*from w ww  .  j a  v  a 2s. c om*/
    };
    model.addChangeListener(changeListener);
    frame.add(colorChooser, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    final JColorChooser colorChooser = new JColorChooser();
    SystemColorChooserPanel newChooser = new SystemColorChooserPanel();
    colorChooser.addChooserPanel(newChooser);

    ActionListener okActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(colorChooser.getColor());
        }/*from  w  w  w  .  ja v  a2s  . c o  m*/
    };

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

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

From source file:MainClass.java

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());
        }/*  w  ww  . j  a v a  2 s  .  c  om*/
    };

    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);
}

From source file:CreateColorSamplePopup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JColorChooser Create Popup Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JButton button = new JButton("Pick to Change Background");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color initialBackground = button.getBackground();

            final JColorChooser colorChooser = new JColorChooser(initialBackground);
            //        colorChooser.setPreviewPanel(new JPanel());
            final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER);
            previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
            colorChooser.setPreviewPanel(previewLabel);
            // Bug workaround
            colorChooser.updateUI();/*from  w w  w.j  av  a  2s. c  o  m*/

            // For okay button selection, change button background to
            // selected color
            ActionListener okActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    Color newColor = colorChooser.getColor();
                    if (newColor.equals(button.getForeground())) {
                        System.out.println("Color change rejected");
                    } else {
                        button.setBackground(colorChooser.getColor());
                    }
                }
            };

            // For cancel button selection, change button background to red
            ActionListener cancelActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    button.setBackground(Color.red);
                }
            };

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

            // Wait until current event dispatching completes before showing
            // dialog
            Runnable showDialog = new Runnable() {
                public void run() {
                    dialog.show();
                }
            };
            SwingUtilities.invokeLater(showDialog);
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public MyPreviewPanel(JColorChooser chooser) {
    curColor = chooser.getColor();

    setPreferredSize(new Dimension(50, 50));
}

From source file:net.sf.maltcms.chromaui.chromatogram1Dviewer.ui.panel.Chromatogram1DHeatmapViewerPanel.java

private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
    if (jCheckBox2.isSelected()) {
        JColorChooser jcc = new JColorChooser(
                backgroundColor == null ? (Color) ps.getPaint(ps.getLowerBound()) : backgroundColor);
        DialogDescriptor dd = new DialogDescriptor(jcc, "Select background color");
        Object result = DialogDisplayer.getDefault().notify(dd);
        if (result == NotifyDescriptor.OK_OPTION) {
            setBackgroundColor(jcc.getColor());
        }/*  w ww.  j a va 2s .c  o m*/
    } else {
        setBackgroundColor((Color) ps.getPaint(ps.getLowerBound()));
    }
}