JTextField.paintComponent

A

Aaron Fude

Hi,

If my overridden paintComponent starts with
public void paintComponent(Graphics g) {
super.paintComponent(g);


should that draw the right background color. I thought yes, but I
discovered no and I have to do?

g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());

Am I doing the right thing or missing something?
 
J

John B. Matthews

Aaron Fude said:
If my overridden paintComponent starts with
public void paintComponent(Graphics g) {
super.paintComponent(g);

should that draw the right background color. I thought yes, but I
discovered no and I have to do?

g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());

Am I doing the right thing or missing something?

The API for clearRect() suggests that your approach is correct for the
background color of offscreen images, as might apply in the presence of
double buffering:

<http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html>
 
K

Knute Johnson

Aaron said:
Hi,

If my overridden paintComponent starts with
public void paintComponent(Graphics g) {
super.paintComponent(g);


should that draw the right background color. I thought yes, but I
discovered no and I have to do?

g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());

Am I doing the right thing or missing something?

On Windows, a JTextField is opaque by default. If I create one and set
its background color, whether or not I override paintComponent() it
draws the background color unless I leave out the super.paintComponent().

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

public class test extends JFrame {
public test() {
setLayout(new GridBagLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.RED);
JTextField tf = new JTextField("Hello World"); /* {
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
};
*/
tf.setBackground(Color.BLUE);
add(tf);
pack();
setVisible(true);
}

public static void main(String[] args) {
new test();
}
}

Maybe you could write an SSCCE to demonstrate your problem.
 

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,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top