To create an instance of a JColorChooser with a default or initial color such as Color.BLUE you can pass the Color object to the constructor of the JColorChooser component. The example below shows how to do it.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class JColorChooserDefaultColor extends JFrame {
public JColorChooserDefaultColor() throws HeadlessException {
initializeUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
() -> new JColorChooserDefaultColor().setVisible(true));
}
private void initializeUI() {
setTitle("JColorChooser Demo");
setLayout(new BorderLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Creates an instance of JColorChooser and set Color.BLUE
// as the default selected color.
JColorChooser jcc = new JColorChooser(Color.BLUE);
getContentPane().add(jcc, BorderLayout.CENTER);
this.pack();
}
}
To get the selected color from JColorChooser we need to create an implementation of the ChangeListener interface. This interface provides 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 foreground color the JLabel component.
package org.kodejava.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();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
() -> new JColorChooserColorSelection().setVisible(true));
}
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();
}
/**
* 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);
}
}
}
The JColorChooser is a Swing component that provides a palette from where we can select a color code in RGB format. The JColorChooser component has two parts, the tabbed pane of color selection and a preview box. The tabbed has three tabs which allows us to select a color from a swatches, a HSB (Hue, Saturation and Brightness) combination and an RGB (Red Blue Green) color combination.
package org.kodejava.swing;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class JColorChooserDemo extends JFrame implements ChangeListener {
private JColorChooser colorChooser = null;
public JColorChooserDemo() throws HeadlessException {
initUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
() -> new JColorChooserDemo().setVisible(true));
}
private void initUI() {
// Set title and default close operation of this JFrame.
setTitle("JColorChooser Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Creates an instance of JColorChooser component and
// adds it to the frame's content.
colorChooser = new JColorChooser();
getContentPane().add(colorChooser, BorderLayout.PAGE_END);
// Add a change listener to get the selected color in this
// JColorChooser component.
colorChooser.getSelectionModel().addChangeListener(this);
this.pack();
}
/**
* Handles color selection in the JColorChooser component.
*
* @param e the ChangeEvent
*/
public void stateChanged(ChangeEvent e) {
// Get the selected color in the JColorChooser component
// and print the color in RGB format to the console.
Color color = colorChooser.getColor();
System.out.println("color = " + color);
}
}
When you run the program above a frame with JColorChooser component will be shown. A string of the color code in an RGB format will be printed in the console if you click a color from the color selection.
The example below demonstrate how to create a custom JTextPane component that have a background image.
package org.kodejava.swing;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;
public class TextPaneWithBackgroundImage extends JFrame {
public TextPaneWithBackgroundImage() {
initUI();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
() -> new TextPaneWithBackgroundImage().setVisible(true));
}
private void initUI() {
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
String imageFile = "/logo.png";
CustomTextPane textPane = new CustomTextPane(imageFile);
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(getWidth(), getHeight()));
getContentPane().add(scrollPane);
pack();
}
}
/**
* A custom text pane that have a background image. To customize the
* JTextPane we override the paintComponent(Graphics g) method. We have
* to draw an image before we call the super class paintComponent method.
*/
class CustomTextPane extends JTextPane {
private final String imageFile;
/**
* Constructor of custom text pane.
*
* @param imageFile image file name for the text pane background.
*/
CustomTextPane(String imageFile) {
super();
// To be able to draw the background image the component must
// not be opaque.
setOpaque(false);
setForeground(Color.BLUE);
this.imageFile = imageFile;
}
@Override
protected void paintComponent(Graphics g) {
try {
// Load an image for the background image of out JTextPane.
BufferedImage image = ImageIO.read(Objects.requireNonNull(
getClass().getResourceAsStream(imageFile)));
g.drawImage(image, 0, 0, (int) getSize().getWidth(),
(int) getSize().getHeight(), this);
} catch (IOException e) {
e.printStackTrace();
}
super.paintComponent(g);
}
}