To select a line with double click - JTextArea
Hi! I have a question.
I thought I found how to select a line with double click, but it just wont do it for me. I mean I get the text of the line but I need it highlighted too.
textArea.addMouseListener(new MouseAdapter()
{
publicvoid mouseClicked(MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2))
{
int offset = infoTextArea.viewToModel(e.getPoint());
int start = Utilities.getRowStart(infoTextArea, offset);
int end = Utilities.getRowEnd(infoTextArea, offset);
String line = infoTextArea.getDocument().getText(start,end - start);
infoTextArea.select(start,end);
// but only thing I get is selected word from double click
infoTextArea.revalidate();
// and then that line is looked up in a big data base
// it can take few second
// result is set as new text of textArea
// so I need the line highlighted while this is beign processed
}
}
});
Any ideas? I strugling with this for a while. I would appreciate any kind of help.
[1652 byte] By [
Element9a] at [2007-10-3 5:15:12]

As you've noticed, by default a double click causes the word to be selected. By default you get line selection with a triple click.
I don't know how to remove actions assigned to mouse clicks but you should be able have your code execute by placing your code in a SwingUtilities.invokeLater(...). This adds your code to the end of the EDT, which meas is should execute after the default double click processing is finished.
Note, you don't need to use revalidate() to highlight the text. revalidate() is only used after you add or remove a component from a Container.
That SwingUtilities.invokeLater() is new for me. I'll look for it in docs.
Here is even simpler version that still doesn't work:
textArea.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e) && (e.getClickCount() == 2))
{
int offset = infoTextArea.viewToModel(e.getPoint());
int start = Utilities.getRowStart(infoTextArea, offset);
int end = Utilities.getRowEnd(infoTextArea, offset);
String line = infoTextArea.getDocument().getText(start,end - start);
textArea.setText("Searching...");
//program ignores this line
// and then that line is looked up in a big data base...
}
}
});
It goes from the initial text to text set after processing and while it's processing I am looking into text with a word highlighted by the double click.
I wasn't sure about that revalidate() - now I know. Thanx