File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes setSelectionStart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "setSelectionStart" Watch "setSelectionStart" New topic
Author

setSelectionStart

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi,
I am trying to use setSelectionStart and setSelectionEnd of the JTextComponent class. I want to highlight some text in theTextArea(JTextPane). I cannot get this to work. Can someone please tell me what I am missing here? Thanks a lot!!! Note: I am getting 'Test' and caret in the console, so at least those things are working.
Barry
class TestListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Test");
int caret = theTextArea.getCaretPosition();
System.out.println(caret);
theTextArea.setSelectionColor(Color.red);
theTextArea.setSelectionStart(caret);
theTextArea.setSelectionEnd(caret + 10);
theFrame.show();
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
You are on the right track ... Your code is correct, but you need to make sure that:
SelectStart >= 0
and that
SelectEnd <= allText.length()
If either is false, then nothing will happen. I think that you are using a TextField because of the ActionListener. You are probably typing something and then hitting return. If your caret is not positioned at least 10 characters from the end of your text then your selection won't happen. Try typing at least 20 characters and moving the caret somewhere closer to the front of your text before hitting return.
Good Luck,
Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Manfred,
I don't think that is the problem. I can do a replaceSelection() method, and that does work. So my setSelectionStart() and setSelectionEnd() methods are definitely working. What I am trying to do is actually highlight the selection without replacing anything. Maybe there is another method I am supposed to use in addition to these? I have also tried theTextArea.repaint(). Can you think of anything else I can try? Thanks!!!
Barry
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Barry,
Your code works for me ... without the frame.show().
I am running my program from the MSDOS prompt. If you are running from within some Java IDE it might be stopping the repaint method from firing when you make the selection changes.
Sorry,
Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Another strange puzzle........... I have also tried from the command line and it does not work for me. Hmmmmmmm.......... I will keep trying and hopefully I can get it to work. If you have anything else you can think of, the comments will be greatly appreciated.

Barry
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Barry,
Just to make sure we are on the same page. Here is the code that works for me (JDK1.2 from DOS prompt).

Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Manfred,
Your code absolutely does work. I even changed the JTextField to a JTextPane(DefaultStyledDocument) which is what I am working with. So............ there must be something in my project that I am not seeing. I will go back and keep trying to figure out what it is. Thanks for the test case!
Barry
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

FYI....... On my project I was doing my test with an ActionListener hooked up to a Button. The ActionEvent was getting fired no problem, but the selection was not taking place. However, when I hooked a MouseListener to the JTextPane and fired a mouseClicked event the selection does happen. Do you have any idea why? Anyway, this will work for what I want to do........ Yeah!!! Thanks!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Barry,
The only thing you might have tried would be to call repaint on the textpane inside your button actionPerformed routine. This might have made the selection show up in the textpane.
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setSelectionStart