How to make selected text in JTextArea into a String?
Solution 1
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
}
}
Solution 2
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.
Solution 3
You can use JTextComponent#setCaretPosition followed by JTextComponent#moveCaretPosition to highlight/select
Solution 4
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.
Comments
-
applemavs almost 2 years
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(); } }
-
Brandon Buck about 11 yearsI realize my answer is geared more towards your end goal and not the question you asked, sorry about that.
-
applemavs about 11 yearsDon't be sorry, I actually switched all my
JTextArea
s toJTextPane
s even before I saw your comment and I'm learning to use Attributes and Styles now. Thanks for giving me the basic information forJTextPane
s in your answer :D
-
-
Brandon Buck about 11 yearsThis isn't much of an answer, but that doesn't make it invalid. I doubt that a
JEditorPane
is what the OP was looking for though. -
applemavs about 11 yearsThank you so much, this is exactly what I needed!
-
mKorbel about 11 years@izuriel this answer can going correct way, JTextPane/JEditorPane has built-in support for HighLighter for OPs ask
how to make individual blocks of text bold, italics, or different font sizes instead of the whole block of text
, please more see in Oracle tutorial -
Protostome about 11 years@izuriel - The author of the question was mentioning the fact that he's building a small scale word processor. I doubt that the OP desires a word processor that functions like gedit or notepad.
-
Brandon Buck about 11 years@Protostome
JTextPane
is a better option for a text editor than aJEditorPane
which is a simple HTML processor. @mKorbel, I'm quite aware you must not have paid any attention to my answer.