Attempting to set the color of a JTextField

J

John L.

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.
 
K

Knute Johnson

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()
 
J

John L.

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.
 
R

Roedy Green

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.
 
D

Daniel Pitts

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.
 
J

John L.

Thanks, I thought I tried that initially, before attempting this more
involved technique.

Whatever the case, it works as reasonably expected now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top