Hey i have some questions/problems.
I have to create a little program for text editing. The (selected) text should be style. Bold, Italic, Underline, Right- left- center alignment. It works great. I used the specific StyleEditorKit Actions.
My problem is now that this actions are fired through buttons in a jtoolbar and jmenuitems in a jmenu / jmenubar.
So there are two click elements to set a text bold, two elements to set a text italic and so on. If one element (e.g. the button in the toolbar) is clicked, the jmenuitem should be selected/activated too. But how can i realize this?
My idea is to check the selected text (CaretListener is implemented). If the text is bold => set the button and menuitem active. But how can i get if the selectedText is bold/italic etc?
I think there is a StyledDocument tree with leafs for this stuff. But how can i get this tree? how can i get the leafs?
This are my first steps:
jTextPane1.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
Highlight[] h = jTextPane1.getHighlighter().getHighlights();
for(int i = 0; i < h.length; i++) {
System.out.println(h[i].getStartOffset());
System.out.println(h[i].getEndOffset());
String selectedText = jTextPane1.getSelectedText();
StyledDocument styleddoc = (StyledDocument) jTextPane1.getDocument();
System.out.println(styleddoc);
}
}
});
But i only get javax.swing.text.DefaultStyledDocument@5098cb76
How can i iterate over the tree and get the leafs / bold or italic elements?
Thank you
In your CaretListener you can use:
You should be using an
Action
:When you set the state of an
Action
, the state of all components using theAction
is changed. For example you can make theAction
selected or enabled.