I have this notebook class that I have been working on. I have two problems that I'm facing right now:
I have two icons in my toolbar that make the text bolded or italicized when you click on it. All of that works fine, however, It always selects all the text in the document rather than specifically selected text. Is there a way I can use the blue highlight of a left click on a mouse in order to bold or italicize specific text? This is the code for the bolding Abstract Action. The italics looks exactly the same, except for italics.
Action Bold = new AbstractAction("Bold", new ImageIcon("bold.png"))
{
public void actionPerformed(ActionEvent e)
{
if(bolded == false)
{
area.setFont(area.getFont().deriveFont(Font.BOLD));
bolded = true;
}
else
{
area.setFont(area.getFont().deriveFont(Font.PLAIN));
bolded = false;
}
}
};
I want to add an actual highlighter that will just paint certain groups of words that the user selects yellow. I've read through the Oracle page on this, and I'm still not all that sure about using it. I see a lot of examples of people searching for specific words and highlighting it that way, but I'm not looking to highlight these specific words. I want the user to decide which text to highlight.
Action Highlight = new AbstractAction("Highlight", new ImageIcon("highlighter.png"))
{
public void actionPerformed(ActionEvent e) throws BadLocationException
{
Highlighter highlighter = area.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
highlighter.addHighlight(0 , 6, painter);
}
};
The code above is what I managed to pull together from some other tutorials online, however, the BadLocationException doesn't compile right when it is inside the Abstract Action, so this isn't looking like a viable option.
Any help is appreciated!
actionPerformed
does not throw any checked Exceptions.
Simply remove the exception and catch it inside the method.
public void actionPerformed(ActionEvent e)
{
try {
Highlighter highlighter = area.getHighlighter();
HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
highlighter.addHighlight(0 , 6, painter);
catch(throws BadLocationException ex) {
ex.printStackTrace();
}
}
}