Drop_of_Rain
asked on
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].getDisp layName()) ;
if (!panels[i].getDisplayName ().equals( "Swatches" ))
{
tcc.removeChooserPanel(pan els[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("Ente r the buttons new text Here.");
previewPanel = new PreviewPanel(backgroundRad ioButton);
previewPanel.setTextOnButt on(newText Field.getT ext());
colorPanel.add(previewPane l, java.awt.BorderLayout.SOUT H);
backgroundRadioButton.setS elected(tr ue);
previewPanel.setBorder(Bor derFactory .createTit ledBorder( "Preview") );
theColorChooser.setPreview Panel(prev iewPanel);
theColorChooser.add(previe wPanel, BorderLayout.SOUTH);
initFontStuff();
}
// ActionListener interface
/**
* Description of the Method
*
*@param ae Description of Parameter
*/
public void actionPerformed(ActionEven t ae) {
// Check the name of the font
if (!StyleConstants.getFontFa mily(attri butes).equ als(fontCo mbo.getSel ectedItem( ))) {
StyleConstants.setFontFami ly(attribu tes,
(String) fontCombo.getSelectedItem( ));
}
// Check the font size (no error checking yet)
if (StyleConstants.getFontSiz e(attribut es) != Integer.parseInt(fontSizeC ombo.getSe lectedItem ().toStrin g())) {
StyleConstants.setFontSize (attribute s, Integer.parseInt(fontSizeC ombo.getSe lectedItem ().toStrin g()));
}
// Check to see if the font should be bold
if (StyleConstants.isBold(att ributes) != boldCheckBox.isSelected()) {
StyleConstants.setBold(att ributes, boldCheckBox.isSelected()) ;
}
// Check to see if the font should be italic
if (StyleConstants.isItalic(a ttributes) != italicCheckBox.isSelected( )) {
StyleConstants.setItalic(a ttributes, 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.getFontFami ly(attribu tes);
boolean bold = StyleConstants.isBold(attr ibutes);
boolean ital = StyleConstants.isItalic(at tributes);
int size = StyleConstants.getFontSize (attribute s);
//Bold and italic don’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("TimesRom an");
model.addElement("Helvetic a");
model.addElement("Courier" );
fontCombo.setModel(model);
fontCombo.setSelectedIndex (1);
fontCombo.addActionListene r(this);
DefaultComboBoxModel model2 = new DefaultComboBoxModel();
model2.addElement("10");
model2.addElement("11");
model2.addElement("12");
model2.addElement("14");
fontSizeCombo.setModel(mod el2);
fontSizeCombo.setSelectedI ndex(2);
fontSizeCombo.addActionLis tener(this );
boldCheckBox.setSelected(t rue);
boldCheckBox.addActionList ener(this) ;
italicCheckBox.addActionLi stener(thi s);
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.GridBagConstraint s 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.a wt.FlowLay out.LEFT)) ;
innerPanel.setLayout(new java.awt.GridBagLayout());
textPanel.setLayout(new java.awt.BorderLayout());
textPanel.setBorder(new javax.swing.border.TitledB order("Cha nge the Button's text."));
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.setBorder(new javax.swing.border.EmptyBo rder(new java.awt.Insets(2, 2, 2, 2)));
jLabel1.setText("The button's new text: ");
jPanel1.add(jLabel1, java.awt.BorderLayout.WEST );
newTextField.addFocusListe ner(
new java.awt.event.FocusAdapte r() {
public void focusLost(java.awt.event.F ocusEvent evt) {
newTextFieldFocusLost(evt) ;
}
});
jPanel1.add(newTextField, java.awt.BorderLayout.CENT ER);
textPanel.add(jPanel1, java.awt.BorderLayout.NORT H);
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraint s.HORIZONT AL;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
innerPanel.add(textPanel, gridBagConstraints);
fontPanel.setLayout(new java.awt.FlowLayout(java.a wt.FlowLay out.LEFT)) ;
fontPanel.setBorder(new javax.swing.border.TitledB order("Cha nge the button's font"));
fontPanel.add(fontCombo);
sizeLabel.setText("Size:") ;
fontPanel.add(sizeLabel);
fontPanel.add(fontSizeComb o);
boldCheckBox.setText("Bold ");
fontPanel.add(boldCheckBox );
italicCheckBox.setText("It alic");
fontPanel.add(italicCheckB ox);
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraint s.HORIZONT AL;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
innerPanel.add(fontPanel, gridBagConstraints);
audioPanel.setLayout(new java.awt.FlowLayout(java.a wt.FlowLay out.LEFT)) ;
audioPanel.setBorder(new javax.swing.border.TitledB order("Cha nge the button's audio recording"));
audioInnerPanel.setLayout( new java.awt.BorderLayout());
audioPanel.add(audioInnerP anel);
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraint s.HORIZONT AL;
gridBagConstraints.anchor = java.awt.GridBagConstraint s.WEST;
innerPanel.add(audioPanel, gridBagConstraints);
colorPanel.setLayout(new java.awt.BorderLayout());
colorPanel.setBorder(new javax.swing.border.TitledB order("Cha nge the button's colors"));
backgroundRadioButton.setT ext("Backg round color");
buttonGroup.add(background RadioButto n);
upperPanel.add(backgroundR adioButton );
textColorRadioButton.setTe xt("Text color");
buttonGroup.add(textColorR adioButton );
upperPanel.add(textColorRa dioButton) ;
colorPanel.add(upperPanel, java.awt.BorderLayout.NORT H);
chooserPanel.setLayout(new java.awt.BorderLayout());
chooserPanel.add(theColorC hooser, java.awt.BorderLayout.CENT ER);
colorPanel.add(chooserPane l, java.awt.BorderLayout.CENT ER);
gridBagConstraints = new java.awt.GridBagConstraint s();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
innerPanel.add(colorPanel, gridBagConstraints);
centerPanel.add(innerPanel );
add(centerPanel, java.awt.BorderLayout.CENT ER);
southPanel.setLayout(new java.awt.FlowLayout(java.a wt.FlowLay out.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.SOUT H);
}
/**
* Description of the Method
*
*@param evt Description of Parameter
*/
private void newTextFieldFocusLost(java .awt.event .FocusEven t evt) {
previewPanel.setTextOnButt on(((JText Field) 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(buttonTe xt.getText ());
defaultForeground = preview.getForeground();
defaultBackground = preview.getBackground();
setLayout(new FlowLayout(FlowLayout.CENT ER, 5, 5));
add(preview);
}
}
}
//------------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
//It needs this Management constructor:
//Override the chooser panels with our own
AbstractColorChooserPanel panels[] = tcc.getChooserPanels();
for (int i=0; i<panels.length; i++)
{
System.out.println(panels[
if (!panels[i].getDisplayName
{
tcc.removeChooserPanel(pan
}
}
/*
* 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("Ente
previewPanel = new PreviewPanel(backgroundRad
previewPanel.setTextOnButt
colorPanel.add(previewPane
backgroundRadioButton.setS
previewPanel.setBorder(Bor
theColorChooser.setPreview
theColorChooser.add(previe
initFontStuff();
}
// ActionListener interface
/**
* Description of the Method
*
*@param ae Description of Parameter
*/
public void actionPerformed(ActionEven
// Check the name of the font
if (!StyleConstants.getFontFa
StyleConstants.setFontFami
(String) fontCombo.getSelectedItem(
}
// Check the font size (no error checking yet)
if (StyleConstants.getFontSiz
StyleConstants.setFontSize
}
// Check to see if the font should be bold
if (StyleConstants.isBold(att
StyleConstants.setBold(att
}
// Check to see if the font should be italic
if (StyleConstants.isItalic(a
StyleConstants.setItalic(a
}
// 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.getFontFami
boolean bold = StyleConstants.isBold(attr
boolean ital = StyleConstants.isItalic(at
int size = StyleConstants.getFontSize
//Bold and italic don’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("TimesRom
model.addElement("Helvetic
model.addElement("Courier"
fontCombo.setModel(model);
fontCombo.setSelectedIndex
fontCombo.addActionListene
DefaultComboBoxModel model2 = new DefaultComboBoxModel();
model2.addElement("10");
model2.addElement("11");
model2.addElement("12");
model2.addElement("14");
fontSizeCombo.setModel(mod
fontSizeCombo.setSelectedI
fontSizeCombo.addActionLis
boldCheckBox.setSelected(t
boldCheckBox.addActionList
italicCheckBox.addActionLi
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.GridBagConstraint
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.a
innerPanel.setLayout(new java.awt.GridBagLayout());
textPanel.setLayout(new java.awt.BorderLayout());
textPanel.setBorder(new javax.swing.border.TitledB
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel1.setBorder(new javax.swing.border.EmptyBo
jLabel1.setText("The button's new text: ");
jPanel1.add(jLabel1, java.awt.BorderLayout.WEST
newTextField.addFocusListe
new java.awt.event.FocusAdapte
public void focusLost(java.awt.event.F
newTextFieldFocusLost(evt)
}
});
jPanel1.add(newTextField, java.awt.BorderLayout.CENT
textPanel.add(jPanel1, java.awt.BorderLayout.NORT
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraint
gridBagConstraints.anchor = java.awt.GridBagConstraint
innerPanel.add(textPanel, gridBagConstraints);
fontPanel.setLayout(new java.awt.FlowLayout(java.a
fontPanel.setBorder(new javax.swing.border.TitledB
fontPanel.add(fontCombo);
sizeLabel.setText("Size:")
fontPanel.add(sizeLabel);
fontPanel.add(fontSizeComb
boldCheckBox.setText("Bold
fontPanel.add(boldCheckBox
italicCheckBox.setText("It
fontPanel.add(italicCheckB
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraint
gridBagConstraints.anchor = java.awt.GridBagConstraint
innerPanel.add(fontPanel, gridBagConstraints);
audioPanel.setLayout(new java.awt.FlowLayout(java.a
audioPanel.setBorder(new javax.swing.border.TitledB
audioInnerPanel.setLayout(
audioPanel.add(audioInnerP
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraint
gridBagConstraints.anchor = java.awt.GridBagConstraint
innerPanel.add(audioPanel,
colorPanel.setLayout(new java.awt.BorderLayout());
colorPanel.setBorder(new javax.swing.border.TitledB
backgroundRadioButton.setT
buttonGroup.add(background
upperPanel.add(backgroundR
textColorRadioButton.setTe
buttonGroup.add(textColorR
upperPanel.add(textColorRa
colorPanel.add(upperPanel,
chooserPanel.setLayout(new
chooserPanel.add(theColorC
colorPanel.add(chooserPane
gridBagConstraints = new java.awt.GridBagConstraint
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
innerPanel.add(colorPanel,
centerPanel.add(innerPanel
add(centerPanel, java.awt.BorderLayout.CENT
southPanel.setLayout(new java.awt.FlowLayout(java.a
buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 6, 0));
saveButton.setText("Save")
buttonPanel.add(saveButton
southPanel.add(buttonPanel
add(southPanel, java.awt.BorderLayout.SOUT
}
/**
* Description of the Method
*
*@param evt Description of Parameter
*/
private void newTextFieldFocusLost(java
previewPanel.setTextOnButt
}
// 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(buttonTe
defaultForeground = preview.getForeground();
defaultBackground = preview.getBackground();
setLayout(new FlowLayout(FlowLayout.CENT
add(preview);
}
}
}
//------------------------
//It needs this Management constructor:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
import should be:
import javax.swing.colorchooser.A bstractCol orChooserP anel;
import javax.swing.colorchooser.A
ASKER
OOPS. Thanks that takes care of it.
ASKER
trainer/ButtonMgrPanel.jav
symbol : class AbstractColorChooserPanel
location: class trainer.ButtonMgrPanel
AbstractColorChooserPanel panels[] = theColorChooser.getChooser
^
1 error
Errors compiling ButtonMgrPanel.