java - How can I measure/calculate the size a Document needs to render itself? -


i have javax.swing.text.document , want calculate size of bounding box document needs render itself.

is possible?

it trivial plain text (height = line count * line height, width = max width on each line) how can rtf, html or other document?

this code might give ideas:

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*;  public class textpaneperfectsize extends jframe {     jtextfield textfield;     jtextpane textpane;      public textpaneperfectsize()     {         textfield = new jtextfield(20);         textfield.settext("add text");         getcontentpane().add(textfield, borderlayout.north );         textfield.addactionlistener( new actionlistener()         {             public void actionperformed(actionevent e)             {                 try                 {                     document doc = textpane.getdocument();                     doc.insertstring(doc.getlength(), " " + textfield.gettext(), null);                     textfield.settext("");                     dimension d = textpane.getpreferredsize();                     rectangle r = textpane.modeltoview( textpane.getdocument().getlength() );                     d.height = r.y + r.height;                     textpane.setpreferredsize( d );                     getcontentpane().validate(); //                  pack();                 }                 catch(exception e2) {}             }         });          jlabel label = new jlabel("hit enter add text text pane");         getcontentpane().add(label);          jpanel south = new jpanel();         textpane = new jtextpane();         textpane.settext("some text");         textpane.seteditable( false ); //      textpane.setpreferredsize( new dimension(120, 23) );          south.add( textpane ); //      getcontentpane().add(south, borderlayout.south);         getcontentpane().add(textpane, borderlayout.south);     }      public static void main(string[] args)     {         jframe frame = new textpaneperfectsize();         frame.setsize(200, 200);         frame.setlocationrelativeto( null );         frame.setdefaultcloseoperation(exit_on_close);         frame.setvisible(true);     } } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -