Solved

Modify this colorchooser

Posted on 2004-08-01
5
255 Views
Last Modified: 2013-11-23
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
Comment
Question by:Drop_of_Rain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 11690273
    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
 

Author Comment

by:Drop_of_Rain
ID: 11690301
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
 
LVL 92

Expert Comment

by:objects
ID: 11690308
import should be:

import javax.swing.colorchooser.AbstractColorChooserPanel;
0
 

Author Comment

by:Drop_of_Rain
ID: 11690312
OOPS. Thanks that takes care of it.
0
 
LVL 92

Expert Comment

by:objects
ID: 11690320
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 87
Facing this issue for maven proxy setting 2 27
ejb message driven bean mdb creation steps 2 37
import as existing maven project 3 42
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question