Wednesday, August 03, 2011

JColorChooser History

JColorChooser in java can be used to select a color from "Swatches", "HSB", "RGB" panels.  But only "Swatches" tab has a history of recent used colors. We want to have a global history for selected colors.

For that we have to add another panel in a JColorChooser (like ExcelPalette).  In java you have to create a new class which extends AbstractColorChooserPanel:
public class HistoryColorChooserPanel extends AbstractColorChooserPanel {          
         @Override
         protected void buildChooser() {    
                //  here you add your UI components
         }
}
To use it:
AbstractColorChooserPanel[] panels = chooser.getChooserPanels() ;
// create a new array of panels containing also your panel
chooser.setChooserPanels(newPanels);


Anytime a color is selected from any tab except "Color History", that color is added to history as the top-left color from the matrix, and all the other colors are moved by a cell to the right.

At first glance it does not seem a "big" functionality, but when you think you want to use a number of colors to make your reports and charts look alike, this will help you a lot of time instead of entering again and again the same colors.

No comments: