Hi, I'm working on a program with GUI and I'm having a fair amount of trouble with the following code. As you can see below, I have an application and a support class.

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, timesnr, helvetica;
   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");
      courier.setFont (new Font ("Courier", style, 10));
      JRadioButton timesnr = new JRadioButton ("Times New Roman");
      timesnr.setFont (new Font ("Times New Roman", style, 10));
      JRadioButton helvetica = new JRadioButton ("Helvetica");
      helvetica.setFont (new Font (typeFace, style, 10));
      
      StyleListener listener = new StyleListener();
      bold.addItemListener (listener);
      italic.addItemListener (listener);
      courier.addItemListener (listener);
      timesnr.addItemListener (listener);
      helvetica.addItemListener (listener);

      add (saying);
      add (bold);
      add (italic);
      add (courier);
      add (timesnr);
      add (helvetica);

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

And the application class:

import javax.swing.JFrame;

public class StyleOptions
{
   //-----------------------------------------------------------------
   //  Creates and presents the program frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Style Options");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      StyleOptionsPanel panel = new StyleOptionsPanel();
      frame.getContentPane().add (panel);

      frame.pack();
      frame.setVisible(true);
   }
}

The aim of what I am trying to do is add three functioning JRadioButtons which will offer three different typefaces (I have added them but can't seem to get them functioning). And then use a GridLayout to put all the items on the panel in a single column.

Any help would be very much greatly appreciated!

Thanks :)

After setting the font try calling validate() and or repaint() on the panel containing the items on which the font changes. If that still doesn't do it, then, on the component on which the font changes call component.setText(component.getText()) after setting the font and then still call validate()/repaint().

(1) In the constructor you have redundant declaration for these attributes
private JRadioButton courier, timesnr, helvetica;
which makes them become the local variables within the constructor, but the actual three attibutes are still null.
(2) The radioButtons have to be grouped so that the client may select only one of them. The radioGroup is an additional attribute for the class:
private ButtonGroup radioGroup;

radioGroup = new ButtonGroup();
      radioGroup.add(courier);
      radioGroup.add(timesnr);
      radioGroup.add(helvetica);

(3) Finally, one has to insert the following code into the itemStateChanged method:

after line 69

if (courier.isSelected()){
         typeFace = new String("Courier");
         }else if(timesnr.isSelected()){
         typeFace = new String("Times New Roman");
         }else if (helvetica.isSelected()){
         typeFace = new String("Helvetica");
         }

Thank you so much for all your help, I have fully functioning code.
If requested, I will post online but the input definitely helped alot!

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.