Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Zoom in TextPane  RSS feed

danish shaikh
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, I m back.
I m creating an RTF Text Editor, i m just near to finish it. I have used JTextPane to load the file contents, but the contents are not fairly visible in pane i also increased the zoom but still after zooming it showing the gap between the normal text and colored text ne body give me the solution its urgent plz give me reply. i m sending u the code...

class ScaledTextPane extends JTextPane {
String zoomFactor = new String ("125%");
public ScaledTextPane() {
String s = (String) zoomFactor;
s = s.substring(0, s.length() - 1);
double scale = new Double(s).doubleValue() / 100;
ScaledTextPane.this.getDocument().putProperty("ZOOM_FACTOR", new Double(scale));

public void setSize(Dimension d)
if (d.width < getParent().getSize().width)
d.width = getParent().getSize().width;

public boolean getScrollableTracksViewportWidth()
return false;

public void repaint(int x, int y, int width, int height) {

class ScaledEditorKit extends RTFEditorKit {
public ViewFactory getViewFactory() {
return new StyledViewFactory();

class StyledViewFactory implements ViewFactory {
public View create(Element elem) {
String kind = elem.getName();
if (kind != null) {
if (kind.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (kind.equals(AbstractDocument.ParagraphElementName)) {
return new ParagraphView(elem);
} else if (kind.equals(AbstractDocument.SectionElementName)) {
return new ScaledView(elem, View.Y_AXIS);
} else if (kind.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
} else if (kind.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
// default to text display
return new LabelView(elem);

class ScaledView extends BoxView{
public ScaledView(Element elem, int axis) {

public double getZoomFactor() {
Double scale=(Double)getDocument().getProperty("ZOOM_FACTOR");
if (scale!=null) {
return scale.doubleValue();
return 1;

public void paint(Graphics g, Shape allocation) {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
double zoomFactor = getZoomFactor();
AffineTransform old=g2d.getTransform();
g2d.scale(zoomFactor, zoomFactor);
super.paint(g2d, allocation);

public float getMinimumSpan(int axis) {
float f = super.getMinimumSpan(axis);
f *= getZoomFactor();
return f;

public float getMaximumSpan(int axis) {
float f = super.getMaximumSpan(axis);
f *= getZoomFactor();
return f;

public float getPreferredSpan(int axis) {
float f = super.getPreferredSpan(axis);
f *= getZoomFactor();
return f;

protected void layout(int width, int height) {
super.layout(new Double(width / getZoomFactor()).intValue(),new Double(height *getZoomFactor()).intValue());

public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
double zoomFactor = getZoomFactor();
Rectangle alloc;
alloc = a.getBounds();
Shape s = super.modelToView(pos, alloc, b);
alloc = s.getBounds();
return alloc;

public int viewToModel(float x, float y, Shape a,Position.Bias[] bias) {
double zoomFactor = getZoomFactor();
Rectangle alloc = a.getBounds();
return super.viewToModel(x, y, alloc, bias);

I have created my texpane an instance of ScaledTextPane..

Danish Shaikh.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!