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]

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