Get the selected word -Java-
sarounette
-
sarounette -
sarounette -
Hello everyone,
My application is as follows:
I have a JTextArea in which I will display sentences. If the user selects a word in this JTextArea, I need to retrieve that "selected word" to submit it for certain processing. I know that I need to use a listener, but which one?? And how to do it? I found a lot of tutorials, but I don't see exactly how to do it... Is there anyone who can help me please!?
My application is as follows:
I have a JTextArea in which I will display sentences. If the user selects a word in this JTextArea, I need to retrieve that "selected word" to submit it for certain processing. I know that I need to use a listener, but which one?? And how to do it? I found a lot of tutorials, but I don't see exactly how to do it... Is there anyone who can help me please!?
3 answers
Hello!
Here is an example of implementation. Double-click on a word or select a portion of text with the mouse, and the selected text will appear at the bottom of the window.
As for you, you need to add a MouseListener to your JTextArea. Retrieve the selected text in the "mouseReleased(MouseEvent e)" method of the MouseListener (see code below).
Once you have retrieved the text, you can do whatever you want with it.
Here is an example of implementation. Double-click on a word or select a portion of text with the mouse, and the selected text will appear at the bottom of the window.
As for you, you need to add a MouseListener to your JTextArea. Retrieve the selected text in the "mouseReleased(MouseEvent e)" method of the MouseListener (see code below).
Once you have retrieved the text, you can do whatever you want with it.
package hacktrack; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class TextSelectDemo extends JFrame implements MouseListener { private JTextArea textarea; private JTextField textfield; public TextSelectDemo() { super("HackTrack - Text selection demo - 17/05/2012"); initialize(); } private void initialize() { setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new BorderLayout()); textarea = new JTextArea(10, 8); textarea.setLineWrap(true); textarea.addMouseListener(this); textarea.setText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"); c.add(textarea, BorderLayout.CENTER); textfield = new JTextField(); c.add(textfield, BorderLayout.SOUTH); setPreferredSize(new Dimension(640,480)); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { JTextArea jta = (JTextArea) e.getSource(); String selectedTxt = jta.getSelectedText(); textfield.setText(selectedTxt); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public static void main(String[] args) { TextSelectDemo demo = new TextSelectDemo(); demo.pack(); demo.setVisible(true); } }