1

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));

     }
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vi Lobo
  • 13
  • 2
  • 2
    Unless offering the 'logical fonts' (`Font.SERIF`, `Font.MONOSPACED` etc), it'd be safer to rely on a **list of available fonts** as given by the current JRE. A good reason can be seen in 'Helvetica". I'm guessing the environment there is OS X. Here (Windows) there is no Helvetica. Arial is MS' undecorated font. For an example of determining the names of installed fonts at run time, see [this answer](https://stackoverflow.com/a/6965149/418556). – Andrew Thompson Sep 17 '19 at 10:42

1 Answers1

1

You didn't initialize your radio buttons in your class, they are null, therefore source will never be equal to one of them and always be "Helvetica".

You should change your code

JRadioButton courier = new JRadioButton("Courier");
JRadioButton times = new JRadioButton("Times New Roman");
JRadioButton helv = new JRadioButton("Helvetica");

to

courier = new JRadioButton("Courier");
times = new JRadioButton("Times New Roman");
helv = new JRadioButton("Helvetica");

in order to initialize your class fields and find the source in FontListener.

Burak Ozmen
  • 118
  • 6