Example usage for javax.swing.colorchooser ColorSelectionModel addChangeListener

List of usage examples for javax.swing.colorchooser ColorSelectionModel addChangeListener

Introduction

In this page you can find the example usage for javax.swing.colorchooser ColorSelectionModel addChangeListener.

Prototype

void addChangeListener(ChangeListener listener);

Source Link

Document

Adds listener as a listener to changes in the model.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JColorChooser chooser = new JColorChooser();
    final MyPreviewPanel pre = new MyPreviewPanel(chooser);
    chooser.setPreviewPanel(pre);/*w w  w.  j  a va  2s  .  co  m*/

    ColorSelectionModel model = chooser.getSelectionModel();
    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            ColorSelectionModel model = (ColorSelectionModel) evt.getSource();
            pre.curColor = model.getSelectedColor();
        }
    });

}

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  . jav a  2  s . co  m
    };
    model.addChangeListener(changeListener);
    frame.add(colorChooser, BorderLayout.CENTER);

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

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);
        }//  w w w  .j a v a2s.  c o  m
    };
    model.addChangeListener(changeListener);

    frame.add(colorChooser, BorderLayout.CENTER);

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

From source file:view.AppearanceSettingsDialog.java

/**
 * Creates new form NewJDialog//  w  ww.j  av  a 2  s .  co m
 *
 * @param parent
 * @param modal
 * @param signatureSettings
 */
public AppearanceSettingsDialog(java.awt.Frame parent, boolean modal, CCSignatureSettings signatureSettings) {
    super(parent, modal);
    initComponents();
    this.signatureSettings = signatureSettings;

    updateText();

    // Pastas conforme o SO
    ArrayList<String> dirs = new ArrayList<>();
    if (SystemUtils.IS_OS_WINDOWS) {
        dirs.add(System.getenv("windir") + File.separator + "fonts");
    } else if (SystemUtils.IS_OS_LINUX) {
        dirs.add("/usr/share/fonts/truetype/");
        dirs.add("/usr/X11R6/lib/X11/fonts/");
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        dirs.add("/Library/Fonts");
        dirs.add("/System/Library/Fonts");
    }
    dirs.add("extrafonts");

    // Hashmap com fonts
    hmFonts = getAllFonts(dirs);
    ArrayList<com.itextpdf.text.Font> alFonts = new ArrayList<>(hmFonts.keySet());

    Collections.sort(alFonts, new Comparator<com.itextpdf.text.Font>() {
        @Override
        public int compare(com.itextpdf.text.Font f1, com.itextpdf.text.Font f2) {
            return f1.getFamilyname().compareToIgnoreCase(f2.getFamilyname());
        }
    });

    DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
    for (com.itextpdf.text.Font font : alFonts) {
        dcbm.addElement(font.getFamilyname());
    }
    cbFontType.setModel(dcbm);

    String fontLocation = signatureSettings.getAppearance().getFontLocation();
    boolean italic = signatureSettings.getAppearance().isItalic();
    boolean bold = signatureSettings.getAppearance().isBold();
    boolean showName = signatureSettings.getAppearance().isShowName();
    boolean showLocation = signatureSettings.getAppearance().isShowLocation();
    boolean showReason = signatureSettings.getAppearance().isShowReason();
    boolean showDate = signatureSettings.getAppearance().isShowDate();
    int align = signatureSettings.getAppearance().getAlign();

    switch (align) {
    case 0:
        cbAlign.setSelectedIndex(0);
        break;
    case 1:
        cbAlign.setSelectedIndex(1);
        break;
    case 2:
        cbAlign.setSelectedIndex(2);
        break;
    default:
        cbAlign.setSelectedIndex(0);
    }

    previewPanel1.setReason(signatureSettings.getReason());
    previewPanel1.setShowDate(showDate);

    if (signatureSettings.getCcAlias() != null) {
        previewPanel1.setAliasName(signatureSettings.getCcAlias().getName());
    } else {
        previewPanel1.setAliasName(Bundle.getBundle().getString("name"));
    }

    if (!signatureSettings.getLocation().isEmpty()) {
        previewPanel1.setLocation(signatureSettings.getLocation());
        cbShowLocation.setSelected(showLocation);
        previewPanel1.setShowLocation(showLocation);
    } else {
        cbShowLocation.setEnabled(false);
        cbShowLocation.setSelected(false);
    }

    cbShowName.setSelected(showName);
    previewPanel1.setShowName(showName);

    if (!signatureSettings.getReason().isEmpty()) {
        previewPanel1.setReason(signatureSettings.getReason());
        cbShowReason.setSelected(showReason);
        previewPanel1.setShowReason(showReason);
    } else {
        cbShowReason.setEnabled(false);
        cbShowReason.setSelected(false);
    }
    previewPanel1.setText(signatureSettings.getText());
    previewPanel1.setAlign(align);
    colorChooser.setPreviewPanel(new JPanel());
    Color color = signatureSettings.getAppearance().getFontColor();
    colorChooser.setColor(color);
    lblSampleText.setForeground(color);
    cbShowReason.setSelected(showReason);
    cbShowLocation.setSelected(showLocation);
    cbShowDateTime.setSelected(showDate);

    ColorSelectionModel model = colorChooser.getSelectionModel();
    ChangeListener changeListener = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent changeEvent) {
            Color newForegroundColor = colorChooser.getColor();
            lblSampleText.setForeground(newForegroundColor);
        }
    };
    model.addChangeListener(changeListener);

    if (fontLocation.contains("aCCinaPDF" + File.separator + "extrafonts")) {
        try {
            Font newFont = Font.createFont(Font.TRUETYPE_FONT, new File(fontLocation));
            Font font = null;
            if (italic && bold) {
                font = newFont.deriveFont(Font.ITALIC + Font.BOLD, 36);
            } else if (italic && !bold) {
                font = newFont.deriveFont(Font.ITALIC, 36);
            } else if (!italic && bold) {
                font = newFont.deriveFont(Font.BOLD, 36);
            } else {
                font = newFont.deriveFont(Font.PLAIN, 36);
            }
            lblSampleText.setFont(font);
        } catch (FontFormatException | IOException ex) {
        }
    } else {
        if (italic && bold) {
            lblSampleText.setFont(new Font(fontLocation, Font.ITALIC + Font.BOLD, 36));
        } else if (italic && !bold) {
            lblSampleText.setFont(new Font(fontLocation, Font.ITALIC, 36));
        } else if (!italic && bold) {
            lblSampleText.setFont(new Font(fontLocation, Font.BOLD, 36));
        } else {
            lblSampleText.setFont(new Font(fontLocation, Font.PLAIN, 36));
        }
    }

    cbBold.setSelected(bold);
    cbItalic.setSelected(italic);

    updateSettings(fontLocation, bold, italic);

    previewPanel1.repaint();
}