wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes call paint method on a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "call paint method on a JPanel" Watch "call paint method on a JPanel" New topic

call paint method on a JPanel

Michael Johannes Metternich

Joined: Jun 21, 2005
Posts: 2
I have the following problem:
A JPanel with overwritten paint method works fine when it is placed directly on the content pane. However when it is placed on another JPanel which is located at the content pane, the paint is called but nothing is visible.
Does anyone have an idea?

to clearify the code:

public class ProfileApplet extends JApplet{
private TestPanel testPanel = new TestPanel();

public void init(){
/*this works fine*/

/*this doesn't*/
JPanel panel = new JPanel();

public class TestPanel extends JPanel{

public void paint(Graphics g){
g.drawLine(10, 10, 20, 20);

Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 89

I am not sure of this but when you do a

here the panel has a paint method since this is the parent and also has a paint mehtod this is the paint method that is being called by the or used to draw the component not the paint method of the testPanel.

Suman Mummaneni
Michael Johannes Metternich

Joined: Jun 21, 2005
Posts: 2
that was my first thought as well, but paint(g) of TestPanel is called, which is shown when putting a print function in the paint method. So it must be something else.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The sub-JPanel doesn't know how large you want it to be. The "Upper" JPanel has a FlowLayout, and that FlowLayout decides the sub-panel should be zero size -- hence it's invisible.

Use the setPreferredSize() method on the sub-JPanel to tell it how big it should be -- or change the upper JPanel's layout manager to something like a BorderLayout that won't make this bogus assumption.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link: http://aspose.com/file-tools
subject: call paint method on a JPanel
It's not a secret anymore!