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();
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024