Search in sources :

Example 6 with AbstractColorChooserPanel

use of javax.swing.colorchooser.AbstractColorChooserPanel in project jdk8u_jdk by JetBrains.

the class Test6524757 method create.

private static Object[] create() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    // show color chooser
    JColorChooser chooser = new JColorChooser();
    JDialog dialog = JColorChooser.createDialog(frame, null, false, chooser, null, null);
    dialog.setVisible(true);
    // process all values
    List<Object> list = new ArrayList<Object>(KEYS.length);
    Component component = getC(getC(dialog.getLayeredPane(), 0), 1);
    AbstractButton ok = (AbstractButton) getC(component, 0);
    AbstractButton cancel = (AbstractButton) getC(component, 1);
    AbstractButton reset = (AbstractButton) getC(component, 2);
    list.add(ok.getText());
    list.add(cancel.getText());
    list.add(reset.getText());
    list.add(Integer.valueOf(reset.getMnemonic()));
    for (int i = 0; i < 5; i++) {
        AbstractColorChooserPanel panel = (AbstractColorChooserPanel) getC(getC(getC(chooser, 0), i), 0);
        list.add(panel.getDisplayName());
        list.add(Integer.valueOf(panel.getMnemonic()));
        if (i == 0) {
            JLabel label = (JLabel) getC(getC(panel, 0), 1);
            JPanel upper = (JPanel) getC(getC(getC(panel, 0), 0), 0);
            JPanel lower = (JPanel) getC(getC(getC(panel, 0), 2), 0);
            addSize(list, upper, 1, 1, 31, 9);
            list.add(label.getText());
            addSize(list, lower, 1, 1, 5, 7);
        } else {
            Component container = getC(panel, 0);
            for (int j = 0; j < 3; j++) {
                AbstractButton button = (AbstractButton) getC(container, j);
                list.add(button.getText());
            }
            JLabel label = (JLabel) getC(container, 3);
            list.add(label.getText());
            if (i == 4) {
                label = (JLabel) getC(container, 4);
                list.add(label.getText());
            }
            if (i == 3) {
                label = (JLabel) getC(panel, 1);
                list.add(label.getText());
                list.add(Integer.valueOf(label.getDisplayedMnemonic()));
            }
        }
    }
    // close dialog
    dialog.setVisible(false);
    dialog.dispose();
    // close frame
    frame.setVisible(false);
    frame.dispose();
    return list.toArray();
}
Also used : JPanel(javax.swing.JPanel) AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) AbstractButton(javax.swing.AbstractButton) JFrame(javax.swing.JFrame) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) JComponent(javax.swing.JComponent) Component(java.awt.Component) JColorChooser(javax.swing.JColorChooser) JDialog(javax.swing.JDialog)

Example 7 with AbstractColorChooserPanel

use of javax.swing.colorchooser.AbstractColorChooserPanel in project jdk8u_jdk by JetBrains.

the class Test4177735 method main.

public static void main(String[] args) throws Exception {
    JColorChooser chooser = new JColorChooser();
    AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
    chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });
    JDialog dialog = show(chooser);
    pause(DELAY);
    dialog.dispose();
    pause(DELAY);
    Test4177735 test = new Test4177735();
    SwingUtilities.invokeAndWait(test);
    if (test.count != 0) {
        throw new Error("JColorChooser leaves " + test.count + " threads running");
    }
}
Also used : AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) JColorChooser(javax.swing.JColorChooser) JDialog(javax.swing.JDialog)

Example 8 with AbstractColorChooserPanel

use of javax.swing.colorchooser.AbstractColorChooserPanel in project CCDD by nasa.

the class CcddDialogHandler method getColorChoicePanel.

/**
 ********************************************************************************************
 * Create a color choice and preview panel
 *
 * @param chooser
 *            reference to a JColorChooser
 *
 * @param initialColor
 *            color initially selected when the dialog appears
 *
 * @return Reference to a JPanel containing the color choice and preview panels
 ********************************************************************************************
 */
