![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e883. Adding a Custom Color Chooser Panel to a JColorChooser DialogThis example creates a color chooser panel with three colored buttons. Clicking on a button changes the selected color to the color of the button.JColorChooser chooser = new JColorChooser(); chooser.addChooserPanel(new MyChooserPanel()); public class MyChooserPanel extends AbstractColorChooserPanel { // These are the methods that must be implemented // in order to create a color chooser panel. // This is called once to initialize the panel. public void buildChooser() { setLayout(new GridLayout(0, 3)); makeAddButton("Red", Color.red); makeAddButton("Green", Color.green); makeAddButton("Blue", Color.blue); } // This method is called whenever the user chooses this panel. // This method should retrieve the currently selected color. public void updateChooser() { } // This method is called to retrieve the label used // in the tab that selects this panel. public String getDisplayName() { return "MyChooserPanel"; } // This method is currently not used. public Icon getSmallDisplayIcon() { return null; } // This method is currently not used. public Icon getLargeDisplayIcon() { return null; } // These are helper methods specifically for this example // Creates a color button and adds it to this panel. private void makeAddButton(String name, Color color) { JButton button = new JButton(name); button.setBackground(color); button.setAction(setColorAction); add(button); } // This action takes the background color of the button // and uses it to set the selected color. Action setColorAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { JButton button = (JButton)evt.getSource(); getColorSelectionModel().setSelectedColor(button.getBackground()); } }; }
e881. Removing a Color Chooser Panel from a JColorChooser Dialog e882. Setting the Order of the Color Chooser Panel Tabs in a JColorChooser Dialog
© 2002 Addison-Wesley. |