java - How to get the part of the text from JLabel according mouse pointer -


does knows how part of text beginning of jlabel pointer of mouse? example, let's have jlabel text 'c:\aaa\bbb\ccc'. user points mouse pointer on characters 'bbb', text 'c:\aaa\bbb'. now, when have part of text, can change color. think use html that.

the java accessibility api conveniently includes getindexatpoint method part of accessibletext interface converts location (such of mouse pointer) index of character @ location:

given point in local coordinates, return zero-based index of character under point. if point invalid, method returns -1.

here test program uses method part of string asked for:

import java.awt.borderlayout; import java.awt.point; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter;  import javax.accessibility.accessibletext; import javax.swing.jframe; import javax.swing.jlabel;  public class jlabelmousedemo {     private static string labeltext = "<html>c:\\aaa\\bbb\\ccc</html>";     private static jlabel label;     private static jlabel substringdisplaylabel;      public static void main(string[] args) {         jframe frame = new jframe();         label = new jlabel(labeltext);         label.addmousemotionlistener(new mousemotionadapter() {             public void mousemoved(mouseevent e) {                 accessibletext accessibletext =                         label.getaccessiblecontext().getaccessibletext();                 point p = e.getpoint();                 int index = accessibletext.getindexatpoint(p);                 if (index >= 0) {                     // index respect displayed                     // characters rather entire html string,                     // must add 6 skip on "<html>", part of                     // labeltext string not displayed on                     // screen. otherwise, substrings end                     // "tml>c:\aaa"                     index += 6;                      // strangely, in testing, index one-based index                     // (for example, mousing on 'c' resulted in                     // index of 1), makes getting part of                     // string character easier.                     string partoftext = labeltext.substring(0, index);                      // display demonstration purposes;                     // figure out how highlight or use string or                     // index in other way suit needs.                     // example, might want round index                     // values line groups of                     // characters, ever having things                     // "c:\aaa\bbb", , never "c:\aaa\b"                     substringdisplaylabel.settext(partoftext);                 }             }         });         frame.add(label);         substringdisplaylabel = new jlabel();         frame.add(substringdisplaylabel, borderlayout.south);         frame.setsize(200, 200);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);     } } 

actually obtaining object of type accessibletext corresponds particular jlabel may not work: far can tell, possible when jlabel displaying html text. seems supported jlabel source:

public accessibletext getaccessibletext() {             view view = (view)jlabel.this.getclientproperty("html");             if (view != null) {                 return this;             } else {                 return null;             }         } 

i don't claim understand what's going on in code or why accessibility not available non-html text, test program did not work when jlabel contained plain rather html text: label.getaccessiblecontext().getaccessibletext() return null, , using forced cast of (accessibletext) label.getaccessiblecontext() yield object ever returned -1 getindexatpoint.


edit: possible part of text without worrying adjusting indices based on location of html tags aren't displayed visible text. have maintain 2 copies of string on label: 1 containing characters displayed (rawtext in example below) sliced according index, , 1 containing formatted html version used text of label (the result of formatlabeltext below). because getindexatpoint returns index relative displayed characters, getting desired substring easier in second example original one. adjustment made index rounding highlighted text lines backslash-delimited groups.

import java.awt.point; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter;  import javax.accessibility.accessibletext; import javax.swing.jframe; import javax.swing.jlabel;  public class jlabelmousehighlightdemo {     private static string rawtext = "c:\\aaa\\bbb\\ccc";     private static jlabel label;      private static string formatlabeltext(int index) {         if (index < 0) {             index = 0;         }         if (index > rawtext.length()) {             index = rawtext.length();         }         stringbuilder sb = new stringbuilder();         sb.append("<html>");         sb.append("<font color='red'>");         sb.append(rawtext.substring(0, index));         sb.append("</font>");         sb.append(rawtext.substring(index));         sb.append("</html>");         return sb.tostring();     }      private static int roundindex(int index) {         // method rounds index align group of         // characters delimited backslash, red text         // "c:\aaa\bbb" instead of "c:\aaa\b".         while (index < rawtext.length() && rawtext.charat(index) != '\\') {             index++;         }         return index;     }      public static void main(string[] args) {         jframe frame = new jframe();         label = new jlabel(formatlabeltext(0));         label.addmousemotionlistener(new mousemotionadapter() {             public void mousemoved(mouseevent e) {                 accessibletext accessibletext =                         label.getaccessiblecontext().getaccessibletext();                 point p = e.getpoint();                 int index = accessibletext.getindexatpoint(p);                 index = roundindex(index);                 label.settext(formatlabeltext(index));             }         });         frame.add(label);         frame.setsize(200, 200);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);     } } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -