This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 87
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: 24183

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]
It is sorta covered in the JavaRanch Style Guide.
subject: call paint method on a JPanel
Similar Threads
Placing a component at center both vertically and horizontally
how to refer back to an applet from a panel that has been added to it
Generat JAR file with resouces such as picture and music!
nothing shows. what did i forget?
Trying to resolve some odd resizing behavior of objects in GridBagLayout