protected JPanel getColorChoicePanel(final JColorChooser chooser, Color initialColor) {
    // Create a panel to hold the color preview text and color boxes
    JPanel previewPanel = new JPanel(new BorderLayout());
    JLabel previewLabel = new JLabel("Preview", SwingConstants.CENTER);
    previewLabel.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
    // Calculate the size of the color preview boxes based on the width of the preview label
    // text
    int previewWidth = (int) previewLabel.getFontMetrics(ModifiableFontInfo.LABEL_BOLD.getFont()).getStringBounds(previewLabel.getText(), previewLabel.getGraphics()).getWidth() / 2 - 1;
    Dimension previewSize = new Dimension(previewWidth, previewWidth);
    // Create a panel to show the original color
    JPanel initialPanel = new JPanel();
    initialPanel.setOpaque(true);
    initialPanel.setBackground(initialColor);
    initialPanel.setPreferredSize(previewSize);
    // Create a panel to show the new color
    final JPanel newPanel = new JPanel();
    newPanel.setOpaque(true);
    newPanel.setBackground(initialColor);
    newPanel.setPreferredSize(previewSize);
    // Add the preview text and color boxes to the preview panel
    previewPanel.add(previewLabel, BorderLayout.PAGE_START);
    previewPanel.add(initialPanel, BorderLayout.LINE_START);
    previewPanel.add(newPanel, BorderLayout.LINE_END);
    // Add a listener for changes to the preview panel in order to capture color change events.
    // These are generated when the user changes the color in the color chooser panel
    previewPanel.addPropertyChangeListener("foreground", new PropertyChangeListener() {

        /**
         ************************************************************************************
         * Handle foreground color change events
         ************************************************************************************
         */
        @Override
        public void propertyChange(PropertyChangeEvent pce) {
            // Update the preview color to the current choice
            newPanel.setBackground(chooser.getColor());
        }
    });
    // Set the color chooser's color preview panel
    chooser.setPreviewPanel(previewPanel);
    // Create the panel for holding the color chooser(s) and the preview panel
    JPanel colorPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
    // Get the color chooser panels available to the current look & feel
    AbstractColorChooserPanel[] chooserPanel = chooser.getChooserPanels();
    // Check if there's only a single color chooser panel
    if (chooserPanel.length == 1) {
        // Add the color chooser panel to the dialog panel
        colorPanel.add(chooserPanel[0]);
    } else // There's more than one color chooser panel
    {
        // Create a tabbed pane
        JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
        tabbedPane.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        colorPanel.add(tabbedPane);
        // Create a border for the tabbed panes
        Border border = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        // Step through the available color chooser panels
        for (int index = 0; index < chooserPanel.length; index++) {
            // Create a panel to hold the chooser so that it's centered in the tab panel
            JPanel pnl = new JPanel(new GridBagLayout());
            pnl.setBorder(border);
            pnl.add(chooserPanel[index]);
            // Set the name, component, and tool tip text (if extant) of the tab for each color
            // chooser panel
            tabbedPane.addTab(chooserPanel[index].getDisplayName(), null, pnl, chooserPanel[index].getToolTipText());
        }
        // Add the color preview panel
        colorPanel.add(previewPanel);
    }
    return colorPanel;
}
Also used : JPanel(javax.swing.JPanel) PropertyChangeEvent(java.beans.PropertyChangeEvent) FlowLayout(java.awt.FlowLayout) PropertyChangeListener(java.beans.PropertyChangeListener) GridBagLayout(java.awt.GridBagLayout) JTabbedPane(javax.swing.JTabbedPane) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Point(java.awt.Point) AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) BorderLayout(java.awt.BorderLayout) Border(javax.swing.border.Border) EtchedBorder(javax.swing.border.EtchedBorder)

Example 9 with AbstractColorChooserPanel

use of javax.swing.colorchooser.AbstractColorChooserPanel 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 10 with AbstractColorChooserPanel

use of javax.swing.colorchooser.AbstractColorChooserPanel in project gate-core by GateNLP.

the class TextAttributesChooser method jbInit.

