To get the selected color from JColorChooser
we need to create an implementation of the ChangeListener
interface. This interface provide a single method call stateChanged(ChangeEvent e)
. The instance of this interface implementation need to be passed to JColorChooser
by calling the JColorChooser.getSelectionModel().addChangeListener()
method.
In this example our ChangeListener
implementation get the selected color and replace the fore ground color the JLabel
component.
package org.kodejava.example.swing;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class JColorChooserColorSelection extends JFrame {
private JColorChooser jcc = null;
private JLabel label = null;
public JColorChooserColorSelection() {
initializeUI();
}
private void initializeUI() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
jcc = new JColorChooser();
jcc.getSelectionModel().addChangeListener(new ColorSelection());
getContentPane().add(jcc, BorderLayout.PAGE_START);
label = new JLabel("Selected Font Color", JLabel.CENTER);
label.setFont(new Font("SansSerif", Font.BOLD, 24));
label.setForeground(Color.BLACK);
label.setPreferredSize(new Dimension(100, 100));
getContentPane().add(label, BorderLayout.CENTER);
this.pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JColorChooserColorSelection().setVisible(true);
}
});
}
/**
* A ChangeListener implementation for listening the color
* selection of the JColorChooser component.
*/
class ColorSelection implements ChangeListener {
public void stateChanged(ChangeEvent e) {
Color color = jcc.getColor();
label.setForeground(color);
}
}
}
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020