java - Prevent typing in the same style in JTextPane -


forgive me possible misleading title, problem bit hard describe.

i'm trying create basic texteditor using jtextpane in java , i've run issue.

as know in texteditors can put caret/cursor behind piece of styled text (for example text bold) , can continue typing in same style (eg. append more bold characters).

luckely present default in jtextpane, want disable style. url-style coded (basicly 1 sets html.attribute.href attribute in style url).

so if put caret behind word (or piece of text) url, want ensure next characters added, not in url-style.

eg. think tinymce has behaviour:

  • you select text
  • click on insert url button
  • insert url
  • place cursor right after url , start typing again in normal style

is there way enforce behaviour in jtextpane?

i thinking this:

  • adding listener content changes in document
  • check if added characters placed right behind piece of text urlstyle
  • if case => remove "href" attribute style of characters

the code use setting url-style selected text can found below. "dot" , "mark" retrieved caret.

        simpleattributeset attr = new simpleattributeset(doc.getcharacterelement(dot).getattributes());         styleconstants.setunderline(attr, true);         styleconstants.setforeground(attr, color.blue);         attr.addattribute(html.attribute.href, url);         doc.setcharacterattributes((dot < mark) ? dot : mark, length, attr, true); 

(note: able tell difference between normal "blue underlined" text , url, href attribute used url.)

ps: first question here, gave enough information. ;)

language: java, jdk 1.7

thanks in advance.

add caretlistener detect move , check whether current caret position needs style reset. if it's detected use

stylededitorkit's method

public mutableattributeset getinputattributes() 

here remove attributes don't need (url, blue, underline).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -