Hi folks,
according to the Java API docs, a component should call paint() whenever
it needs refresh, and a swing component should call paintComponent() for
this purpose. I now need to render a tiny graphic (manually) into a
JFrame and to this end overrode paint() in JFrame. Interestingly, this
does not work correctly, the JFrame doesn't seem to call this custom
hook correctly. Sometimes my graphics is redrawn, sometimes it isn't.
The same effect happens when overriding paintComponent(): Doesn't work
reliable.
It *does* work reliable if I install a JPanel or a Canvas into the
content pane of the JFrame and override paint() there. (Thus, problem
solved on my side so far)
However, concerning the JFrame paint() method, am I missing something or
is this a "feature" (not to call it "bug")?
So long,
Thomas
SadRed - 17 Oct 2007 11:06 GMT
> Hi folks,
>
[quoted text clipped - 16 lines]
> So long,
> Thomas
Do like this:
myJframe.getContentPane().add(new DrawPanel(), BorderLayout.center);
--------
class DrawPanel extends JPanel{
...
public void paintComponent(Graphics g){
...
// you do drawing here
}
...
}
Thomas Richter - 17 Oct 2007 12:29 GMT
SadRed schrieb:
> Do like this:
> myJframe.getContentPane().add(new DrawPanel(), BorderLayout.center);
[quoted text clipped - 7 lines]
> ...
> }
Yes, thanks, I know that this works. The question is rather, why doesn't
JFrame.paint() work? Is this a *bug* or am I missing something?
So long,
Thomas
Lew - 17 Oct 2007 15:34 GMT
> SadRed schrieb:
>
[quoted text clipped - 12 lines]
> Yes, thanks, I know that this works. The question is rather, why doesn't
> JFrame.paint() work? Is this a *bug* or am I missing something?
It might be a bug, but likely not in Swing.
Have you checked the Java bugbase?
According to the Javadocs, the main purpose of JFrame's paint() is to forward
paint() to contained children. Does your override call super.paint(g)?
The usual approach is to put "tiny graphics" in subcomponents, not directly in
the JFrame.

Signature
Lew
Thomas Richter - 17 Oct 2007 15:45 GMT
>> Yes, thanks, I know that this works. The question is rather, why doesn't
>> JFrame.paint() work? Is this a *bug* or am I missing something?
[quoted text clipped - 6 lines]
> forward paint() to contained children. Does your override call
> super.paint(g)?
Yup, it does. Of course *before* drawing my graphics into the frame.
> The usual approach is to put "tiny graphics" in subcomponents, not
> directly in the JFrame.
Clearly. It just was the simplest way for this specific problem at hand...
So long,
Thomas
Knute Johnson - 20 Oct 2007 03:04 GMT
>>> Yes, thanks, I know that this works. The question is rather, why doesn't
>>> JFrame.paint() work? Is this a *bug* or am I missing something?
[quoted text clipped - 16 lines]
> So long,
> Thomas
A JFrame is not a subclass of JComponent. It doesn't have a
paintComponent() method to override. You and the others figured out the
solution.
knute...
Roedy Green - 20 Oct 2007 08:24 GMT
On Wed, 17 Oct 2007 11:24:03 +0200, Thomas Richter
<thor@math.tu-berlin.de> wrote, quoted or indirectly quoted someone
who said :
>according to the Java API docs, a component should call paint() whenever
>it needs refresh, and a swing component should call paintComponent() for
not quite. You need to compose a paintComponent method to override
the usual one. However you don't call it directly. Normally it gets
called as a side effect of a validate, setVisible etc etc. However,
if it needs a manual prod because data on which the rendering is based
have changed, you can call repaint to enqueue an event to get your
paintComponent called.
See http://mindprod.com/jgloss/repaint.html
http://mindprod.com/jgloss/paintcomponent.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com