Results 1 to 6 of 6
  1. #1
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Unhappy Dynamically change the font and color message to be displayed on a panel.

    So my code has not yet changed the color of the font. I'm working on getting the font style to change first(font type, font size, font style). I also need to center the font via a button. So if anyone has any tips on that it would be appreciated. My error messages are as follows
    at FontViewFrame.setSampleFont(FontViewFrame.java:141 )
    at FontViewFrame.<init>(FontViewFrame.java:31)
    at FontViewerTest.main(FontViewerTest.java:10)

    Why am I getting these errors? The class names for both of my classes are also highlighted in yellow! >.< Someone please help.
    Java Code:
    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)
    
    
    //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;
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    what line is 141? Which variable in this line is null? Figure this out and then can initialize the variable before using it.

  3. #3
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    The lines of code in question are,
    Java Code:
    String facename = (String) facenameCombo.getSelectedItem(); <----in --setSampleFont
    
    setSampleFont(); <----in innerclass ChoiceListener
    
    
    JFrame frame = new FontViewFrame ();   <---in class FontViewer
    I have a general understanding of what a NullPointerException is, however I'm just having a hard time seeing where I have an empty or "null" object. I feel like I've instantiated everything.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why are you doing this?
    Java Code:
    JComboBox facenameCombo = new JComboBox();
    It makes a local version of facenameCombo and initializes it, instead of using the instance variable. I don't believe this will be accessible outside the method since it's local.

    edit: As a random side not, if you want to add more font capabilities, it's quite easy to write a method to generate all the fonts on a system using the GraphicsEnvironment class.
    Last edited by sunde887; 05-09-2011 at 06:14 AM.

  5. #5
    jeskoston is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    zomg! Why did I do that!? I think I was on auto-type-pilot. Thank you so much for pointing that out. I was reading your post thinking, wow he has a good point, and thinking...why wouldn't a use a perfectly good instance variable. Thanks sunde887, Fubarable as well, you guys are awesome!

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome. Please mark your thread solved with the thread tools at the top.

    Also, if you have a large amount of code please be sure to flag the parts the exception trickles through. It makes it easier for us to help.

Similar Threads

  1. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  2. Can you set the font for an entire panel?
    By pahiker in forum AWT / Swing
    Replies: 8
    Last Post: 07-05-2010, 10:45 AM
  3. change Panel Color
    By aadi_j in forum AWT / Swing
    Replies: 2
    Last Post: 11-16-2009, 10:12 AM
  4. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  5. Replies: 0
    Last Post: 06-25-2008, 10:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •