1 Reply Latest reply: May 24, 2012 1:33 PM by KevinPas RSS

    Font size different in JTextPane than Open Office or Windows Notepad

    KevinPas
      Shouldn't Open Office, Notepad and JTextPane all appear the same?

      I've been working on this off and on for about a year and I must be missing something:

      In open office or Notepad select a 24 point font then type "Something". Do the same in a JTextPane and the JTextPane will appear a lot smaller.

      I need to do three things to JTextPane to get the size siimilar (I don't thing fontScale() should be necessary but it is):

      1) In the DefaultStyledDocument that I added to the JTextPane do the following:

      SimpleAttributeSet sas = new SimpleAttributeSet ();
      sas .addAttribute ( javax.swing.text.StyleConstants.FontFamily, "Font name to try" );
      sas .addAttribute ( javax.swing.text.StyleConstants.FontSize, (int)( (float) 24.0 * fontScale()) );
      insertString ( position, "Something", sas );

      2) Write the function fontScale():

      int res = Toolkit.getDefaultToolkit() .getScreenResolution();
      return (float) res / (float) 72;

      3) Each time I change the font/point size do the following to the JTextPane for each new char entered:

      MutableAttributeSet attr = new SimpleAttributeSet ();
      {
      int size = (int) ( (float)24 * fontScale() );
      StyleConstants .setFontFamily ( attr, "Font name to try" );
      StyleConstants .setFontSize ( attr, size );

      setCharacterAttributes ( drawingJPanel, attr, false );
      }
        • 1. Re: Font size different in JTextPane than Open Office or Windows Notepad
          KevinPas
          In the comments of my fontScale() function I have a link to the following:

          http://java.sun.com/products/java-media/2D/reference/faqs/index.html

          (then search for "10 pt" or "72/96")

          But I'm not confident that this is really the answer. It doesn't make sense to me that JTextPane needs special treatment to get it to work like other "text editors".

          Thanks for any thoughts or insights.

          Edited by: user13659321 on May 24, 2012 11:33 AM