下面列出了javax.swing.JColorChooser#createDialog ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ColorEditor() {
// Set up the editor (from the table's point of view),
// which is a button.
// This button brings up the color chooser dialog,
// which is the editor from the user's point of view.
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
// Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button, "Pick a Color", true, // modal
colorChooser, this, // OK button handler
null); // no CANCEL button handler
}
private Color showDialog(Component component, String title, Color initialColor) throws HeadlessException {
final JColorChooser chooser = new JColorChooser(initialColor != null ? initialColor : Color.white);
// configuring color chooser panel
chooser.setPreviewPanel(new JPanel());
if (colorChoosers != null) {
chooser.setChooserPanels(colorChoosers);
} else {
chooser.removeChooserPanel(chooser.getChooserPanels()[0]);
chooser.removeChooserPanel(chooser.getChooserPanels()[1]);
}
// creating dialog
ColorTracker ok = new ColorTracker(chooser);
JDialog dialog = JColorChooser.createDialog(component, title, true, chooser, ok, null);
dialog.setVisible(true); // blocks until user brings dialog down...
return ok.getColor();
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
Object object = actionEvent.getSource();
Component component = object instanceof Component ? (Component)object : null;
JDialog jDialog = JColorChooser.createDialog(component, "ColorChooser", false, new JColorChooser(Color.BLUE), null, null);
jDialog.setVisible(true);
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
public ColorTableCellEditor() {
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
//Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button,
StaticMessageSource.getMessage("ColorChooser.chooseColor"),
true, //modal
colorChooser,
this, //OK button handler
null); //no CANCEL button handler
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
public ColorEditor()
{
b = new JButton("Edit");
b.addActionListener(this);
cc = new JColorChooser();
dl = JColorChooser.createDialog(b, "Choose a Color", true, cc,
this, null);
}
protected Color showMoreColorsDialog() {
JColorChooser colorChooser = new JColorChooser(getSelectedColor());
AbstractColorChooserPanel[] oldChooserPanels = colorChooser.getChooserPanels();
AbstractColorChooserPanel[] newChooserPanels = new AbstractColorChooserPanel[oldChooserPanels.length + 1];
System.arraycopy(oldChooserPanels, 0, newChooserPanels, 1, oldChooserPanels.length);
newChooserPanels[0] = new MyAbstractColorChooserPanel();
colorChooser.setChooserPanels(newChooserPanels);
ColorTracker colorTracker = new ColorTracker(colorChooser);
JDialog dialog = JColorChooser.createDialog(this, "Select Colour", true, colorChooser, colorTracker, null);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
return colorTracker.getColor();
}
public void actionPerformed(ActionEvent e) {
colorDialog = JColorChooser.createDialog(frame, "Choose A Color", true, colorChooser, okListener, cancelListener);
colorDialog.setVisible(true);
}
private static Color noTransparencyColorChooser(Component component, String title, Color initialColor) throws Exception {
final JColorChooser pane = new JColorChooser(initialColor != null
? initialColor : Color.white);
AbstractColorChooserPanel[] colorPanels = pane.getChooserPanels();
for (int i = 1; i < colorPanels.length; i++) {
AbstractColorChooserPanel cp = colorPanels[i];
Field f = cp.getClass().getDeclaredField("panel");
f.setAccessible(true);
Object colorPanel = f.get(cp);
Field f2 = colorPanel.getClass().getDeclaredField("spinners");
f2.setAccessible(true);
Object spinners = f2.get(colorPanel);
Object transpSlispinner = Array.get(spinners, 3);
if (i == colorPanels.length - 1) {
transpSlispinner = Array.get(spinners, 4);
}
Field f3 = transpSlispinner.getClass().getDeclaredField("slider");
f3.setAccessible(true);
JSlider slider = (JSlider) f3.get(transpSlispinner);
slider.setEnabled(false);
Field f4 = transpSlispinner.getClass().getDeclaredField("spinner");
f4.setAccessible(true);
JSpinner spinner = (JSpinner) f4.get(transpSlispinner);
spinner.setEnabled(false);
}
final Color[] col = new Color[]{initialColor};
JDialog dialog = JColorChooser.createDialog(component, title, true, pane, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
col[0] = pane.getColor();
}
}, null);
dialog.setVisible(true);
return col[0];
}