• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Modify this colorchooser

How would I modify this colorchooser in this class with this code:

        //Override the chooser panels with our own
        AbstractColorChooserPanel panels[] = tcc.getChooserPanels();
        for (int i=0; i<panels.length; i++)
        {
               System.out.println(panels[i].getDisplayName());
             if (!panels[i].getDisplayName().equals("Swatches"))
             {
               tcc.removeChooserPanel(panels[i]);
             }
       }






/*
 *  ButtonMgrPanel.java
 *
 */
package trainer;

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;

/**
 *  Description of the Class
 *
 *@author    Chris Seguin
 */
public class ButtonMgrPanel extends JPanel implements ActionListener {

      private SimpleAttributeSet attributes = new SimpleAttributeSet();
      private PreviewPanel previewPanel;

      // Variables declaration - do not modify
      private javax.swing.JPanel audioInnerPanel;
      private javax.swing.JPanel audioPanel;
      private javax.swing.JRadioButton backgroundRadioButton;
      private javax.swing.JCheckBox boldCheckBox;
      private javax.swing.ButtonGroup buttonGroup;
      private javax.swing.JPanel buttonPanel;
      private javax.swing.JPanel centerPanel;
      private javax.swing.JPanel chooserPanel;
      private javax.swing.JPanel colorPanel;
      private javax.swing.JComboBox fontCombo;
      private javax.swing.JPanel fontPanel;
      private javax.swing.JComboBox fontSizeCombo;
      private javax.swing.JPanel innerPanel;
      private javax.swing.JCheckBox italicCheckBox;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JTextField newTextField;
      private javax.swing.JButton saveButton;
      private javax.swing.JLabel sizeLabel;
      private javax.swing.JPanel southPanel;
      private javax.swing.JRadioButton textColorRadioButton;
      private javax.swing.JPanel textPanel;
      private javax.swing.JColorChooser theColorChooser;
      private javax.swing.JPanel upperPanel;


      /**
       *  Creates new form ButtonMgrPanel
       */
      public ButtonMgrPanel() {
            initComponents();
            audioInnerPanel.add(new AudioManagerPanel());
            newTextField.setText("Enter the buttons new text Here.");

            previewPanel = new PreviewPanel(backgroundRadioButton);
            previewPanel.setTextOnButton(newTextField.getText());
            colorPanel.add(previewPanel, java.awt.BorderLayout.SOUTH);

            backgroundRadioButton.setSelected(true);
            previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
            theColorChooser.setPreviewPanel(previewPanel);
            theColorChooser.add(previewPanel, BorderLayout.SOUTH);

            initFontStuff();
      }


      // ActionListener interface
      /**
       *  Description of the Method
       *
       *@param  ae  Description of Parameter
       */
      public void actionPerformed(ActionEvent ae) {
            // Check the name of the font
            if (!StyleConstants.getFontFamily(attributes).equals(fontCombo.getSelectedItem())) {
                  StyleConstants.setFontFamily(attributes,
                              (String) fontCombo.getSelectedItem());
            }
            // Check the font size (no error checking yet)
            if (StyleConstants.getFontSize(attributes) != Integer.parseInt(fontSizeCombo.getSelectedItem().toString())) {
                  StyleConstants.setFontSize(attributes, Integer.parseInt(fontSizeCombo.getSelectedItem().toString()));
            }
            // Check to see if the font should be bold
            if (StyleConstants.isBold(attributes) != boldCheckBox.isSelected()) {
                  StyleConstants.setBold(attributes, boldCheckBox.isSelected());
            }
            // Check to see if the font should be italic
            if (StyleConstants.isItalic(attributes) != italicCheckBox.isSelected()) {
                  StyleConstants.setItalic(attributes, italicCheckBox.isSelected());
            }
            // and update our preview label
            updatePreviewFont();
      }


      // Get the appropriate font from our attributes object and update the preview label
      /**
       *  Description of the Method
       */
      protected void updatePreviewFont() {
            String name = StyleConstants.getFontFamily(attributes);
            boolean bold = StyleConstants.isBold(attributes);
            boolean ital = StyleConstants.isItalic(attributes);
            int size = StyleConstants.getFontSize(attributes);

            //Bold and italic donĂ¢&#8364;&#8482;t work properly in beta 4.
            Font f = new Font(name, (bold ? Font.BOLD : 0) + (ital ? Font.ITALIC : 0), size);
            previewPanel.setFont(f);
            //    previewLabel.setFont(f);
      }


