Hi,
I want to be able to resize a font that is used within my styled JTextPane
so that it grows both horizontally and vertically when the form resizes.
I have the code to rescale the font (posted below), and that can be applied
to a label which works fine.
What I want to do is set this newly rescaled font as the font for the styled
JTextPane, but I cannot seem to make it work.
I have tried setting the font using textPane.setFont(), I have also tried
resetting the styles but there is only a fotnSize atrribute not a specific
method for setting the real font to be used for a style..
From the code below, assume that handleResize() is called from a component
listener - when the component resizes. This is definitely being called.
Assume also that addStyles() is using a JTextPane set as a class level var,
as is the _font.
The rescale appears to work for the label but not the text pane.
Any ideas,
Steve
private void handleResize(){
Rectangle bounds = _textPane.getBounds();
try{
String line = "";
for(int i=0; i< 80; i++){
line+="X";
}
Graphics g = this.getGraphics();
FontMetrics fm = g.getFontMetrics(_font);
int strWidth = fm.stringWidth(line);
int strHeight = fm.getHeight();
int gridHeight = strHeight * 25;
//Now try and work out the scaling!!!
int componentYRange = bounds.height - bounds.y;
int componentXRange = bounds.width - bounds.x;
double scaleXFactor = (double) componentXRange / strWidth;
double scaleYFactor = (double) componentYRange / gridHeight;
AffineTransform fontTransform = new AffineTransform();
fontTransform.scale(scaleXFactor, scaleYFactor);
//New Scaled Font
_font = _font.deriveFont(fontTransform);
System.out.println(_font.getSize() + "("+scaleXFactor + ", " +
scaleYFactor+")");
Style s = _textPane.getStyle("regular");
StyleConstants.setForeground(s, new
Color((int)(Math.random()*255),
(int)(Math.random()*255),(int)(Math.random()*255) ));
_testLabel.setFont(_font); //WORKS FINE
_textPane.setFont(_font); //DOES NTO DO ANYTHING
//reapplyStyles(_textPane,s);
}catch(Exception err){
System.out.println(err.getMessage());
err.printStackTrace();
}
}
protected void addStylesToDocument(StyledDocument doc) {
//Initialize some styles.
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("STYLE CHANGED!");
reapplyStyles(_textPane, (Style)e.getSource());
}
};
_textPane.setFont(_font);
Style regular = doc.addStyle("regular", null);
//StyleConstants.setFontFamily(regular, _font.getFamily());
//StyleConstants.setFontSize(regular, _font.getSize());
StyleConstants.setForeground(regular, Color.GREEN);
StyleConstants.setBackground(regular, Color.BLACK);
regular.addChangeListener(changeListener);
Style s = doc.addStyle("italic", null);
s.addAttributes(regular);
StyleConstants.setItalic(s, true);
s.addChangeListener(changeListener);
s = doc.addStyle("bold", null);
s.addAttributes(regular);
StyleConstants.setBold(s, true);
s.addChangeListener(changeListener);
s = doc.addStyle("underline", null);
s.addAttributes(regular);
StyleConstants.setUnderline(s, true);
s.addChangeListener(changeListener);
}
John McGrath - 28 Mar 2005 01:54 GMT
> _textPane.setFont(_font); //DOES NTO DO ANYTHING
All Swing and AWT components have a Font property. Most components use
this property, but some do not. A JTextPane holds styled text, so each
element in the text pane has its own font information and the superclass
Font property is not used.
To change the font for a some or all of the text, you will need to set the
Font attribute for the text that you want to modify.
It looks like you were trying to modify the font for a style. I think
that should work, but I could not say off the top of my head exactly what
needs to be done.

Signature
Regards,
John McGrath
John McGrath - 28 Mar 2005 02:49 GMT
> It looks like you were trying to modify the font for a style. I think
> that should work, but I could not say off the top of my head exactly what
> needs to be done.
On second thought, that probably will not work for you. It seems that you
can set various attributes that control the Font (FontFamily, FontSize,
Bold, Italic, etc.), but there is no attribute whose value can be set to a
java.awt.Font, so you cannot use a derived font.

Signature
Regards,
John McGrath