At 11:02 AM 3/12/2001, Reynshteyn, Leonid wrote:
>How can I do it if the component has focus?

er... selectAll() should do it (the code below works just find for 
me).  What JDK are you using?

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

public class test extends JPanel
{
    public static void main( String[] args )
    {
       JFrame f = new JFrame( "Test" );
       f.setContentPane( new test() );
       f.setBounds( 100, 100, 100, 100 );
       f.show();
    }

    public test()
    {
       final JTextField a = new JTextField("this is a test");

       add( a );

       JButton b;
       add(b= new JButton("Select") );
       b.setRequestFocusEnabled(false);
       b.addActionListener( new ActionListener() {
          public void actionPerformed( ActionEvent ev )
          {
             a.selectAll();
          }
       } );
    }
}

>-----Original Message-----
>From: Dmitry Beransky [mailto:[EMAIL PROTECTED]]
>Sent: Monday, March 12, 2001 1:30 PM
>To: [EMAIL PROTECTED]
>Subject: Re: highlighting text in TextField
>
>
>The default implementation of the Caret shows highlighting only when the
>corresponding component has focus.  If you want the highlighting effect on
>an unfocused component your options are: 1) writing your own Caret; 2) fake
>the highlighting by playing with the background/foreground colors.
>
>Dmitry
>
>At 10:16 AM 3/12/2001, Reynshteyn, Leonid wrote:
> >Can someone suggest the simplest way to programmatically highlight text in
> >JTextField?
> >
> >selectAll() highlights the text internally but does not display the text
> >highlighted. I want it to appear as if it's been highlighted with a mouse.
>
>_______________________________________________
>Advanced-swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/advanced-swing
>_______________________________________________
>Advanced-swing mailing list
>[EMAIL PROTECTED]
>http://eos.dk/mailman/listinfo/advanced-swing

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to