7 Replies - 1674 Views - Last Post: 09 May 2012 - 10:07 AM Rate Topic: -----

#1 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 01-March 11

Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:28 AM

Okay, right now I'm working on a program that can dynamically change the font and the color of a message to be displayed on a panel. The message can be displayed in bold and italic at the same time, or can be displayed at the center of the panel. I also have to use check boxes to select bold, italic or centered. User can select the font name or font size from combo boxes, and select a color from the radio buttons.

This is my code so far, i feel it is done, but it is not working. If I can get pointed in the right direction it would be appreciated.
//This will allow user to view font effects, test, modify style of font.
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.text.*;
public class FontViewerTest extends JApplet
{
    public static void main(String[] args)
    {
        JFrame frame = new FontViewFrame ();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Font Thingy (BETA)");
        frame.setVisible(true);
    }
}



import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/*This builds the frame that contains a text field and a control panel to change the font and color of the message to be displayed*/
public class FontViewFrame extends JFrame
{
        public FontViewFrame()
{   
    //Construct text sample
    sampleField = new JLabel("A cow jumps over the moon");
    add(sampleField, BorderLayout.WEST);

    //This is a "global" listener

     class ChoiceListener implements ActionListener
    {

         public void actionPerformed(ActionEvent event) 
        {
            //JComboBox source = (JComboBox) event.getSource();
            setSampleFont();
        }
    }//End inner-class choice listener
    listener = new ChoiceListener();

    createControlPanel();
    setSampleFont();
    setSize(FRAME_WIDTH, FRAME_HEIGHT);
}//End FontViewFrame constructor

//Creates the control panel that will allow for font change
public void createControlPanel()
{
    JPanel facenamePanel = createComboBox();
    JPanel sizeGroupPanel = createCheckBoxes();
    JPanel styleGroupPanel = createRadioButtons();

    //Line up components panels
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new GridLayout(3,1));
    controlPanel.add(facenamePanel);
    controlPanel.add(sizeGroupPanel);
    controlPanel.add(styleGroupPanel);

    //add panels to content pane
    add(controlPanel, BorderLayout.SOUTH);
}//End createControlPanel

//Creates a combo box with the font style choices.
public JPanel createComboBox()
{
    JComboBox facenameCombo = new JComboBox();
    facenameCombo.addItem("Serif");
    facenameCombo.addItem("SansSerif");
    facenameCombo.addItem("Monospaced");
    facenameCombo.addItem("Dialog");
    facenameCombo.addItem("DialogInput");
    facenameCombo.setEditable(true);
    facenameCombo.addActionListener(listener);

    JPanel panel = new JPanel();
    panel.add(facenameCombo);
    return panel;
}//End createcomboBox()

//Creates check boxes for selctiing bold, italic and center.
public JPanel createCheckBoxes()
{
    italicCheckBox = new JCheckBox("Italic");
    italicCheckBox.addActionListener(listener);

    boldCheckBox = new JCheckBox("Bold");
    boldCheckBox.addActionListener(listener);

    //centerCheckBox = new JCheckBox("Center");
    //centerCheckBox.addActionListener(listener);

    JPanel panel = new JPanel();
    panel.add(italicCheckBox);
    panel.add(boldCheckBox);
    //panel.add(centerCheckBox);
    panel.setBorder(new TitledBorder(new EtchedBorder(),"Style"));

    return panel;
}//End create checkBoxes

//Creates Radio buttons to select the fontsize and color
public JPanel createRadioButtons()
{
    smallButton = new JRadioButton("Small");
    smallButton.addActionListener(listener);

    mediumButton = new JRadioButton("Medium");
    mediumButton.addActionListener(listener);

    largeButton = new JRadioButton("Large");
    largeButton.addActionListener(listener);
    largeButton.setSelected(false);

    /*redButton = new JRadioButton("Red");
    redButton.addActionListener(listener);

    blueButton = new JRadioButton("Blue");
    blueButton.addActionListener(listener);

    yellowButton new JRadioButton("Yellow");
    yellowButton.addActionListener(listener);*/

//Add buttons to buttons group
    ButtonGroup group = new ButtonGroup();
    group.add(smallButton);
    group.add(mediumButton);
    group.add(largeButton);
    /*group.add(redButton);
    group.add(blueButton);
    group.add(yellowButton);*/

    JPanel panel = new JPanel();
    panel.add(smallButton);
    panel.add(mediumButton);
    panel.add(largeButton);
    /*panel.add(redButton);
    panel.add(blueButton);
    panel.add(yellowButton);*/
    panel.setBorder(new TitledBorder(new EtchedBorder(), "Size and Color"));

    return panel;
}//End createRadioButtons

