Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: select text cross multiple textareas

Tags:

java

select

swing

Let's say we have two JTextArea in a JPanel, I want the user to be able to select text cross these two JTextArea using mouse in one select, Is it possible in Swing?

Regards

so far I have tried:

I tried add mouselistener to these JTextArea, when mouseEnter, I select text in that TextArea, and highlight the text, pragmatically. This is both hard and error prone, I think maybe there is a better way

EDIT:

Thanks for all the answers and suggestions. Based on the answers, I implemented a basic solution and put it as an answer for other's reference. :)

like image 454
didxga Avatar asked Feb 13 '23 14:02

didxga


2 Answers

that is the text in the first textarea remain selected (text is in hightlight), when user is selecting text in the second one

I assume you mean the user is selecting different pieces of text in the two different text areas.

By default text components only show the selected text when the text component has focus.

The simple answer is to create a custom Caret and override the focusLost(...) method and invoke setSelectionVisible(true) at the end of the method to make sure the selection is painted even when the text component doesn't have focus.

Here is a fancier version of that approach that will allow you to specify a different selection background Color for the text component that doesn't have focus (if you wish):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class SelectionCaret extends DefaultCaret
{
    private static final Color SELECTION_COLOR = UIManager.getColor("TextField.selectionBackground");

    private Highlighter.HighlightPainter focusedPainter;
    private Highlighter.HighlightPainter unfocusedPainter;

    public SelectionCaret()
    {
        this(SELECTION_COLOR);
    }

    public SelectionCaret(Color unfocusedColor)
    {
        focusedPainter = new DefaultHighlighter.DefaultHighlightPainter(SELECTION_COLOR);
        unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(unfocusedColor);

        setBlinkRate( UIManager.getInt("TextField.caretBlinkRate") );
    }

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter()
    {
        return getComponent().hasFocus() ? focusedPainter : unfocusedPainter;
    }

    @Override
    public void focusGained(FocusEvent e)
    {
        setSelectionVisible(false);
        super.focusGained(e);
    }

    @Override
    public void focusLost(FocusEvent e)
    {
        super.focusLost(e);
        setSelectionVisible(true);
    }

    private static void createAndShowUI()
    {
        JTextField textField1 = new JTextField("Text Field1   ");
        JTextField textField2 = new JTextField("Text Field2   ");
        JTextField textField3 = new JTextField("Non Editable   ");
        textField3.setEditable(false);

        textField1.setCaret(new SelectionCaret());
        textField2.setCaret(new SelectionCaret());
        textField3.setCaret(new SelectionCaret());

        textField1.select(5, 11);
        textField2.select(5, 11);
        textField3.select(5, 11);
        ((DefaultCaret)textField1.getCaret()).setSelectionVisible(true);
        ((DefaultCaret)textField2.getCaret()).setSelectionVisible(true);
        ((DefaultCaret)textField3.getCaret()).setSelectionVisible(true);

        JPanel north = new JPanel();
        north.add( new JTextField("Text Field0   ") );
        north.add(textField1);
        north.add(textField2);
        north.add(textField3);

        JFrame frame = new JFrame("Selection Caret");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( north );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

The above code was based on the code provided by mKorbel in this posting.

like image 127
camickr Avatar answered Feb 16 '23 05:02

camickr


Probably you will have to work registering an event handler in one JTextArea, maybe there is one specific for the kind of event that belong to text selection, and then programmatically propagate the same effect on the other JTextArea. But there is no more magic beyond that.

For the event type you are looking, look forward the keywords: Swing JTextArea event listener for text selection

  • Which event a selection of text trigger in Java JTextArea?
  • Detecting text selection in JTextArea

If my memory doesn fail, there the JTextField cames with a method for selecting the text in a range.

For this kind of problem, look for : Selecting text in jTextField

  • http://forums.codeguru.com/showthread.php?308517-How-do-you-highlight-the-text-in-a-JTextfield

I thing that this way i'll strike the problem.

Hope it helps!!

Greetings!.

like image 33
Victor Avatar answered Feb 16 '23 05:02

Victor