Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:53:33+00:00 2026-06-05T18:53:33+00:00

I am using a JColorchooser at various places in an application. There can be

  • 0

I am using a JColorchooser at various places in an application. There can be multiple instances of the panel that can invoke a JColorChooser.
The “Swatches” panel in the chooser has an area of “recent” colors, which only persists within each instance of JColorChooser. I would like to (a) have the same “recent” colors in all my choosers in my application, and (b) to save the colors to disk so that these colors survive close and restart of the application.
(At least (a) could be solved by using the same single chooser instance all over the whole app, but that apears cumbersome because I would need to be very careful with attached changelisteners, and adding/removing the chooser panel to/from various dialogs.)

I did not find any method that lets me set (restore) these “recent” colors in the chooser panel. So to me, it appears that the only ways of achieving this would be:

  • serialize and save / restore the whole chooser (chooser panel?)
    or
  • create my own chooser panel from scratch

Is this correct, or am I missing something?

BTW: I would also like to detect a double click in the chooser, but it seems hard to find the right place to attach my mouse listener to. Do I really need to dig into the internal structure of the chooser panel to do this? (No, it does not work to detect a second click on the same color, because the change listener only fires if a different color is clicked.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:53:34+00:00Added an answer on June 5, 2026 at 6:53 pm

    As you noticed, there is no public api to access the recent colors in the DefaultSwatchChooserPanel, even the panel itself isn’t accessible.

    As you’ll need some logic/bean which holds and resets the recent colors anyway (plus the extended mouse interaction), rolling your own is the way to go. For some guidance, have a look at the implementation of the swatch panel (cough … c&p what you need and modify what you don’t). Basically, something like

    // a bean that keeps track of the colors
    public static class ColorTracker extends AbstractBean {
    
        private List<Color> colors = new ArrayList<>();
    
        public void addColor(Color color) {
            List<Color> old = getColors();
            colors.add(0, color);
            firePropertyChange("colors", old, getColors());
        }
    
        public void setColors(List<Color> colors) {
            List<Color> old = getColors();
            this.colors = new ArrayList<>(colors);
            firePropertyChange("colors", old, getColors());
        }
    
        public List<Color> getColors() {
            return new ArrayList<>(colors);
        }
    }
    
    // a custom SwatchChooserPanel which takes and listens to the tracker changes
    public class MySwatchChooserPanel ... {
    
       ColorTracker tracker;
    
       public void setColorTracker(....) {
           // uninstall old tracker 
           ....
           // install new tracker
           this.tracker = tracker;
           if (tracker != null) 
               tracker.addPropertyChangeListener(.... );
           updateRecentSwatchPanel()
       }
    
       /** 
        * A method updating the recent colors in the swatchPanel
        * This is called whenever necessary, specifically after building the panel,
        * on changes of the tracker, from the mouseListener
        */
       protected void updateRecentSwatchPanel() {
           if (recentSwatchPanel == null) return;
           recentSwatchPanel.setMostRecentColors(tracker != null ? tracker.getColors() : null);
       }
    
    // the mouseListener which updates the tracker and triggers the doubleClickAction
    // if available
    class MainSwatchListener extends MouseAdapter implements Serializable {
        @Override
        public void mousePressed(MouseEvent e) {
            if (!isEnabled())
                return;
            if (e.getClickCount() == 2) {
                handleDoubleClick(e);
                return;
            }
    
            Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
            setSelectedColor(color);
            if (tracker != null) {
                tracker.addColor(color);
            } else {
                recentSwatchPanel.setMostRecentColor(color);
            }
        }
    
        /**
         * @param e
         */
        private void handleDoubleClick(MouseEvent e) {
            if (action != null) {
                action.actionPerformed(null);
            }
        }
    }
    
    
    } 
    
    // client code can install the custom panel on a JFileChooser, passing in a tracker
    private JColorChooser createChooser(ColorTracker tracker) {
        JColorChooser chooser = new JColorChooser();
        List<AbstractColorChooserPanel> choosers = 
                new ArrayList<>(Arrays.asList(chooser.getChooserPanels()));
        choosers.remove(0);
        MySwatchChooserPanel swatch = new MySwatchChooserPanel();
        swatch.setColorTracker(tracker);
        swatch.setAction(doubleClickAction);
        choosers.add(0, swatch);
        chooser.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0]));
        return chooser;
    }
    

    As to doubleClick handling: enhance the swatchChooser to take an action and invoke that action from the mouseListener as appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using EF Code First I have an model object that has multiple properties that
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using Android TelephonyManager an application can obtain the state of data activity over the
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C#, I need a class called User that has a username, password, active
Using Javascript, how can I generate random numbers that are skewed towards one end
Using Nunit, I want to be able to write a test fixture that will
Using PHP, I can convert MySQL data or static table data to csv, Excel,
Using top it's easy to identify processes that are hogging memory and cpu, but
Using jQuery, one can easily find out whether a particular element is visible using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.