//setSampleFont will get the user choice for font name
//, style, size, and color and sets the font of the text sample

public void setSampleFont()
{
    //get font name
    //JComboBox source = (JComboBox) event.getSource(); 
    String facename = (String) facenameCombo.getSelectedItem();

    //get font style
        int style = 0;
        if(italicCheckBox.isSelected())
            style = style + Font.ITALIC;
        if(boldCheckBox.isSelected())
            style = style + Font.BOLD;
        /*if(centerCheckBox.isSelected())
            style = style + sampleField = new JLabel("Phoung Nguyen's CECS 277");
            add(sampleField, BorderLayout.WEST)*/
        //get font size
        int size = 0;
        //int color = 0;

        final int SMALL_SIZE = 24;
        final int MEDIUM_SIZE = 36;
        final int LARGE_SIZE = 48;
        /*final int RED_COLOR = 
        final int BLUE_COLOR = 
        final int YELLOW_COLOR =    */

        if(smallButton.isSelected())
            size = SMALL_SIZE;
        else if(mediumButton.isSelected())
            size = MEDIUM_SIZE;
        else if(largeButton.isSelected())
            size = LARGE_SIZE;
        /*if(redButton.isSelected())
            color = red;
        if(blueButton.isSelected())
            color = blue;
        if(yellowButton.isSelected())
            color = yellow;*/

        //Set font, color of textfield
        sampleField.setFont(new Font(facename, style, size));
        sampleField.repaint();
}
private JComboBox facenameCombo;
private JLabel sampleField;
private JCheckBox italicCheckBox;
private JCheckBox boldCheckBox;
//private   JCheckBox centerCheckBox;
private JRadioButton smallButton;
private JRadioButton mediumButton;
private JRadioButton largeButton;
/*private JRadioButton redButton;
private JRadiobutton blueButton;
private JRadioButton yellowButton;*/
private ActionListener listener;

private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 400;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically change the font and the color of a message to be displaye

#2 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:30 AM

What's not working? What errors do you encounter? What line of the code are the errors popping up on?
Was This Post Helpful? 0
  • +
  • -

#3 AaronHopkins  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 180
  • Joined: 01-March 11

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:33 AM

i receive this error:

Exception in thread "main" java.lang.NullPointerException
    at FontViewFrame.setSampleFont(FontViewFrame.java:141)
    at FontViewFrame.<init>(FontViewFrame.java:31)
    at FontViewerTest.main(FontViewerTest.java:10)


Was This Post Helpful? 0
  • +
  • -

#4 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:38 AM

//This will allow user to view font effects, test, modify style of font.
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.text.*;
public class FontViewerTest extends JApplet
{
    public static void main(String[] args)
    {
        JFrame frame = new FontViewFrame ();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Font Thingy (BETA)");
        frame.setVisible(true);
    }
}



You are extending JApplet but you are never using it. You are also making a JFrame in the JApplet. Here is an article on JApplets (http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html).

Another thing, I have always got errors doing this
JFrame frame = new FontViewFrame();

Try this
FontViewFrame frame = new FontViewFrame();

This post has been edited by The_Programmer-: 09 May 2012 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:41 AM

View PostThe_Programmer-, on 09 May 2012 - 12:38 PM, said:

You are extending JApplet but you are never using it. You are also making a JFrame in the Another thing, I have always got errors doing this
JFrame frame = new FontViewFrame();

Try this [code]FontViewFrame frame = new FontViewFrame();

Not elegant but it will work. The only method used is main() so FontViewerTest can extends anything

FontViewFrame extends JFrame. It can be stored in a JFrame

@OP: None of these are realted to your problem
Was This Post Helpful? 0
  • +
  • -

#6 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:46 AM

Why are you creating an ActionListener class in your constructor?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 09:58 AM

View PostThe_Programmer-, on 09 May 2012 - 12:46 PM, said:

Why are you creating an ActionListener class in your constructor?

It is used in createRadioButtons() and createComboBox()
should work
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: Dynamically change the font and the color of a message to be displaye

Posted 09 May 2012 - 10:07 AM

The problem is in your createComboBox() method
		JComboBox facenameCombo = new JComboBox();



this create a local JComboBox that will stop to exit as soon as you method exits. You want:
		facenameCombo = new JComboBox();



to create the combo in the instance variable facenameCombo so that setSampleFont() can see it when it does:
String facename = (String) facenameCombo.getSelectedItem();

If you had posted your stack trace we will have seen it right away
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1