Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call paint method on a JPanel

 
Michael Johannes Metternich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
Google App Engine Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Johannes Metternich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic