JColorChooser: getSelectionModel() : JColorChooser « javax.swing « Java by API






JColorChooser: getSelectionModel()


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainClass {

  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);
      }
    };
    model.addChangeListener(changeListener);
    frame.add(colorChooser, BorderLayout.CENTER);

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

}
           
       








Related examples in the same category

1.JColorChooser: addChooserPanel(AbstractColorChooserPanel p)
2.JColorChooser: createDialog(Component c, String t, boolean m, JColorChooser c, ActionListener ok, ActionListener cancel)
3.JColorChooser: setDragEnabled(boolean b)
4.JColorChooser: setPreviewPanel(JComponent preview)
5.JColorChooser: showDialog(Component component, String title, Color initialColor)