use of javax.swing.JColorChooser in project CodenameOne by codenameone.
the class ColorIcon method installWithColorPicker.
public static void installWithColorPicker(final JButton button, final JTextComponent colorText) {
install(button, colorText);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int color = Integer.decode("0x" + colorText.getText());
if (colorChooser == null) {
colorChooser = new JColorChooser();
}
colorChooser.setColor(color);
JDialog dlg = JColorChooser.createDialog(button, "Pick color", true, colorChooser, new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = colorChooser.getColor().getRGB() & 0xffffff;
colorText.setText(Integer.toHexString(i));
}
}, null);
dlg.setLocationByPlatform(true);
dlg.pack();
dlg.setVisible(true);
}
});
}
use of javax.swing.JColorChooser in project hid-serial by rayshobby.
the class G4P method selectColor.
/**
* This will open a version of the Java Swing color chooser dialog. The dialog's
* UI is dependent on the OS and JVM implementation running. <br>
*
* If you click on Cancel then it returns the last color previously selected.
*
* @return the ARGB colour as a 32 bit integer (as used in Processing).
*/
public static int selectColor() {
Frame owner = (sketchApplet == null) ? null : sketchApplet.frame;
if (chooser == null) {
chooser = new JColorChooser();
AbstractColorChooserPanel[] oldPanels = chooser.getChooserPanels();
// Do not assume what panels are present
LinkedList<AbstractColorChooserPanel> panels = new LinkedList<AbstractColorChooserPanel>();
for (AbstractColorChooserPanel p : oldPanels) {
String displayName = p.getDisplayName().toLowerCase();
if (displayName.equals("swatches"))
panels.addLast(p);
else if (displayName.equals("rgb"))
panels.addFirst(p);
else if (displayName.startsWith("hs"))
panels.addFirst(p);
}
AbstractColorChooserPanel[] newPanels;
newPanels = panels.toArray(new AbstractColorChooserPanel[panels.size()]);
chooser.setChooserPanels(newPanels);
ColorPreviewPanel pp = new ColorPreviewPanel(lastColor);
chooser.getSelectionModel().addChangeListener(pp);
chooser.setPreviewPanel(pp);
}
// Set the preview color
((ColorPreviewPanel) chooser.getPreviewPanel()).setPrevColor(lastColor);
// Use the last color selected to start it off
chooser.setColor(lastColor);
JDialog dialog = JColorChooser.createDialog(owner, "Color picker", true, chooser, new ActionListener() {
public void actionPerformed(ActionEvent e) {
lastColor = chooser.getColor();
}
}, null);
dialog.setVisible(true);
return lastColor.getRGB();
}
use of javax.swing.JColorChooser in project runelite by runelite.
the class ConfigPanel method openGroupConfigPanel.
private void openGroupConfigPanel(Config config, ConfigDescriptor cd, ConfigManager configManager) {
scrollBarPosition = getScrollPane().getVerticalScrollBar().getValue();
removeAll();
String name = cd.getGroup().name() + " Configuration";
JLabel title = new JLabel(name);
title.setToolTipText(cd.getGroup().description());
add(title, SwingConstants.CENTER);
for (ConfigItemDescriptor cid : cd.getItems()) {
if (cid.getItem().hidden()) {
continue;
}
JPanel item = new JPanel();
item.setLayout(new BorderLayout());
name = cid.getItem().name();
JLabel configEntryName = new JLabel(name);
configEntryName.setToolTipText("<html>" + name + ":<br>" + cid.getItem().description() + "</html>");
item.add(configEntryName, BorderLayout.CENTER);
if (cid.getType() == boolean.class) {
JCheckBox checkbox = new JCheckBox();
checkbox.setSelected(Boolean.parseBoolean(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName())));
checkbox.addActionListener(ae -> changeConfiguration(checkbox, cd, cid));
item.add(checkbox, BorderLayout.EAST);
}
if (cid.getType() == int.class) {
int value = Integer.parseInt(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName()));
SpinnerModel model = new SpinnerNumberModel(value, 0, Integer.MAX_VALUE, 1);
JSpinner spinner = new JSpinner(model);
Component editor = spinner.getEditor();
JFormattedTextField spinnerTextField = ((JSpinner.DefaultEditor) editor).getTextField();
spinnerTextField.setColumns(SPINNER_FIELD_WIDTH);
spinner.addChangeListener(ce -> changeConfiguration(spinner, cd, cid));
item.add(spinner, BorderLayout.EAST);
}
if (cid.getType() == String.class) {
JTextField textField = new JTextField("", TEXT_FIELD_WIDTH);
textField.setText(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName()));
textField.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
changeConfiguration(textField, cd, cid);
textField.setToolTipText(textField.getText());
}
});
textField.setToolTipText(textField.getText());
item.add(textField, BorderLayout.EAST);
}
if (cid.getType() == Color.class) {
JButton colorPicker = new JButton("Pick a color");
colorPicker.setFocusable(false);
colorPicker.setBackground(Color.decode(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName())));
colorPicker.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
final JFrame parent = new JFrame();
JColorChooser jColorChooser = new JColorChooser(Color.decode(configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName())));
jColorChooser.getSelectionModel().addChangeListener(e1 -> colorPicker.setBackground(jColorChooser.getColor()));
parent.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
changeConfiguration(jColorChooser, cd, cid);
}
});
parent.add(jColorChooser);
parent.pack();
parent.setVisible(true);
}
});
item.add(colorPicker, BorderLayout.EAST);
}
if (cid.getType() == Dimension.class) {
JPanel dimensionPanel = new JPanel();
dimensionPanel.setLayout(new BorderLayout());
String str = configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName());
String[] splitStr = str.split("x");
int width = Integer.parseInt(splitStr[0]);
int height = Integer.parseInt(splitStr[1]);
SpinnerModel widthModel = new SpinnerNumberModel(width, 0, Integer.MAX_VALUE, 1);
JSpinner widthSpinner = new JSpinner(widthModel);
Component widthEditor = widthSpinner.getEditor();
JFormattedTextField widthSpinnerTextField = ((JSpinner.DefaultEditor) widthEditor).getTextField();
widthSpinnerTextField.setColumns(4);
SpinnerModel heightModel = new SpinnerNumberModel(height, 0, Integer.MAX_VALUE, 1);
JSpinner heightSpinner = new JSpinner(heightModel);
Component heightEditor = heightSpinner.getEditor();
JFormattedTextField heightSpinnerTextField = ((JSpinner.DefaultEditor) heightEditor).getTextField();
heightSpinnerTextField.setColumns(4);
ChangeListener listener = e -> configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), widthSpinner.getValue() + "x" + heightSpinner.getValue());
widthSpinner.addChangeListener(listener);
heightSpinner.addChangeListener(listener);
dimensionPanel.add(widthSpinner, BorderLayout.WEST);
dimensionPanel.add(new JLabel(" x "), BorderLayout.CENTER);
dimensionPanel.add(heightSpinner, BorderLayout.EAST);
item.add(dimensionPanel, BorderLayout.EAST);
}
if (cid.getType().isEnum()) {
Class<? extends Enum> type = (Class<? extends Enum>) cid.getType();
JComboBox box = new JComboBox(type.getEnumConstants());
box.setFocusable(false);
// sorry but this is the way to keep the size of the combobox in check.
box.setPrototypeDisplayValue("XXXXXXXX");
try {
Enum selectedItem = Enum.valueOf(type, configManager.getConfiguration(cd.getGroup().keyName(), cid.getItem().keyName()));
box.setSelectedItem(selectedItem);
box.setToolTipText(selectedItem.toString());
} catch (IllegalArgumentException ex) {
log.debug("invalid seleced item", ex);
}
box.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
changeConfiguration(box, cd, cid);
box.setToolTipText(box.getSelectedItem().toString());
}
});
item.add(box, BorderLayout.EAST);
}
add(item);
}
JButton resetButton = new JButton("Reset");
resetButton.addActionListener((e) -> {
configManager.setDefaultConfiguration(config, true);
// Reload configuration panel
openGroupConfigPanel(config, cd, configManager);
});
add(resetButton);
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> openConfigList());
add(backButton);
revalidate();
getScrollPane().getVerticalScrollBar().setValue(0);
}
use of javax.swing.JColorChooser in project runelite by runelite.
the class ConfigPanel method changeConfiguration.
private void changeConfiguration(JComponent component, ConfigDescriptor cd, ConfigItemDescriptor cid) {
ConfigItem configItem = cid.getItem();
if (component instanceof JCheckBox) {
JCheckBox checkbox = (JCheckBox) component;
boolean originalState = !checkbox.isSelected();
boolean config = originalState ? configItem.warnOnDisable() : configItem.warnOnEnable();
if (!configItem.confirmationWarining().isEmpty() && config) {
int value = JOptionPane.showOptionDialog(component, configItem.confirmationWarining(), "Are you sure?", YES_NO_OPTION, WARNING_MESSAGE, null, new String[] { "Yes", "No" }, "No");
if (value != YES_OPTION) {
checkbox.setSelected(originalState);
return;
}
}
configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), "" + checkbox.isSelected());
}
if (component instanceof JSpinner) {
JSpinner spinner = (JSpinner) component;
configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), "" + spinner.getValue());
}
if (component instanceof JTextField) {
JTextField textField = (JTextField) component;
configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), textField.getText());
}
if (component instanceof JColorChooser) {
JColorChooser jColorChooser = (JColorChooser) component;
configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), String.valueOf(jColorChooser.getColor().getRGB()));
}
if (component instanceof JComboBox) {
JComboBox jComboBox = (JComboBox) component;
configManager.setConfiguration(cd.getGroup().keyName(), cid.getItem().keyName(), ((Enum) jComboBox.getSelectedItem()).name());
}
}
use of javax.swing.JColorChooser in project freeplane by freeplane.
the class ColorTracker method showCommonJColorChooserDialog.
public static Color showCommonJColorChooserDialog(final Component component, final String title, final Color initialColor, final Color defaultColor) {
final JColorChooser pane = ColorTracker.getCommonJColorChooser();
pane.setColor(initialColor);
final ColorTracker ok = new ColorTracker(pane);
final JDialog dialog = JColorChooser.createDialog(component, title, true, pane, ok, null);
final Container container = (Container) dialog.getContentPane().getComponent(1);
if (defaultColor != null) {
final JButton defaultBtn = new JButton(TextUtils.getText("reset_to_default"));
defaultBtn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
dialog.dispose();
ok.setColor(defaultColor);
}
});
container.add(defaultBtn);
}
dialog.addWindowListener(new Closer());
dialog.addComponentListener(new DisposeOnClose());
dialog.setVisible(true);
return ok.getColor();
}
Aggregations