How line numbers are drawn in Jtextpane in "Java" swing

Source: Internet
Author: User
Tags gety

Oracle in the Jtextpane class does not directly provide a way to display line numbers, so this function should be done by the programmer himself, the author found that many of the online display line number of code there is a problem, is not accurate, especially after the number of lines has changed a lot.

The author first put out the code:

ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;Importjava.awt.Insets;ImportJavax.swing.ImageIcon;ImportJavax.swing.JTextPane;ImportJavax.swing.text.AttributeSet;Importjavax.swing.text.Element;Importjavax.swing.text.Position;Importjavax.swing.text.StyleConstants;Importjavax.swing.text.StyledDocument; Public classEditpanelextendsJtextpane {/*** Whether to implement line number, default does not show*/    Private BooleanShowlinenumber =false; Private intFontSize = 16;//The default is font number 16th     PublicEditpanel () {Super(); }     Public voidSetshowlinenumber (Booleanisshow) {         This. Showlinenumber =isshow; }     Public BooleanGetshowlinenumber () {return  This. Showlinenumber; } @Override Public voidPaint (Graphics g) {Super. Paint (g);        Styleconstants.setfontsize (Getinputattributes (), getfontsize ()); if(Getshowlinenumber ()) {drawlinenumber (g); }    }    protected voidDrawlinenumber (Graphics g) {SetMargin (NewInsets (0, 35, 0, 0)); //draw the background color of line numbersG.setcolor (NewColor (180, 180, 180)); G.fillrect (0, 0, 30, GetHeight ()); //How many rows are obtainedStyleddocument docu =getstyleddocument (); Element element=docu.getdefaultrootelement (); introws =Element.getelementcount (); //draw the color of line numbers//System.out.println ("y:" + GetY ());G.setcolor (NewColor (90, 90, 90)); G.setfont (NewFont (GetFont (). GetName (), GetFont (). GetStyle (), 16));  for(introw = 0; Row < rows; row++) {g.drawstring (row+ 1) + "", 2, Getpositiony (row + 1)); }    }     Public voidSetfontsize (intfontSize) {        if(Fontsize!=12 &&fontSize!=14 &&fontSize!=16 &&fontSize!=18 &&fontSize!=20 &&fontSize!=22 &&fontSize!=24 ){            Throw NewRuntimeException ("The line number is not recognized"); }         This. fontSize =fontSize; }     Public intgetfontsize () {returnfontSize; }    /*** Get position of Y coordinate in line number <br/> * In the process of calculation, there is a ratio value, which is determined by the difference in the return value of Gety (). * @paramrow number of lines *@returnthe y-coordinate position of the row*/    Private intGetpositiony (introw) {        inty = 0; Switch(Getfontsize ()) { Case12: Y= (Row * 18)-4;  Break;  Case14: Y= (Row * 20)-5;  Break;  Case16: Y= (Row * 23)-6;  Break;  Case18: Y= (Row * 26)-8;  Break;  Case20: Y= (Row * 29)-10;  Break;  Case22: Y= (Row * 31)-11;  Break;  Case24: Y= (Row * 34)-12;  Break; }        returny; }}
Editpanel.java

The test is:

 public  static  void   main (string[] args) {JFrame JF  =new         JFrame ("line number Test" );        Editpanel panel  =new   Editpanel (); Panel.setshowlinenumber ( true ); //  display line number   Jf.add ( new   JScrollPane (panel)); Jf.setpreferredsize ( new  Dimension (600,400         true  ); }

In general, when drawing line numbers, there is a problem, that is, how to locate the position of Y. The author of the problem of the solution has been reflected in the above code, the following details to say a bit.

A partial code for the Getpostiony () method in the Editpanel class that is first posted:

        Switch (Getfontsize ()) {        case            := (Row *)-4;              break;

It is important to note that this 18 is obtained by Gety (), and in Paint (griphics g), the value of the Gety () is printed and then the difference is obtained. After the author's test found that if the font is set to 12th, then the difference of gety () is 18, then the position of the line number y is the value of (the first few rows of *18), you can let the line number in front of each row. And then minus 4, is to put the line number up a little, looks good.

Other values and so on.

How line numbers are drawn in Jtextpane in "Java" swing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.