Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe12.iad.POSTED!83aa503d!not-for-mail From: Daniel Pitts User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Attempting to set the color of a JTextField References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 35 Message-ID: X-Complaints-To: abuse@newsrazor.net NNTP-Posting-Date: Fri, 14 Oct 2011 17:42:16 UTC Date: Fri, 14 Oct 2011 10:42:15 -0700 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8801 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.