Мне нужно подчеркнуть выделенное слово в моем приложении, просто щелкнув слово или выбрав фрагмент текста.
, поэтому моей первой идеей было использование JLabel
, потому что оно анализирует тексткак HTML
, но я не узнал, как узнать выбранное слово, тогда я попытался JEditorPane
, и это сработало, но не так, как ожидалось, поскольку я использовал MouseListener
mouseReleased()
, чтобы подчеркнуть выделенный текст.
public void mouseReleased (MouseEvent e){
if (jEditorPane.getSelectedText()==null ||jEditorPane.getSelectedText().equals(""))
return;
//to know the get real location of the text
//because by default the editorpane will add the rest of the html elements
// to the text to make look like a proper page
int x1=jEditorPane.getText().indexOf("<body>")+"<body>".length();
int x2=jEditorPane.getText().indexOf("</body>");
//the editor pane will add few white spaces after the body tag
String trim = (jEditorPane.getText().subSequence(x1, x2)+"").trim();
int selectionStart = jEditorPane.getSelectionStart();
int selectionEnd = jEditorPane.getSelectionEnd();
String text = trim;
String beg = text.substring(0,selectionStart);
String mid = "<U>"+text.substring(selectionStart,selectionEnd)+"</U>";
String end = text.substring(selectionEnd,text.length());
jEditorPane.setText(beg+mid+end);
}
в выбранном тексте неточности!некоторые из моего выбора подчеркнуты, а некоторые нет, мне нужно сделать его точным или подчеркнуть выделенное слово и заранее поблагодарить. (JEditorPane
не требуется, если у вас есть идея получше)