0

I am making a text application where I can change the text to bold (I will add more in the future).

I put the bold option in a JMenu as a JCheckBox.

I want this checkbox to get selected or deselected depending on whether the insertion point (the blinking line) is on a bold or normal text. Just like it is in Microsoft Word.

The application

Enes T.
  • 137
  • 2
  • 12
  • Could you maybe provide some code for us to see, not just a screenshot? – David Apr 23 '16 at 22:25
  • You need to determine the current style based on the location of the caret, something like [this](http://stackoverflow.com/questions/24163370/how-to-get-style-from-jtextpane) might give you a jumping off point, or [this example](http://stackoverflow.com/questions/4360234/get-style-at-caret-position-in-a-jtextpane) or [this example](http://stackoverflow.com/questions/4819780/getting-style-from-any-offset-in-jtextpane) – MadProgrammer Apr 23 '16 at 22:27
  • 1
    Be sure you are using the `StyledEditorKit` actions, seen [here](http://stackoverflow.com/a/8534162/230513). – trashgod Apr 23 '16 at 23:08

1 Answers1

0

Quick answer from my phone: You need to add a CaretListener to the document. When triggered, you will detect the character style with StyledDocument.getCharacterAttributes. To get the bold attribute, you check it with StyleConstants.isBold. Be careful to not trigger your bold action while setting the check box state.

Sharcoux
  • 5,546
  • 7
  • 45
  • 78