aspose file tools*
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "call paint method on a JPanel" Watch "call paint method on a JPanel" New topic
Author

call paint method on a JPanel

Michael Johannes Metternich
Greenhorn

Joined: Jun 21, 2005
Posts: 2
Hello,
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*/
//getContentPane().add(testPanel);

/*this doesn't*/
JPanel panel = new JPanel();
panel.add(testPanel);
getContentPane().add(panel);
}
}

public class TestPanel extends JPanel{

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

thanks!
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Hi,
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
Bangalore
India
Michael Johannes Metternich
Greenhorn

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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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