How to better use the Jtextpane

Source: Internet
Author: User

I often see many friends on the Internet asking how to control characters in JTextArea, how to set the color of a particular character, and so on. I am in Java to do a SQL Query Analyzer found a better solution is to use Jtextpane, then how to better use Jtextpane, I now pick part of the program part of the process for your reference.

package com. Jdagui;


import javax.swing.text.*;


import java.util.*;


import java.awt.*;


import com. jda.*;


/**


* @author Whxu


*/


public class Jdastyleddocument extends Defaultstyleddocument


{


private int type = -1;//data connection type


AttributeSet myattributeset = null;


public jdastyleddocument (int type)


  {


this.type = type;


  }


  /**


* Insert String


  */


public void insertstring (int offset,string str,attributeset a)


throws Badlocationexception


  {


this.myattributeset = A;


Super.insertstring (Offset,str,a);


Setsyntaxcolor (Offset,str.length ());


  }


/**


* Delete String


  */


public void Remove (int offs,int len)


throws Badlocationexception


  {


Super.remove (Offs,len);


Setsyntaxcolor (offs);


  }


  /**


* Get the character of the position


  */


private String Getpositionchar (int offset)


  {


String str = "";


Try


   {


str = getText (offset,1);


   }


catch (Badlocationexception ex)


   {


//ex.printstacktrace (System.out);


   }


return str;


  }


/**


* Starts at the specified position, pushing backwards to the first encountered space position


  */


Private String getbeforeblankstring (int offset)


  {


String str = "";


if (offset<0) return "";


 


str = Getpositionchar (offset);


if (Syntaxmgr.isspacechar (str))


return "";


String r = getbeforeblankstring (offset-1);


return r + str;


  }


  /**


* Starts at the specified position and pushes to the first space position encountered


  */


private String getafterblankstring (int offset)


  {


String str = "";


if (Offset>getlength ()) return "";


str = Getpositionchar (offset);


if (Syntaxmgr.isspacechar (str))


return "";


String r = getafterblankstring (offset+1);


return str + R;


  }


  /**


* According to Postion, forward judgment, backward judgement, set color, return to set the position of the end of the color


  */


private int Setsyntaxcolor (int offset)


  {


if (offset<0) return offset;//If the location you set does not exist, you may not have to consider


if (myattributeset==null) return offset;//if Myattributeset is null, you do not have to consider


String ifsyntax = "";


String before = getbeforeblankstring (offset-1);


String after = getafterblankstring (offset);


Syntax = (before + after). Trim ();


int start = Offset-before.length ();


int tmp_len = Ifsyntax.length ();


if (start<0 | | tmp_len<=0) return offset;//if the string that sets the color is empty, returns the


//Set color


Styleconstants.setforeground (MutableAttributeSet) Myattributeset,


Syntaxmgr.issyntax (Type,ifsyntax));


setcharacterattributes (start,tmp_len,myattributeset,true);


return start + Tmp_len;


  }


  /**


* According to a range, set within the range of the Syntaxcolor


  */


private int Setsyntaxcolor (int offset,int len)


throws Badlocationexception


   {


//If scope does not exist, do not consider


if (offset<0 | | | len<0) return offset;


int tmp_offset = offset;


while (Tmp_offset<offset + len)


    {


Tmp_offset = Setsyntaxcolor (Tmp_offset);


Tmp_offset = Getnextwordoffset (Tmp_offset);


    }


Tmp_offset = Setsyntaxcolor (Tmp_offset);/Set the Last word after the loop is finished


return tmp_offset;


   }


   /**


* According to Postion, get the starting point of the next word


   */


private int getnextwordoffset (int offset)


   {


int roffset = offset;


int textlength = GetLength ();


while (roffset<=textlength && offset>=0)


    {


String str = Getpositionchar (roffset);


if (! Syntaxmgr.isspacechar (str))


     {


break;


     }


roffset+=1;


    }


if (roffset!=offset)//sets the color of the interval


   {


//Set color


Styleconstants.setforeground (MutableAttributeSet) Myattributeset,


Syntaxcolormgr.getspacecolor ());


setcharacterattributes (offset,roffset-offset,myattributeset,true);


   }


return roffset;


  }


}

To this end, we have done a document that applies to Jtextpane. The use of Jtextpane is relatively simple.

Can be used in this way

Jtextpane Sqlpane = new Jtextpane (new Jdastyleddocument (type));

Because I used this example to display different keywords depending on the database type, I passed an int type.

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.