Character widths in JTextPane

Hello everybody
I write a WYSIWYG Report-Editor. My Problem is that the character-widths in the Editor is different to the Output (rtf,pdf).
For Exaple in the Editor i can write 45 w's (Arial,plain,11) in one Line an in the Output is was 57. With Times New Roman i can write 64 ant in the Output is possible to set only 57 in one line.
So i have write an own BoxView for the TextPane and set int the paint method "g2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON)" The result is that the characterwitds are identic to the output but the caretpositions in the editor are wrong.
Then i try to set putProperty("i18n", Boolean.TRUE) in the document. The result is that the characterwidts are equals the the version without fractionalmetrics->ON.
What can i do?
Thanks Thomas

Thank You for the tip
I overwrites the getFontMetrics Method in the JTextPane
@Override
public FontMetrics getFontMetrics(Font font) {
Graphics graphics = getGraphics();
Graphics2D g2d = (Graphics2D) graphics;
if(g2d == null){
return super.getFontMetrics(font);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON);
return g2d.getFontMetrics(font);
It works!

Similar Messages

  • Reading TrueType Font character widths including kerning

    Okay, here it is: Since getStringBounds() and stringWidth() deliver incredible unprecise values and Sun apparently doesn't really care to fix this, I need to read the kerning information of a true type font manually.
    What I have is a field that is formatted as "width is 3.5cm" and its content is "Yo folks, this rocks" or something like that. What I need to do now is to measure whether the text (the content) really matches the field's size (3.5cm) or needs to be wrapped or cut.
    getStringBounds() and stringWidth() simply delivers crap as results, since Sun's rasterizer apparently completely ignores the kerning information and simply counts the character widths together (see: http://forum.java.sun.com/thread.jsp?forum=20&thread=421610).
    So, does anyone have a clue on how to read TTF fonts directly and measure a font using the information that is already contained in the TTF file? And please, I know that Sun's rasterizer is system independant, but that' doesn't help me any single bit and waiting for Sun to perfect their rasterizer is out of question as well.
    I shall award the author of the best clue or hint into the right direction (hey, after all, maybe I oversaw something (nope, setting KEY_FRACTIONALMETRICS doesn't count, that still returns the wrong results, although they're better and more precise than without them <g>)) with ten Duke bucks.
    Thanks a lot (and sorry for the language)

    I have a starnge feeling that this may be partly to do with the size
    of the font you are using. For Ariel plain I got the following size
    10 point=     374
    This is the actaul width of the text (in pixels) as displayed.
    My monitor is 78.818 dpi. so converting this to 72 dpi = 341.65
    at 96 dpi this is 455.5.
    The following code was used (It shows the mouse position)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    public class Fonts extends JFrame{
      private static final String text =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
      JTextPane jtf;
      StyledDocument doc;
      public Fonts(int pSize) {
        super();
        doc = new DefaultStyledDocument();
        jtf =new JTextPane(doc);
        jtf.setText(text);
        Font f = new Font("Ariel",Font.PLAIN,pSize);
        jtf.setFont(f);
        getContentPane().add(jtf);
        jtf.addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseMoved(MouseEvent me) {
              System.out.println(me.getX()+" "+getY());
        setSize(800,600);
      public JTextPane getTextPane() {
        return jtf;
      public static void main(String[] args) {
         Fonts f = null;
         try {
           f = new Fonts(Integer.parseInt(args[0]));
           f.setVisible(true);
         }catch(Exception e) {
            System.out.println("Please enter a valid point size");
            System.exit(1);
        try{
         Rectangle start = f.getTextPane().modelToView(0);
         Rectangle end = f.getTextPane().modelToView(text.length());
         System.out.println("point size "+args[0]+"\t"+(end.getX()-start.getX()));
        }catch(Exception e) {
          e.printStackTrace();
    }matfud

  • Fixed character width font

    Hi All,
    1.
    In SAPScript i m using the font
    ANDALE_J Size 8
    This font is NOT 'Proportionate Spacing'
    ie. It is fixed character width font
    as per the definition.
    2.
    In preview, the alignment of data
    &field1(35)& &field2(15)&  &field3(25)
    is seen perfectly OK.
    (multiple rows in detail data)
    3.
    But when printout is taken, the
    data is not printed as per alignment
    seen on the screen.
    Can anybody guide & help.
    Regards,
    Amit Mittal.

    This means that the font you are using is not available for the device type used by the output device (printer) you are using.
    Check transaction se73.
    Select Printer fonts -> Display -> double click on the device type for the output device you are using and verify if the font family <b>ANDALE_J</b> size <b>080</b> is listed.
    If it is not, use one that is available or ask the system admin to install the font for the device type and in some cases on the printer as well.
    Rishi

  • Character encoding in jtextpane

    hi,
    this is regarding the character encoding in a JTextPane. I need to display UTF-8 charcters in a JTextPane and i'm able to do it. But when i read the data back from the JTextPane using getText() method i'm loosing the encoding.. The characters are distorted and some junk symbols are coming.
    How can we read the data from a JTextPane preserving the encoding.
    Thanks,
    Sriram.

    Thank You for the tip
    I overwrites the getFontMetrics Method in the JTextPane
    @Override
    public FontMetrics getFontMetrics(Font font) {
    Graphics graphics = getGraphics();
    Graphics2D g2d = (Graphics2D) graphics;
    if(g2d == null){
    return super.getFontMetrics(font);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    return g2d.getFontMetrics(font);
    It works!

  • Only one character in a JTextPane using a DefaultStyledDocument

    Heya,
    I'm developing a program to read and filter some big traces file and I'm having a little problem now.
    I've done a small text program, with the class MyFile, MyDocument, FileViewer and Test, I try to load myDocument in a JTextPane but I only see the first character of the document. Maybe I nedd to implement more function in MyDocument class, I've also read some thing about the Element but I'm a little loose, I don't know which way to choose for my test.
    Hope you could help me,
    Thanks.

    Here a part of my code FileViewer class
             * Set properties of the components
            mainWindowApplication.setTitle("Traces Restit Test");
            mainWindowApplication.setSize(640, 480);
            //tracesTextPane.setText("test");
            //tracesTextPane.setContentType("DefaultStyledDocument");
            tracesTextPane.setEditable(false);
            tracesTextPane.getCaret().setVisible(true);
            // Creation of the scrollable areas - ENTRAINE UN BUG ET NE FONCTIONNE PAS VRAIMENT
            //scrollTraces = new JScrollPane(tracesTextPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
            loadFileButton.setText("Go test !");
            colorLineButton.setText("Color 1/2 !");
            clearButton.setText("Clear !");
            exitButton.setText("Quit");
            myDocument = GUI_myDocument;      
             * Style, red letter
            final Style styleRed = tracesTextPane.addStyle("Red", null);
            StyleConstants.setForeground(styleRed, Color.red);
            StyleConstants.setFontSize(styleRed, 14);
             * Odd line will be in red, pair line are keeped in black
            for (int lineNumber = 0; lineNumber < myDocument.myFile.getNumberOfLines();
                    lineNumber++) {
                if ((lineNumber % 2) == 0) {
                    // Get the line
                    int offsetBeginLine = myDocument.getOffsetBeginLine(lineNumber);
                    int offsetEndLine = myDocument.getOffsetEndLine(lineNumber);
                    int lengthLine = myDocument.getLengthLine(offsetBeginLine,
                            offsetEndLine);
                    // Odd line in red
                    myDocument.setCharacterAttributes(offsetBeginLine, lengthLine,
                        tracesTextPane.getStyle("Red"), true);
            // TEST
            //myDocument.setCharacterAttributes(0, 5,tracesTextPane.getStyle("Red"), true);
             * Initialisation of the reading task progressBar
            taskProgressBar = new JProgressBar(0);
            taskProgressBar.setValue(0);
            taskProgressBar.setStringPainted(true);
             * Specify LayoutManagers to arrange components in the containers
            mainWindowApplication.getContentPane().setLayout(new BorderLayout());
            buttonPanel.setLayout(new FlowLayout());
            tracesPanel.setLayout(new BorderLayout());
             * Add components to containers, layout constraints
            buttonPanel.add(taskProgressBar);
            buttonPanel.add(loadFileButton);
            buttonPanel.add(colorLineButton);
            buttonPanel.add(clearButton);
            buttonPanel.add(exitButton);
            //tracesPanel.add(scrollTraces);
            tracesPanel.add(tracesTextPane);
            mainWindowApplication.getContentPane().add(tracesPanel, "Center");
            mainWindowApplication.getContentPane().add(buttonPanel, "South");

  • Set Character Width of Select List

    Hi All,
    I have a select list on my apex page. It is a dynamic select list generated from a sql query. Does anyone know how to set the max character length of the list item, as some of my entries are enourmous and the select list takes up the width of my page.
    Thanks in advance!

    User,
    What is your name?
    You would do this with the LOV query. Here's an example:
    SELECT
       CASE
          WHEN LENGTH(display_col) > 50 THEN SUBSTR(display_col,1,47) || '...'
          ELSE display_col
       END display,
       return_col AS return
    FROM my_tableRegards,
    Dan
    http://danielmcghan.us
    http://www.skillbuilders.com
    You can reward this reply by marking it as either Helpful or Correct ;-)

  • Specifying the Character Width or Height of a font

    I have a Flex SDK 4.5.x and ActionScript 3.0 application that I am working on that contains Label, TextField and RichEditableText fields.  In our application, I need to be able to specify the font and point size that will be used to display each of the above.  This part is easy and not an issue.  The problem we are experiencing is a secondary requirement that I must be able to change the average width or height of a character from what the font / point size default is.  For instance, if I have a Text display object configured with Arial TTF and a point size of 16, the default average character height and width are 0.222 and 0.121 inches respectively.  I need to be able to change the font height and/or width of the default average character resulting in a Text display object that is correctly stretched in the respective height / width direction.

    Not that I know of. Searching for "prefWidth" in the CSS documentation yields no results. JavaFX layout is typically not managed by CSS, but by FXML and/or Java. You can set the prefHeight and prefWidth as attributes in the FXML file:
    <ListView fx:id="..." prefWidth="130" prefHeight="100" ... />or in Java:
    ListView<String> listView = new ListView<String>();
    listView.setPrefWidth(130);
    listView.setPrefHeight(100);

  • Font Character Width How ?

    I have a text area and need to place a guide at a certain
    character offset but don't know how to calculate the pixel width
    that text will occupy. The text is not typed yet; I need to place
    the guide before any text is input. I use a fixed width font only
    (courier). Any suggestions ?

    I am using Text Layout Framework 2. I have drawn text field using TextFlow and container controller.

  • Word wrapping at JTextPane boundaries

    Hi ! I have two problems related to JTextPane -
    1. I want to wrap words which are larger than the width of JTextPane so that it may remain visible inside the fixed boundaries.
    2. if there is an image and it can not be displayed in the same line in continuation of text then it should be displayed in the next line.
    All images that i have used have less width than that of JTextPane.
    Both these problems may be related or different, I dont know how to solve these problems.
    Please suggest any solution.

    Till now I am able to find the height of JTextPane after inserting some text by creating a dummy JTextPane
    and calling the method getPrefferedSize().height
    this wraps text correctly at word boundaries but i want it to be wrapped according to word boundaries and
    textpane boundaries both
    actually I am developing a chat application in which I have to show message from other person
    and if there is some text like ": )", ": D" etc then i want to show smileys :), :D in the place of these substrings
    I can insert text and replace the text with images by using StyledDocument's insert function
    but if i simply replace the text with images then the images are shown in the same line and wrapping doesn't work
    hence some part of images are not visible in the JTextPane
    as shown in the links http://www.facebook.com/photo.php?fbid=193222607456431&set=a.193222600789765.36992.100003060781951&type=1&ref=nf
    and http://www.facebook.com/photo.php?fbid=193225164122842&set=a.193222600789765.36992.100003060781951&type=1&ref=nf
    here link 1 shows the problem of partial display of image
    and link 2 shows the problem of inserting text that is larger than the width of the JTextPane
    And i want that whatever is inside the JTextPane should be shown inside the boundaries of it, I don't want to add a
    horizontal scrollbar !!!!!
    hope you got the problem, please provide some help !

  • Character mode report in report 3.0.5.2

    Hi everyone,
    Can someone help please how a character mode report be created which is needed to be saved as text file of extra width likely of 14"x8.5" pagesize(lanscape). I have almost 14 numeric columns, and I need this report then to load into EXCEL sheet. I don't want fields wrap around.
    Can someone please help me step by step the procedure of creating such kind of report? I tried all possibilities of my own but didn't get it. The report version is rather old i.e. 3.0.5.2.1. I have no clue if old version is the problem. Any assistance in this matter will highly be appreciated.
    Thanks
    Zahir

    Okay, well, if you're in 3.0 then you'll need to alter the character widths in the report level property palette. As I mentioned in the previous reply the character mode settings are independant from the bitmapped settings. You'll also need to set 'Design in character units' to be true.
    Once you've done this then you should see character units in the layout editor, and you should see that altering the width makes the numbers larger.
    This is only half the story though. When you're ready to output the report you'll run with mode=character, and specify a prt file to use in the desformat parameter (by default we'll use dflt.prt). You'll also need to set the required widths inside the prt file that you're going to use to output (for more on prt files consult the on-line help).
    Hope this helps,
    Danny

  • Type1 font missing glyph width from /Widths array

    Hi everyone
    I am trying to calculate character width in order to show highlighted search results in a PDF document. The problem I have faced is that the /Widths array I am depending on has a portion of its values equal to 0. The values substituted with zeros are the ones that are used in the text. This is probably related to embedded font subsets, but I am not sure at the moment. What are the actions that should be taken in such a situation? See the structure of the mentioned PDF in a screenshot below:
    Thank you in advance for your time.

    Yes, writing a 0 into the width for a character that has been removed from a subset is a somewhat-common operation.
    I am surprised that you are finding glyphs that are actually being used in the content stream with 0 widths.

  • Text width discrepancies.

    We have a desktop application client/server application which will render Crystal Reports along with a web application which interfaces with the server and renders the same reports.  I am seeing differences in the character widths which can cause differences in the reports depending on which medium it is rendered.  Included are screenshots of the same report rendered in Crystal Reports as a preview, one rendered within our
    desktop application, and one rendered using the web interface.  The report is just basically static text to show the issue, note how the first line of numerals seems to get longer in each screenshot until in the web interface it actually wraps to a new line.  We are using the .Net Crystal Report Viewer component.
    Print preview
    Desktop application
    Web interface
    Any thoughts on ways to mitigate this issue?

    Hi Sean,
    Do you have No Printer selected? If so then its USP10 and GDIPlus causing the differences.
    If you have the report set to a specific printer then make sure that printer is installed on the WEB server and follow this MS KBA on how to share printers for all users:
    //For printers exposed to System account as per MS Kbase
    http://support.microsoft.com/default.aspx?scid=kb;en-us;184291
    Don

  • How to calculate the size of JTextPane in advance?

    I have a dialog that contains JTextPane. The text that is to be displayed in the JTextPane is obtained at run-time and is passed to the dialog constructor.
    Providing that I know how width the JTextPane should be, how can I calculate the height that will be sufficient to hold the text? I want to make all the text visible; I do not want to use JScrollPane.
    gracjan

    Hi!
            int width=somewidth;
            pane.setSize(width,Integer.MAX_VALUE);
            int height=pane.getPreferredSize().height;works fine for me when pane is JEditorPane.
    You may look at reply 5 and 6 of this:
    http://forum.java.sun.com/thread.jspa?threadID=5114687
    Annette

  • JTextPane inside JScrollPane

    Hi,
    I've seen a lot of possibilities to get a JTextPane into JScrollPane without wraping the text in the forum. None worked for me..
    My situation:
    JTextPane(editable=false) with given text(DefaultStyledDocument) is inside JScrollPane.
    I replace (by pressing a button) some text parts with longer text AND JTextPane wraps it!..
    I tried to set the size of the textPane to the computed line width of the longst line, but nothing happened.. (I expected the JScrollPane to show off horizontal scrollbar, because the width of JTextPane is getting bigger than the width of JScrollPane).
    Anyone knows how to put formatted text into text- or scrollpane without wraping it?
    And who allowed JTextPane to wrap my lines anyway?!
    (it's called StyledDocument and not ChaosDocument :)
    Thanks in advance!
    Raman.

    Not sure that JTextPane is the best place to start. The wrapping is controlled by ParagraphView and its underlying structure. Try looking at implementing extensions to the standard EditorKit/ViewFactory to tweak this behaviour

  • Page width

    I have an "wide" report (approx 800 character width). Within the Report Builder tool, it run correctly. It's designed as a character, paper layout, landscape, 10 pages accross. I've defined the report within concurrent manager. When I run it through concurrent manager, the report truncates to 80 columns. I've done several other similar reports with no problem but am stumped by this one. Any ideas? Thank you very much.

    I've checked the main margin. It is defined as much larger than the contents. I've also changed desformat to wide (from dflt). Unfortunately when I try to copy another report/replace the query, I get errors REP-1051 and REP-1070 as I was getting several days ago.
    I'm still looking for ideas.
    Thank you.

Maybe you are looking for

  • Problem in login lumia 610

    Dear sir, I m missing my Nokia account. Those account actived this service. So please give me suggestions.

  • CANNOT CHANGE QUALITY PRESETS

    Captured video is 1920x1080p, AVCHD format, using Adobe Premiere pro CS5 and Encore CS5, win 7 computer. Used Adobe dynamic link to get edited source into Encore. It uses quality presets 720x480, 29.97fps. I edit and build in Encore which results in

  • How to use the Oracle AQ JMS API using Toplink

    My application is using AQ using PL/SQL. We want to convert the PL/SQL code to the JMS interface Oracle offers, the problem is that I need to use a java.sql.Connection object in order to connect to the AQ. I don't own the java.sql.Connection in Topli

  • How can I add text to a photo?

    Is there a way in iPhoto to add text directly to a photo being edited?  With my old PC I was able to add text with Microsoft dicital image suite software.  Thanks.........Allen

  • Dear Cyclosaurus (just trying to get your attention)

    I apologize in advance (I would of emailed if I had your email addy). I don't mean to be pushy but I was not sure if you were auto-magically following the thread below which I have been posting to and keeps getting buried. So I went went ahead as I w