JTextPane setFont doesnt work

Hi All

I have set the font of a jtextpane by using the following code:

this.text = new JTextPane( new DefaultStyledDocument() );

this.text.setBorder( new EmptyBorder( 0, 0, 0, 0 ) );

this.text.setText( "" );

this.text.setSize( 600, 300 );

this.text.setFont( new Font( "Monospaced", Font.PLAIN, 10 ));

I need to set the font to Monospaced to ensure all the characters are equally spaced. Whatever I do nothing and I mean nothing works. I have tried other font types and they dont work.

It seams like a bug but I could be doing something wrong.

Can anybody help me find a solution.

[640 byte] By [Cybergena] at [2007-10-3 3:47:31]
# 1
Could the new DefaultStyledDocument() set a different font? What happens if you use an empty c'tor for the JTextPane?
PhHeina at 2007-7-14 21:44:29 > top of Java-index,Java Essentials,Java Programming...
# 2
My bet is that you shouldn't be invoking setFont on that object, but rather be using the API in a different way.I refer you to a tutorial, which probably shows examples of how to do that. http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
warnerjaa at 2007-7-14 21:44:29 > top of Java-index,Java Essentials,Java Programming...
# 3

> nothing works.

As in you'll get a different font?

I didn't have a problem setting the font but will read the tutorial mentioned

above.

Here is the code that didn't give me any problem.

import java.awt.BorderLayout;

import java.awt.Font;

import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;

import javax.swing.JTextPane;

import javax.swing.border.EmptyBorder;

import javax.swing.text.DefaultStyledDocument;

class TestClass {

public static void main(String[] args) {

new TestClass();

}

public TestClass() {

// Get all font family names

GraphicsEnvironment ge = GraphicsEnvironment

.getLocalGraphicsEnvironment();

String fontNames[] = ge.getAvailableFontFamilyNames();

// Iterate the font family names

for (int i = 0; i < fontNames.length; i++) {

System.out.println(fontNames[i]);

}

// got Monospaced so I'll use it

JTextPane text = new JTextPane( new DefaultStyledDocument() );

text.setBorder( new EmptyBorder( 0, 0, 0, 0 ) );

text.setText( "" );

text.setSize( 600, 300 );

text.setFont( new Font( "Monospaced", Font.PLAIN, 30 ));

JFrame frame = new JFrame();

frame.getContentPane().add(text, BorderLayout.CENTER);

frame.setSize(600,300);

frame.setVisible(true);

}

}

harmmeijera at 2007-7-14 21:44:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Sorry that I wasnt clear with 'doesnt work'

What I mean is, no matter what I set the font to, it uses some default font. I want a monospace font so I have tried

text.setFont (new Font("Courier", Font.PLAIN, 18));

text.setFont (new Font("Courier New", Font.PLAIN, 18));

text.setFont (new Font("Monospace", Font.PLAIN, 18));

No matter what I set it to, it still uses some font that isnt monospace. Some how the font isnt overriden.

Cybergena at 2007-7-14 21:44:29 > top of Java-index,Java Essentials,Java Programming...
# 5
Hi Everybody. I want to appologize. This is an error on my behalf. I'm sorry if I have wasted any of your time.
Cybergena at 2007-7-14 21:44:29 > top of Java-index,Java Essentials,Java Programming...
# 6
Just out of curiosity, what was the problem?
Djaunla at 2007-7-14 21:44:29 > top of Java-index,Java Essentials,Java Programming...