Search in sources :

Example 31 with JColorChooser

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);
        }
    });
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JColorChooser(javax.swing.JColorChooser) JDialog(javax.swing.JDialog)

Example 32 with JColorChooser

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();
}
Also used : Frame(java.awt.Frame) AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JColorChooser(javax.swing.JColorChooser) LinkedList(java.util.LinkedList) JDialog(javax.swing.JDialog)

Example 33 with JColorChooser

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);
}
Also used : Color(java.awt.Color) FocusListener(java.awt.event.FocusListener) DocumentListener(javax.swing.event.DocumentListener) YES_NO_OPTION(javax.swing.JOptionPane.YES_NO_OPTION) ConfigManager(net.runelite.client.config.ConfigManager) YES_OPTION(javax.swing.JOptionPane.YES_OPTION) Map(java.util.Map) MouseAdapter(java.awt.event.MouseAdapter) ImageIO(javax.imageio.ImageIO) ConfigDescriptor(net.runelite.client.config.ConfigDescriptor) ChangeListener(javax.swing.event.ChangeListener) BorderLayout(java.awt.BorderLayout) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) WARNING_MESSAGE(javax.swing.JOptionPane.WARNING_MESSAGE) ItemEvent(java.awt.event.ItemEvent) BufferedImage(java.awt.image.BufferedImage) RuneLiteConfig(net.runelite.client.config.RuneLiteConfig) WindowAdapter(java.awt.event.WindowAdapter) Component(java.awt.Component) WindowEvent(java.awt.event.WindowEvent) Dimension(java.awt.Dimension) Slf4j(lombok.extern.slf4j.Slf4j) Config(net.runelite.client.config.Config) JCheckBox(javax.swing.JCheckBox) JPanel(javax.swing.JPanel) JColorChooser(javax.swing.JColorChooser) JTextField(javax.swing.JTextField) SpinnerNumberModel(javax.swing.SpinnerNumberModel) PluginDescriptor(net.runelite.client.plugins.PluginDescriptor) SwingConstants(javax.swing.SwingConstants) GridLayout(java.awt.GridLayout) ConfigItemDescriptor(net.runelite.client.config.ConfigItemDescriptor) ConfigItem(net.runelite.client.config.ConfigItem) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImageIcon(javax.swing.ImageIcon) SpinnerModel(javax.swing.SpinnerModel) DocumentEvent(javax.swing.event.DocumentEvent) Plugin(net.runelite.client.plugins.Plugin) JComponent(javax.swing.JComponent) JButton(javax.swing.JButton) JFormattedTextField(javax.swing.JFormattedTextField) JSpinner(javax.swing.JSpinner) IOException(java.io.IOException) JOptionPane(javax.swing.JOptionPane) PluginPanel(net.runelite.client.ui.PluginPanel) MouseEvent(java.awt.event.MouseEvent) PluginManager(net.runelite.client.plugins.PluginManager) JScrollPane(javax.swing.JScrollPane) PluginInstantiationException(net.runelite.client.plugins.PluginInstantiationException) TreeMap(java.util.TreeMap) FocusEvent(java.awt.event.FocusEvent) JLabel(javax.swing.JLabel) Comparator(java.util.Comparator) JPanel(javax.swing.JPanel) JButton(javax.swing.JButton) WindowAdapter(java.awt.event.WindowAdapter) JTextField(javax.swing.JTextField) FocusEvent(java.awt.event.FocusEvent) SpinnerModel(javax.swing.SpinnerModel) SpinnerNumberModel(javax.swing.SpinnerNumberModel) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) ChangeListener(javax.swing.event.ChangeListener) ConfigItemDescriptor(net.runelite.client.config.ConfigItemDescriptor) Component(java.awt.Component) JComponent(javax.swing.JComponent) MouseEvent(java.awt.event.MouseEvent) JComboBox(javax.swing.JComboBox) JFormattedTextField(javax.swing.JFormattedTextField) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) JCheckBox(javax.swing.JCheckBox) WindowEvent(java.awt.event.WindowEvent) JSpinner(javax.swing.JSpinner) FocusListener(java.awt.event.FocusListener) JColorChooser(javax.swing.JColorChooser)

Example 34 with JColorChooser

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());
    }
}
Also used : JCheckBox(javax.swing.JCheckBox) JComboBox(javax.swing.JComboBox) ConfigItem(net.runelite.client.config.ConfigItem) JSpinner(javax.swing.JSpinner) JTextField(javax.swing.JTextField) JColorChooser(javax.swing.JColorChooser)

Example 35 with JColorChooser

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();
}
Also used : Container(java.awt.Container) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JColorChooser(javax.swing.JColorChooser) JDialog(javax.swing.JDialog)

Aggregations

JColorChooser (javax.swing.JColorChooser)35 JDialog (javax.swing.JDialog)14 ActionEvent (java.awt.event.ActionEvent)10 ActionListener (java.awt.event.ActionListener)10 JButton (javax.swing.JButton)10 JLabel (javax.swing.JLabel)9 JPanel (javax.swing.JPanel)9 Color (java.awt.Color)7 JFrame (javax.swing.JFrame)7 AbstractColorChooserPanel (javax.swing.colorchooser.AbstractColorChooserPanel)7 BorderLayout (java.awt.BorderLayout)5 Component (java.awt.Component)5 JTextField (javax.swing.JTextField)5 JCheckBox (javax.swing.JCheckBox)4 JOptionPane (javax.swing.JOptionPane)4 JRadioButton (javax.swing.JRadioButton)4 JScrollPane (javax.swing.JScrollPane)4 Frame (java.awt.Frame)3 WindowAdapter (java.awt.event.WindowAdapter)3 WindowEvent (java.awt.event.WindowEvent)3