java - jTextArea stops showing highlighting on text after losing focus -


when jtextarea in focus allows text highlighting, doesn't show text selection when loses focus. possible continue displaying text highlighting if user moves focus component on related jframe?

but doesn't show selection on text when looses focus.

there 3 ways:

enter image description here

  • or programatically override highlighter

enter image description here

import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.text.badlocationexception; import javax.swing.text.defaulthighlighter; import javax.swing.text.highlighter; import javax.swing.text.jtextcomponent;  public class multihighlight implements actionlistener {      private jtextcomponent comp;     private string charstohighlight;      public multihighlight(jtextcomponent c, string chars) {         comp = c;         charstohighlight = chars;     }      @override     public void actionperformed(actionevent e) {         highlighter h = comp.gethighlighter();         h.removeallhighlights();         string text = comp.gettext().touppercase();         (int j = 0; j < text.length(); j += 1) {             char ch = text.charat(j);             if (charstohighlight.indexof(ch) >= 0) {                 try {                     h.addhighlight(j, j + 1, defaulthighlighter.defaultpainter);                 } catch (badlocationexception ble) {                 }             }         }     }      public static void main(string args[]) {         final jframe frame = new jframe("multihighlight");         frame.add(new jtextfield("another focusable jcomponents"), borderlayout.north);         jtextarea area = new jtextarea(10, 20);         area.settext("this story\nof hare who\nlost spectacles."                 + "\nthis story\nof hare who\nlost spectacles.");         frame.getcontentpane().add(new jscrollpane(area), borderlayout.center);         jbutton b = new jbutton("highlight vowels");         b.addactionlistener(new multihighlight(area, "aeiouaeiou"));         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.getcontentpane().add(b, borderlayout.south);         swingutilities.invokelater(new runnable() {             @override             public void run() {                 frame.pack();                 frame.setvisible(true);             }         });     } } 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -