Clickable text in JTextArea?
Hi all,
I'm writing a program that populates a textarea with medical terms and such. And, the problem is I need to give an explanation to the more difficult words by allowing the user to click on the words and a popup window with the explanations will appear. I'm not familiar with the mouse clicks technology in Java, so please help if you can!
Also, any suggestions on how the data can be stored/retrieved when I'm using a MySQL database?
Many thanks!
[481 byte] By [
marc79] at [2007-9-30 20:52:25]

For this you have to add mouse listener in a text area. So that on mouse click you can do something.
try following code, Run it and double click on some word.
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class TextAreaDemo extends JPanel{
private JTextArea text = null, answer = null;
public TextAreaDemo(){
text=new JTextArea("Hi all,\nI'm writing a program that populates a textarea with medical terms and " +
"such. And, the problem is I need to give an explanation to the more difficult words " +
"by allowing the user to click on the words and a popup window with the explanations " +
"will appear. I'm not familiar with the mouse clicks technology in Java, so please " +
"help if you can!\n\nAlso, any suggestions on how the data can be stored/retrieved " +
"when I'm using a MySQL database?\n\nMany thanks!");
answer=new JTextArea(3, 20);
answer.setEditable(false);
setLayout(new BorderLayout());
add(new JScrollPane(text));
add(new JScrollPane(answer), BorderLayout.SOUTH);
text.addMouseListener(new MyMouseListener());
}
class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 2){
answer.setText("Do you want to know about:\n\t" + text.getSelectedText());
}
}
}
public static void main(String argv[]){
JFrame frame=new JFrame();
frame.setContentPane(new TextAreaDemo());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
May help you.
Regards,