      /**
       *  Description of the Method
       */
      private void initFontStuff() {
            DefaultComboBoxModel model = new DefaultComboBoxModel();
            model.addElement("TimesRoman");
            model.addElement("Helvetica");
            model.addElement("Courier");
            fontCombo.setModel(model);
            fontCombo.setSelectedIndex(1);
            fontCombo.addActionListener(this);

            DefaultComboBoxModel model2 = new DefaultComboBoxModel();
            model2.addElement("10");
            model2.addElement("11");
            model2.addElement("12");
            model2.addElement("14");
            fontSizeCombo.setModel(model2);
            fontSizeCombo.setSelectedIndex(2);
            fontSizeCombo.addActionListener(this);

            boldCheckBox.setSelected(true);
            boldCheckBox.addActionListener(this);
            italicCheckBox.addActionListener(this);

            actionPerformed(null);
      }


      /**
       *  This method is called from within the constructor to initialize the form.
       *  WARNING: Do NOT modify this code. The content of this method is always
       *  regenerated by the Form Editor.
       */
      private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;

            buttonGroup = new javax.swing.ButtonGroup();
            centerPanel = new javax.swing.JPanel();
            innerPanel = new javax.swing.JPanel();
            textPanel = new javax.swing.JPanel();
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            newTextField = new javax.swing.JTextField();
            fontPanel = new javax.swing.JPanel();
            fontCombo = new javax.swing.JComboBox();
            sizeLabel = new javax.swing.JLabel();
            fontSizeCombo = new javax.swing.JComboBox();
            boldCheckBox = new javax.swing.JCheckBox();
            italicCheckBox = new javax.swing.JCheckBox();
            audioPanel = new javax.swing.JPanel();
            audioInnerPanel = new javax.swing.JPanel();
            colorPanel = new javax.swing.JPanel();
            upperPanel = new javax.swing.JPanel();
            backgroundRadioButton = new javax.swing.JRadioButton();
            textColorRadioButton = new javax.swing.JRadioButton();
            chooserPanel = new javax.swing.JPanel();
            theColorChooser = new javax.swing.JColorChooser();
            southPanel = new javax.swing.JPanel();
            buttonPanel = new javax.swing.JPanel();
            saveButton = new javax.swing.JButton();

            setLayout(new java.awt.BorderLayout());

            centerPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

            innerPanel.setLayout(new java.awt.GridBagLayout());

            textPanel.setLayout(new java.awt.BorderLayout());

            textPanel.setBorder(new javax.swing.border.TitledBorder("Change the Button's text."));
            jPanel1.setLayout(new java.awt.BorderLayout());

