Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTextPane - changing font size or family works, but messes up spacing...

843806Mar 12 2009 — edited Mar 12 2009
I have a font size chooser that someone can use to select a new font size for the currently selected text. The method here works fine. The font size is changed by executing the document's setCharacterAttributes method through the StyledEditorKit's FontSizeAction. The JTextPane also recognizes the change, but the problem is: the JTextPane does not draw the line spacing correctly. When I change from font size 12 to 48, for example, the text size grows to 48, but the spacing between one line of text and another (only those separated by a newline, not wrapped text) seems to remain at size 12. That is, the text seems squished together. Then, we I set it again to size 48, the line spacing is corrected to 48 (so the text seems to be too far away). No extra line feeds are added in this process. The only way to correct this is to change another attribute or remove and re-insert the line feeds. Weird...

Also, we I set font size from 12 to 48, then 48 to 12. The font size becomes 12, but the line spacing remains at 48. I tried firing two separate events and calling the setCharacterAttributes method on the document twice, but it doesn't work. However, I did try launching a separate Thread to execute setCharacterAttributes twice, with a 1 second delay between calls, and that worked. Seems like the JTextPane is not receiving events right... maybe a timing issue, but I don't know.

It's almost like the JTextPane is drawing the text based upon the last set font size. Is this a bug? I'm running Java 1.6.0_12 on Windows XP.

This problem also occurs if I change the font family... the line spacing is based upon the last font family. Any thoughts would be AWESOME!
fontSizeChooser.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e)
	{
		JComboBox comboBox = (JComboBox)e.getSource();
		try{
			if(comboBox.getSelectedItem() != null && !comboBox.getSelectedItem().equals(""))
			{
				Integer fontSize = Integer.parseInt(comboBox.getSelectedItem().toString() );
				//1638 is the maximum font size in MS Word 2007... why not here, too?
				if(fontSize > 0 && fontSize <= 1638)
					(new RTFEditorKit.FontSizeAction("font-size-" + fontSize, fontSize)).actionPerformed(
						new ActionEvent(mainEditor, ActionEvent.ACTION_PERFORMED, null) );
				else
					throw new NumberFormatException("Must be an integer between 1 and 1638");
			}
		}
		catch(NumberFormatException exception)
		{
			comboBox.setSelectedIndex(-1);
			//Send message to user
			JOptionPane.showMessageDialog(mainEditor, exception.getMessage(),
				"Invalid font size", JOptionPane.ERROR_MESSAGE);
		}
	}});
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 9 2009
Added on Mar 12 2009
5 comments
981 views