I have provided you with an example of coding having the Change Listener. You will find the coding very lengthy but it can be useful for you to understand things in full coding :
Code:
import java.awt.*;
import javax.swing.event.*;
import javax.swing.colorchooser.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorChooserDemo extends JPanel
implements ChangeListener {
protected JColorChooser tcc;
protected JLabel banner;
public ColorChooserDemo() {
super(new BorderLayout());
banner = new JLabel("This is Study Place!",
JLabel.CENTER);
banner.setForeground(Color.red);
banner.setBackground(Color.yellow);
banner.setOpaque(true);
banner.setFont(new Font("Times", Font.BOLD, 22));
banner.setPreferredSize(new Dimension(120, 77));
JPanel bannerPanel = new JPanel(new BorderLayout());
bannerPanel.add(banner, BorderLayout.CENTER);
bannerPanel.setBorder(BorderFactory.createTitledBorder("Banner"));
tcc = new JColorChooser(banner.getForeground());
tcc.getSelectionModel().addChangeListener(this);
tcc.setBorder(BorderFactory.createTitledBorder(
"Choose Text Color"));
add(bannerPanel, BorderLayout.CENTER);
add(tcc, BorderLayout.PAGE_END);
}
public void stateChanged(ChangeEvent e) {
Color newColor = tcc.getColor();
banner.setForeground(newColor);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ColorChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new ColorChooserDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Bookmarks