Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8792 > unrolled thread
| Started by | "John L." <johnlarew@sbcglobal.net> |
|---|---|
| First post | 2011-10-14 07:15 -0700 |
| Last post | 2011-10-14 09:44 -0700 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | "John L." <johnlarew@sbcglobal.net> |
|---|---|
| Date | 2011-10-14 07:15 -0700 |
| Subject | Attempting to set the color of a JTextField |
| Message-ID | <c72ebf3b-6aad-457c-be87-5d7a992e0d9e@h5g2000vbf.googlegroups.com> |
I'm trying to change the color of a JTextField. There are many
interesting discussions on much more detailed topics in other posts.
Here is my best attempt, which does NOT work as I might reasonably
expect:
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) {
g2.setPaint(Color.RED);
g2.drawString(s1,0,0);
}
}
}
Thanks in advance for any help or insight you may be able to provide.
[toc] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2011-10-14 07:21 -0700 |
| Message-ID | <j79ghg$4lm$1@dont-email.me> |
| In reply to | #8792 |
On 10/14/2011 7:15 AM, John L. wrote:
> I'm trying to change the color of a JTextField. There are many
> interesting discussions on much more detailed topics in other posts.
>
> Here is my best attempt, which does NOT work as I might reasonably
> expect:
>
> 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) {
> g2.setPaint(Color.RED);
> g2.drawString(s1,0,0);
> }
> }
> }
>
> Thanks in advance for any help or insight you may be able to provide.
See setOpaque(), setForeground() and setBackground()
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | "John L." <johnlarew@sbcglobal.net> |
|---|---|
| Date | 2011-10-14 08:05 -0700 |
| Message-ID | <b33e0b12-fcef-46c5-88c7-8f1dcd45a19f@h14g2000yqi.googlegroups.com> |
| In reply to | #8794 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2011-10-14 10:42 -0700 |
| Message-ID | <Yv_lq.763$AF3.464@newsfe12.iad> |
| In reply to | #8795 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "John L." <johnlarew@sbcglobal.net> |
|---|---|
| Date | 2011-10-19 07:16 -0700 |
| Message-ID | <fa171857-1671-49e5-ac22-72775735897d@n13g2000vbv.googlegroups.com> |
| In reply to | #8801 |
Thanks, I thought I tried that initially, before attempting this more involved technique. Whatever the case, it works as reasonably expected now.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-10-14 09:44 -0700 |
| Message-ID | <gjpg9719ghi1ekci3687tmdq155ph65c0r@4ax.com> |
| In reply to | #8792 |
On Fri, 14 Oct 2011 07:15:11 -0700 (PDT), "John L."
<johnlarew@sbcglobal.net> wrote, quoted or indirectly quoted someone
who said :
>I'm trying to change the color of a JTextField. There are many
>interesting discussions on much more detailed topics in other posts.
There is no need to override paintComponent just to change colours:
final JFrame jFrame = new JFrame();
final Container contentPane = jFrame.getContentPane();
final JTextField textfield = new JTextField( "this is a
TEST" );
textfield.setBackground( Color.BLACK );
textfield.setForeground( Color.YELLOW );
see http://mindprod.com/jgloss/jtextfield.html
If fooling around with paintComponent just to experiment
see http://mindprod.com/jgloss/paintcomponent.html
The trick is setOpaque.
--
Roedy Green Canadian Mind Products
http://mindprod.com
It should not be considered an error when the user starts something
already started or stops something already stopped. This applies
to browsers, services, editors... It is inexcusable to
punish the user by requiring some elaborate sequence to atone,
e.g. open the task editor, find and kill some processes.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web