I am working on a program for class and am having trouble getting a set of radio buttons to change the font of a JLabel. I have JCheckBoxes that work for bold and italics, however the font radio buttons do not work. Is there a reload or repaint method I must call for the font to update? I am able to change the font by using similar code in the StyleListener method, so I have a feeling the issue lies in the FontListener method. I have tried debugging through the code as well and it seems as if the code in the FontListener method doesn't actually do anything. What am I missing?
//********************************************************************
// StyleOptionsPanel.java adapted from Java Foundations
//
// Demonstrates the use of check boxes.
//********************************************************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StyleOptionsPanel extends JPanel
{
private JLabel saying;
private JCheckBox bold, italic;
private JRadioButton courier, times, helv;
private ButtonGroup fonts;
private int style = Font.PLAIN;
private String typeFace = "Helvetica";
//-----------------------------------------------------------------
// Sets up a panel with a label and some check boxes that
// control the style of the label's font.
//-----------------------------------------------------------------
public StyleOptionsPanel()
{
saying = new JLabel ("Say it with style!");
saying.setFont (new Font (typeFace, style, 20));
bold = new JCheckBox ("Bold");
bold.setBackground (Color.cyan);
italic = new JCheckBox ("Italic");
italic.setBackground (Color.cyan);
JRadioButton courier = new JRadioButton("Courier");
JRadioButton times = new JRadioButton("Times New Roman");
JRadioButton helv = new JRadioButton("Helvetica");
ButtonGroup fonts = new ButtonGroup();
fonts.add(courier);
fonts.add(times);
fonts.add(helv);
FontListener listener1 = new FontListener();
courier.addActionListener(listener1);
times.addActionListener(listener1);
helv.addActionListener(listener1);
StyleListener listener = new StyleListener();
bold.addItemListener (listener);
italic.addItemListener (listener);
add(courier);
add(times);
add(helv);
add (saying);
add (bold);
add (italic);
setBackground (Color.cyan);
setPreferredSize (new Dimension(300, 100));
}
//*****************************************************************
// Represents the listener for both check boxes.
//*****************************************************************
private class StyleListener implements ItemListener
{
//--------------------------------------------------------------
// Updates the style of the label font style.
//--------------------------------------------------------------
public void itemStateChanged (ItemEvent event)
{
style = Font.PLAIN;
if (bold.isSelected())
style = Font.BOLD;
if (italic.isSelected())
style += Font.ITALIC;
saying.setFont (new Font (typeFace, style, 20));
}
}
private class FontListener implements ActionListener{
// Updates the font of the String variable saying
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
typeFace = "Helvetica";
if(source == courier) typeFace = "Courier";
else if(source == times) typeFace = "Times New Roman";
else typeFace = "Helvetica";
saying.setFont(new Font(typeFace, style, 20));
}
}
}