I'm working on a simple word processor with java swing and layouts, and I'm trying to figure out how to make individual blocks of text bold, italics, or different font sizes instead of the whole block of text changing at once in my JTextArea.
Is there some way to initialize a String as the user highlights the text in the JTextArea with their mouse? I would love it if there was some sort of ActionListener or something for JTextArea which could detect all this and easily save anything as a string, but I'm not sure if this is possible. Something like this would be great:
String selectedtext;
JTextArea type;
class TextPanel extends JPanel implements ActionListener
{
public TextPanel()
{
type = new JTextArea();
type.addActionListener(this);
this.add(type);
}
public void actionPerformed(ActionEvent e)
{
selectedtext = e.getSelected();
}
}
JTextArea doesn't have any built-in functionality that will do this, but:
In order for someone to select text, they have to click on the text area, drag and release the click. So, attach a MouseListener and implement the mouseReleased method to check if any text was selected, and if so to save it as a string:
public void mouseReleased(MouseEvent e) {
if (textArea.getSelectedText() != null) { // See if they selected something
String s = textArea.getSelectedText();
// Do work with String s
}
}
You're not going to be able to accomplish this with a JTextArea
, you will need something that supports rich display of text like a JTextPanel
and you'll need to define styles for it, applying these styles to specific regions.
Here is an example of a utility class for created styles (linked to give example of defining styles). The addNewStyle
and changeFont
are the two most important methods to reference. The addNewStyle
method shows how to add a predefined style to the document that you can reference when inserted (mostly for pasting should you want to past with a format). The changeFont
method shows how to create a style and apply it to a region (in the method the region is from 0
to the end of the document - so the entire document).
You'll probably need these styles to be crafted dynamically and so you'll need to fetch them from the region if one exists (that I've not done). All of this is done with a StyledDocument
And example of appending text with a style to a StyledDocument
(purely for example) is:
styledDocument.insertString(
styledDocument.getLength(), textToInsert,
styledDocument.getStyle(styleName));
It's been some time since I worked with JTextPane
s and StyledDocuments
so most of this is pulled from the project where I did the work. I wish I could give you more info rather than just a starting point.
You can use JTextComponent#setCaretPosition followed by JTextComponent#moveCaretPosition to highlight/select
For detecting selection changes in JTextArea, it would be better if you make use of the CaretListener.
jTextArea.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent ce)
{
int dot=ce.getDot();
int mark=ce.getMark();
if(dot!=mark)
selectedText=jTextArea.getSelectedText();
else selectedText=null;
}
});
Now, if you want to do some operations with the selected text when mouse is dragged, you could do it, because the selectedText is updated.
JTextArea doesn't have that capability. You should look at
JEditorPane
It can display html , so you can use bold tags and whatever you need..