// public TextAttributesChooser()
void jbInit() throws Exception {
    sampleText = new JTextPane();
    sampleText.setText("Type your own sample here...");
    if (currentStyle == null) {
        StyleContext context = new StyleContext();
        currentStyle = context.addStyle(null, null);
        currentStyle.addAttributes(sampleText.getInputAttributes());
    }
    // The overall organisation is:
    // First level
    // Font Tab
    // Foreground colour
    // Background colour
    // Second level
    // Sample text
    // Third level
    // Ok Button
    // Cancel Button
    Box contents = Box.createVerticalBox();
    // FIRST LEVEL
    JTabbedPane firstLevel = new JTabbedPane();
    // Font stuff
    Box fontBox = Box.createVerticalBox();
    fontFamilyCombo = new JComboBox<String>(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
    fontFamilyCombo.setSelectedItem(StyleConstants.getFontFamily(currentStyle));
    fontSizeCombo = new JComboBox<String>(new String[] { "6", "8", "10", "12", "14", "16", "18", "20", "22", "24", "26" });
    fontSizeCombo.setSelectedItem(new Integer(StyleConstants.getFontSize(currentStyle)).toString());
    fontSizeCombo.setEditable(true);
    JPanel box = new JPanel();
    box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
    box.add(fontFamilyCombo);
    box.add(Box.createHorizontalStrut(5));
    box.add(fontSizeCombo);
    box.add(Box.createHorizontalGlue());
    box.setBorder(BorderFactory.createTitledBorder("Font"));
    fontBox.add(box);
    box = new JPanel();
    box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
    // First column
    Box box1 = Box.createVerticalBox();
    boldChk = new JCheckBox("<html><b>Bold</b></html>");
    boldChk.setSelected(StyleConstants.isBold(currentStyle));
    box1.add(boldChk);
    // italicChk = new JCheckBox("<html><i>Italic</i></html>");
    // italicChk.setSelected(StyleConstants.isItalic(currentStyle));
    // box1.add(italicChk);
    underlineChk = new JCheckBox("<html><u>Underline</u></html>");
    underlineChk.setSelected(StyleConstants.isUnderline(currentStyle));
    // box1.add(underlineChk);
    box.add(box1);
    // Second column
    box1 = Box.createVerticalBox();
    italicChk = new JCheckBox("<html><i>Italic</i></html>");
    italicChk.setSelected(StyleConstants.isItalic(currentStyle));
    box1.add(italicChk);
    subscriptChk = new JCheckBox("<html>T<sub>Subscript</sub></html>");
    subscriptChk.setSelected(StyleConstants.isSubscript(currentStyle));
    // box1.add(subscriptChk);
    superscriptChk = new JCheckBox("<html>T<sup>Superscript</sup></html>");
    superscriptChk.setSelected(StyleConstants.isSuperscript(currentStyle));
    // box1.add(superscriptChk);
    strikethroughChk = new JCheckBox("<html><strike>Strikethrough</strike></html>");
    strikethroughChk.setSelected(StyleConstants.isStrikeThrough(currentStyle));
    // box1.add(strikethroughChk);
    box.add(box1);
    box.add(Box.createHorizontalGlue());
    box.setBorder(BorderFactory.createTitledBorder("Effects"));
    fontBox.add(box);
    // Use colors checkboxes
    box = new JPanel();
    box.setLayout(new BoxLayout(box, BoxLayout.X_AXIS));
    useForegroundChk = new JCheckBox("Use foreground colour");
    useForegroundChk.setSelected(false);
    box.add(useForegroundChk);
    useBackgroundChk = new JCheckBox("Use background colour");
    useBackgroundChk.setSelected(false);
    box.add(useBackgroundChk);
    box.add(Box.createHorizontalGlue());
    box.setBorder(BorderFactory.createTitledBorder("Use Colours"));
    fontBox.add(box);
    fontBox.add(Box.createVerticalGlue());
    firstLevel.add("Font", fontBox);
    // Colors stuff
    fgChooser = new JColorChooser(StyleConstants.getForeground(currentStyle));
    JTabbedPane tp = new JTabbedPane();
    AbstractColorChooserPanel[] panels = fgChooser.getChooserPanels();
    for (int i = 0; i < panels.length; i++) {
        tp.add(panels[i].getDisplayName(), panels[i]);
    }
    firstLevel.add("Foreground", tp);
    bgChooser = new JColorChooser(StyleConstants.getBackground(currentStyle));
    tp = new JTabbedPane();
    panels = bgChooser.getChooserPanels();
    for (int i = 0; i < panels.length; i++) {
        tp.add(panels[i].getDisplayName(), panels[i]);
    }
    firstLevel.add("Background", tp);
    contents.add(firstLevel);
    // SECOND LEVEL
    JPanel secondLevel = new JPanel();
    secondLevel.setBorder(BorderFactory.createTitledBorder("Sample"));
    // Sample text
    JScrollPane scroller = new JScrollPane(sampleText);
    scroller.setPreferredSize(new Dimension(400, 50));
    secondLevel.add(scroller);
    secondLevel.add(Box.createHorizontalGlue());
    contents.add(secondLevel);
    // THIRD LEVEL
    // Buttons
    Box thirdLevel = Box.createHorizontalBox();
    okButton = new JButton("OK");
    thirdLevel.add(okButton);
    cancelButton = new JButton("Cancel");
    thirdLevel.add(cancelButton);
    contents.add(thirdLevel);
    getContentPane().add(contents, BorderLayout.CENTER);
    fontFamilyCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            StyleConstants.setFontFamily(currentStyle, (String) fontFamilyCombo.getSelectedItem());
            updateSample();
        }
    });
    fontSizeCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Integer.parseInt((String) fontSizeCombo.getSelectedItem());
            } catch (NumberFormatException nfe) {
                fontSizeCombo.setSelectedIndex(3);
            }
            StyleConstants.setFontSize(currentStyle, Integer.parseInt((String) fontSizeCombo.getSelectedItem()));
            updateSample();
        }
    });
    boldChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            StyleConstants.setBold(currentStyle, boldChk.isSelected());
            updateSample();
        }
    });
    italicChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            StyleConstants.setItalic(currentStyle, italicChk.isSelected());
            updateSample();
        }
    });
    underlineChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (underlineChk.isSelected())
                strikethroughChk.setSelected(false);
            StyleConstants.setUnderline(currentStyle, underlineChk.isSelected());
            updateSample();
        }
    });
    strikethroughChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (strikethroughChk.isSelected())
                underlineChk.setSelected(false);
            StyleConstants.setStrikeThrough(currentStyle, strikethroughChk.isSelected());
            updateSample();
        }
    });
    superscriptChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (superscriptChk.isSelected())
                subscriptChk.setSelected(false);
            StyleConstants.setSuperscript(currentStyle, superscriptChk.isSelected());
            updateSample();
        }
    });
    subscriptChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (subscriptChk.isSelected())
                superscriptChk.setSelected(false);
            StyleConstants.setSubscript(currentStyle, subscriptChk.isSelected());
            updateSample();
        }
    });
    fgChooser.getSelectionModel().addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            StyleConstants.setForeground(currentStyle, fgChooser.getColor());
            useForegroundChk.setSelected(true);
            updateSample();
        }
    });
    useForegroundChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (useForegroundChk.isSelected()) {
                StyleConstants.setForeground(currentStyle, fgChooser.getColor());
            } else {
                currentStyle.removeAttribute(StyleConstants.Foreground);
            }
            updateSample();
        }
    });
    bgChooser.getSelectionModel().addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            StyleConstants.setBackground(currentStyle, bgChooser.getColor());
            useBackgroundChk.setSelected(true);
            updateSample();
        }
    });
    useBackgroundChk.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (useBackgroundChk.isSelected()) {
                StyleConstants.setBackground(currentStyle, bgChooser.getColor());
            } else {
                currentStyle.removeAttribute(StyleConstants.Background);
            }
            updateSample();
        }
    });
    this.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentShown(ComponentEvent e) {
            updateSample();
        }
    });
    okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            choice = true;
            setVisible(false);
        }
    });
    cancelButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            choice = false;
            setVisible(false);
        }
    });
}
Also used : AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) ChangeListener(javax.swing.event.ChangeListener) ChangeEvent(javax.swing.event.ChangeEvent)

Aggregations

AbstractColorChooserPanel (javax.swing.colorchooser.AbstractColorChooserPanel)11 JColorChooser (javax.swing.JColorChooser)7 JDialog (javax.swing.JDialog)4 JLabel (javax.swing.JLabel)3 Frame (java.awt.Frame)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 JPanel (javax.swing.JPanel)2 ChangeEvent (javax.swing.event.ChangeEvent)2 ChangeListener (javax.swing.event.ChangeListener)2 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1 Component (java.awt.Component)1 Dimension (java.awt.Dimension)1 FlowLayout (java.awt.FlowLayout)1 Font (java.awt.Font)1 GridBagLayout (java.awt.GridBagLayout)1 Paint (java.awt.Paint)1