• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

call paint method on a JPanel

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic