Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make selected text in JTextArea into a String?

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();
    }
}
like image 820
applemavs Avatar asked Oct 18 '25 06:10

applemavs


2 Answers

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
    }
}
like image 69
drew moore Avatar answered Oct 19 '25 20:10

drew moore


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.

like image 41
Brandon Buck Avatar answered Oct 19 '25 22:10

Brandon Buck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!