            jPanel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(2, 2, 2, 2)));
            jLabel1.setText("The button's new text:  ");
            jPanel1.add(jLabel1, java.awt.BorderLayout.WEST);

            newTextField.addFocusListener(
                              new java.awt.event.FocusAdapter() {
                                    public void focusLost(java.awt.event.FocusEvent evt) {
                                          newTextFieldFocusLost(evt);
                                    }
                              });

            jPanel1.add(newTextField, java.awt.BorderLayout.CENTER);

            textPanel.add(jPanel1, java.awt.BorderLayout.NORTH);

            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
            innerPanel.add(textPanel, gridBagConstraints);

            fontPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

            fontPanel.setBorder(new javax.swing.border.TitledBorder("Change the button's font"));
            fontPanel.add(fontCombo);

            sizeLabel.setText("Size:");
            fontPanel.add(sizeLabel);

            fontPanel.add(fontSizeCombo);

            boldCheckBox.setText("Bold");
            fontPanel.add(boldCheckBox);

            italicCheckBox.setText("Italic");
            fontPanel.add(italicCheckBox);

            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
            innerPanel.add(fontPanel, gridBagConstraints);

            audioPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

            audioPanel.setBorder(new javax.swing.border.TitledBorder("Change the button's audio recording"));
            audioInnerPanel.setLayout(new java.awt.BorderLayout());

            audioPanel.add(audioInnerPanel);

            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 3;
            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
            gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
            innerPanel.add(audioPanel, gridBagConstraints);

            colorPanel.setLayout(new java.awt.BorderLayout());

            colorPanel.setBorder(new javax.swing.border.TitledBorder("Change the button's colors"));
            backgroundRadioButton.setText("Background color");
            buttonGroup.add(backgroundRadioButton);
            upperPanel.add(backgroundRadioButton);

            textColorRadioButton.setText("Text color");
            buttonGroup.add(textColorRadioButton);
            upperPanel.add(textColorRadioButton);

            colorPanel.add(upperPanel, java.awt.BorderLayout.NORTH);

            chooserPanel.setLayout(new java.awt.BorderLayout());

            chooserPanel.add(theColorChooser, java.awt.BorderLayout.CENTER);

            colorPanel.add(chooserPanel, java.awt.BorderLayout.CENTER);

            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 4;
            innerPanel.add(colorPanel, gridBagConstraints);

            centerPanel.add(innerPanel);

            add(centerPanel, java.awt.BorderLayout.CENTER);

            southPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

            buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 6, 0));

            saveButton.setText("Save");
            buttonPanel.add(saveButton);

            southPanel.add(buttonPanel);

            add(southPanel, java.awt.BorderLayout.SOUTH);

      }


      /**
       *  Description of the Method
       *
       *@param  evt  Description of Parameter
       */
      private void newTextFieldFocusLost(java.awt.event.FocusEvent evt) {
            previewPanel.setTextOnButton(((JTextField) evt.getSource()).getText());
      }

      // End of variables declaration

      /**
       *  Description of the Class
       *
       *@author    Chris Seguin
       */
      class PreviewPanel extends JPanel {
            JRadioButton background;
            JButton preview;
            Color defaultForeground;
            Color defaultBackground;


            /**
             *  Constructor for the PreviewPanel object
             *
             *@param  background  Description of Parameter
             */
            public PreviewPanel(JRadioButton background) {
                  this.background = background;
                  init();
            }


            /**
             *  Sets the Foreground attribute of the PreviewPanel object
             *
             *@param  fg  The new Foreground value
             */
            public void setForeground(Color fg) {
                  try {
                        if (background.isSelected()) {
                              preview.setBackground(fg);
                        }
                        else {
                              preview.setForeground(fg);
                        }
                  }
                  catch (Exception e) {
                  }
            }


            /**
             *  Sets the TextOnButton attribute of the PreviewPanel object
             *
             *@param  str  The new TextOnButton value
             */
            public void setTextOnButton(String str) {
                  preview.setText(str);
            }


            /**
             *  Sets the Font attribute of the PreviewPanel object
             *
             *@param  font  The new Font value
             */
            public void setFont(Font font) {
                  super.setFont(font);
                  try {
                        preview.setFont(font);
                  }
                  catch (Exception e) {
                  }
            }


            /**
             *  Description of the Method
             */
            protected void init() {
                  preview = new JButton("Here goes the new button text");
                  //preview.setText(buttonText.getText());
                  defaultForeground = preview.getForeground();
                  defaultBackground = preview.getBackground();
                  setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                  add(preview);
            }
      }

}

//--------------------------------------------------------------------------------------------------
//It needs this Management constructor:
0
Drop_of_Rain
Asked:
Drop_of_Rain
  • 3
  • 2
1 Solution
 
objectsCommented:
    public ButtonMgrPanel() {
          initComponents();
          audioInnerPanel.add(new AudioManagerPanel());
          newTextField.setText("Enter the buttons new text Here.");

          previewPanel = new PreviewPanel(backgroundRadioButton);
          previewPanel.setTextOnButton(newTextField.getText());
          colorPanel.add(previewPanel, java.awt.BorderLayout.SOUTH);

          backgroundRadioButton.setSelected(true);
          previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
          AbstractColorChooserPanel panels[] = theColorChooser.getChooserPanels();
          for (int i=0; i<panels.length; i++)
          {
             if (!panels[i].getDisplayName().equals("Swatches"))
             {
               theColorChooser.removeChooserPanel(panels[i]);
             }
          }
          theColorChooser.setPreviewPanel(previewPanel);
          theColorChooser.add(previewPanel, BorderLayout.SOUTH);

          initFontStuff();
     }
0
 
Drop_of_RainAuthor Commented:
I get this error: I added this: import javax.swing.colorchooser.AbstractColorChooserPanel.*;

trainer/ButtonMgrPanel.java [65:1] cannot resolve symbol
symbol  : class AbstractColorChooserPanel
location: class trainer.ButtonMgrPanel
          AbstractColorChooserPanel panels[] = theColorChooser.getChooserPanels();
          ^
1 error
Errors compiling ButtonMgrPanel.

0
 
objectsCommented:
import should be:

import javax.swing.colorchooser.AbstractColorChooserPanel;
0
 
Drop_of_RainAuthor Commented:
OOPS. Thanks that takes care of it.
0
 
objectsCommented:
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now