| |
e884. Listening for OK and Cancel Events in a JColorChooser Dialog
This example defines an action that creates and shows a
color chooser dialog.
// Create the chooser
final JColorChooser chooser = new JColorChooser();
// Define listener for ok events
ActionListener okListener = new ActionListener() {
// Called when user clicks ok
public void actionPerformed(ActionEvent evt) {
// Note: The source is an internal button in the dialog
// and should not be used
// Get selected color
Color newColor = chooser.getColor();
}
};
// Define listener for cancel events
ActionListener cancelListener = new ActionListener() {
// Called when user clicks cancel
public void actionPerformed(ActionEvent evt) {
// Note: The source is an internal button in the dialog
// and should not be used
// Note: The original color is not restored.
// getColor() returns the latest selected color.
Color newColor = chooser.getColor();
}
};
// Choose whether dialog is modal or modeless
boolean modal = false;
// Create the dialog that contains the chooser
dialog = JColorChooser.createDialog(null, "Dialog Title", modal,
chooser, okListener, cancelListener);
// Add listener for clicks to the close-window icon
dialog.addWindowListener(new WindowAdapter() {
// Called when user clicks the close-window icon.
// This type of event is usually treated like a cancel.
public void windowClosing(WindowEvent evt) {
// Note: The original color is not restored.
// getColor() returns the latest selected color.
Color newColor = chooser.getColor();
}
}) ;
e885.
Listening for Changes to the Selected Color in a JColorChooser Dialog
© 2002 Addison-Wesley.
|