Issue
Just like in Word or WYSIWYG editor, I want the ability to make TextArea
perform a simple bullet list.
Solution
I was able to figure it out using RichTextFX StyleClassedTextArea
:
public static final String BULLET_CODE = " \u2022\t";
textArea.setOnKeyPressed(event ->
{
KeyCode code = event.getCode();
if (KeyCode.ENTER.equals(code))
{
textArea.moveTo(textArea.getCaretPosition() - 1);
textArea.selectParagraph();
if (StringUtils.startsWith(textArea.getSelectedText(), BULLET_CODE))
{
textArea.moveTo(textArea.getCaretPosition() + 1);
textArea.lineStart(NavigationActions.SelectionPolicy.CLEAR);
textArea.insertText(textArea.getCaretPosition(), BULLET_CODE);
} else
{
textArea.moveTo(textArea.getCaretPosition() + 1);
}
textArea.deselect();
}
});
And adding a button that inserts a bullet:
addBullet.setOnAction((ActionEvent t) ->
{
textArea.selectLine();
if (!StringUtils.startsWith(textArea.getSelectedText(), BULLET_CODE))
{
textArea.deselect();
textArea.lineStart(NavigationActions.SelectionPolicy.CLEAR);
textArea.insertText(spellCheckArea.getTextArea().getCaretPosition(), BULLET_CODE);
textArea.lineEnd(NavigationActions.SelectionPolicy.CLEAR);
}
textArea.deselect();
textArea.requestFocus();
});
Answered By - trilogy
Answer Checked By - Senaida (JavaFixing Volunteer)