javacolorsjcolorchooser

Change Preview Panel for JColorChooser


The objective is to change the Preview section from this :

enter image description here

To something like this where the Preview area has a border, a solid box in the background and "Hello World" string which changes to the color selected for the Preview.

enter image description here

Started with this sample from java2 which simply shows the JColorChooser.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;

public class ColorChooserSample {
  public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    final JButton button = new JButton("Pick to Change Background");

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Color initialBackground = button.getBackground();
        JColorChooser jColorChooser = new JColorChooser();
        jColorChooser.setPreviewPanel(null);
        Color background = jColorChooser.showDialog(null,
            "JColorChooser Sample", initialBackground);
        if (background != null) {
          button.setBackground(background);
        }
      }
    };
    button.addActionListener(actionListener);
    content.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}

Just to see if I can even affect the Preview area, I modified the original code from this :

    Color background = JColorChooser.showDialog(null,
        "JColorChooser Sample", initialBackground);

To this :

        JColorChooser jColorChooser = new JColorChooser();
        jColorChooser.setPreviewPanel(null);
        Color background = jColorChooser.showDialog(null,
            "JColorChooser Sample", initialBackground);

Basically this is to attempt to see if the Preview section could be null (blank) but it had no affect which has me wondering if the setPreviewPanel() is the correct call.

Also, after the code change this Warning appears : The static method showDialog(Component, String, Color) from the type JColorChooser should be accessed in a static way

Questions :

Are there any examples which alter the preview section of the Color Chooser?

Why did the null above not work.

If the Warning indicates that JColorChooser should be accessed in a static way, how would one actually make a setPreviewPanel() call?


Solution

  • There weren't many examples of modifying the JColorChooser preview panel. I found four examples. Along with the Javadoc for JColorChooser, I created a working example of a modified JColorChooser preview panel.

    JColorChooser Example GUI 1

    I modified the original JFrame / JPanel to change the background color of the JPanel, so it would be easier to see the result.

    JColorChooser Example GUI 2

    Here's the modified JColorChooser.

    JColorChooser Example GUI 3

    Here I changed the background color of the preview JPanel to yellow.

    JColorChooser Example GUI 4

    Which then changes the background color of the main JPanel to yellow.

    Here's the code. I separated the code into bite-sized methods and classes, so I could concentrate on one part of the GUI data a time.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.HeadlessException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class ColorChooserSample implements Runnable{
    
        public static void main(String args[]) {
            SwingUtilities.invokeLater(
                    new ColorChooserSample());
        }
    
        private JPanel panel;
    
        @Override
        public void run() {
            JFrame frame = new JFrame(
                    "JColorChooser Sample");
            frame.setDefaultCloseOperation(
                    JFrame.EXIT_ON_CLOSE);
    
            panel = new JPanel();
            panel.setPreferredSize(new Dimension(300, 200));
            JButton button = new JButton(
                    "Pick to Change JPanel Background");
            button.addActionListener(new ColorListener());
            panel.add(button);
    
            frame.add(panel, BorderLayout.CENTER);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public void setJPanelBackground(Color color) {
            panel.setBackground(color);
            panel.repaint();
        }
    
        public class ColorListener implements 
                ActionListener, ChangeListener {
    
            private JColorChooser chooser;
    
            private JPanel previewPanel;
    
            @Override
            public void actionPerformed(
                    ActionEvent actionEvent) {
                Color backgroundColor = showDialog(panel, 
                        "Set JPanel Background", 
                        panel.getBackground());
                setJPanelBackground(backgroundColor);
            }
    
            private Color showDialog(Component component, 
                    String title, Color initialColor) 
                            throws HeadlessException {
                chooser = new JColorChooser(initialColor);
                chooser.getSelectionModel()
                    .addChangeListener(this);
    
                // configuring color chooser panel
                previewPanel = new JPanel();
                previewPanel.setBackground(initialColor);
                JLabel label = new JLabel("Hello World!");
                previewPanel.add(label);
                chooser.setPreviewPanel(previewPanel);
    
                // creating dialog
                ColorTracker ok = new ColorTracker(chooser);
                JDialog dialog = JColorChooser.createDialog(
                        component, title, true, chooser, 
                        ok, null);
                dialog.setVisible(true);
                return ok.getColor();
            }
    
            @Override
            public void stateChanged(ChangeEvent event) {
                Color newColor = chooser.getColor();
                previewPanel.setBackground(newColor);
            }
    
        }
    
        private class ColorTracker implements ActionListener {
    
            private Color color;
    
            private JColorChooser chooser;
    
            public ColorTracker(JColorChooser chooser) {
                this.chooser = chooser;
            }
    
            @Override
            public void actionPerformed(ActionEvent event) {
                color = chooser.getColor();
            }
    
            public Color getColor() {
                return color;
            }
    
        }
    
    }