Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8801

Re: Attempting to set the color of a JTextField

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: Attempting to set the color of a JTextField
References <c72ebf3b-6aad-457c-be87-5d7a992e0d9e@h5g2000vbf.googlegroups.com> <j79ghg$4lm$1@dont-email.me> <b33e0b12-fcef-46c5-88c7-8f1dcd45a19f@h14g2000yqi.googlegroups.com>
Message-ID <Yv_lq.763$AF3.464@newsfe12.iad> (permalink)
Date 2011-10-14 10:42 -0700

Show all headers | View raw


On 10/14/11 8:05 AM, John L. wrote:
> I might report that this new reasonable attempt failed also:
>
> class ITextField extends JTextField
> {
>     private String s1 = null;
>     public void setText(String s) {
>        s1 = s;
>     }
>     public void paintComponent(Graphics g) {
>        super.paintComponent(g);
>        Graphics2D g2 = (Graphics2D) g;
>        if (s1 != null) {
>           setOpaque(true);
>           setForeground(Color.RED);
>           g2.drawString(s1,0,0);
>           repaint();
>        }
>     }
> }
>
> BTW, the background is not red.
>
> Thanks in advance for any help or insight you may provide.

Don't set those in the paintComponent method, they are meant to be set 
by the code constructing the field.  Extending JTextField is the wrong 
approach.


JTextField textField = new JTextField();
textField.setForeground(Color.RED);


That should do what you want.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Attempting to set the color of a JTextField "John L." <johnlarew@sbcglobal.net> - 2011-10-14 07:15 -0700
  Re: Attempting to set the color of a JTextField Knute Johnson <nospam@knutejohnson.com> - 2011-10-14 07:21 -0700
    Re: Attempting to set the color of a JTextField "John L." <johnlarew@sbcglobal.net> - 2011-10-14 08:05 -0700
      Re: Attempting to set the color of a JTextField Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-14 10:42 -0700
        Re: Attempting to set the color of a JTextField "John L." <johnlarew@sbcglobal.net> - 2011-10-19 07:16 -0700
  Re: Attempting to set the color of a JTextField Roedy Green <see_website@mindprod.com.invalid> - 2011-10-14 09:44 -0700

csiph-web