The total left indent and right indent of multiple lines in jtextarea

Source: Internet
Author: User
Import Java. AWT. borderlayout; import Java. AWT. container; import Java. AWT. event. keyevent; import Java. AWT. event. keylistener; import javax. swing. jframe; import javax. swing. jtextarea; import javax. swing. text. badlocationexception; public class indentation extends jframe implements keylistener {public static final int width = 1000; public static final int Height = 700; jtextarea ta; Public indentation () {initialize ();} public void initialize () {TA = new jtextarea (); TA. addkeylistener (this); Container c = This. getcontentpane (); C. setlayout (New borderlayout (); C. add (TA, borderlayout. center); setbounds (100,100, width, height); this. setdefaclocloseoperation (exit_on_close); setvisible (true);} public static void main (string ARGs []) {New indentation ();} public void keypressed (keyevent E) {// in eclipse, it is implemented through tab. However, when pressing the tab key in the jtextarea control, it is processed as the '\ t' character, so use the CTRL + q shortcut to implement if (E. iscontroldown () & keyevent. vk_q = E. getkeycode () {// indent string text = TA to the right of multiple rows. gettext (); int start = TA. getselectionstart (); int end = TA. getselectionend (); int startrow = 0; int endrow = 0; int n = 0; try {startrow = TA. getlineofoffset (start); endrow = TA. getlineofoffset (end); // n = endrow-startrow; while (endrow> = startrow) {end = text. substring (0, end ). lastindexof (10); TA. insert ("\ t", end + 1); // TA. replacerange ("\ t", end, end + 1); endrow -- ;}} catch (badlocationexception E1) {e1.printstacktrace () ;}} else if (E. isshiftdown () & keyevent. vk_tab = E. getkeycode () {// unindent int start = TA on the left of multiple rows. getselectionstart (); int end = TA. getselectionend (); int startrow = 0; int endrow = 0; int n = 0; try {startrow = TA. getlineofoffset (start); endrow = TA. getlineofoffset (end); // n = endrow-startrow; while (endrow> = startrow) {end = TA. gettext (). substring (0, end ). lastindexof (10); If (TA. gettext (). charat (end + 1) = '\ t') {TA. replacerange ("", end + 1, end + 2);} endrow --;} catch (badlocationexception E1) {// todo auto-generated catch blocke1.printstacktrace ();}}} public void keyreleased (keyevent arg0) {} public void keytyped (keyevent arg0 ){}}

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.