Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing - paintComponent method not being called

i simply implemented class that inherits JPanel like below

public class Orpanel extends JPanel {

....
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(tpaint);
        g2d.fill(rect); 
    }
....
}

class Orpanel is loading image and adjusting it's own size.

here's question.

Calling JFrame's setContentpane(Orpanel's instance) makes it works fine but when i attach Orpanel to JFrame calling add() method instead of setContentpane (i know setcontentpane is not meaning attach.. anyway), it dosen't work.

finally figured out when i used add() method, Component that was added to JFrame doesn't call paintComponent() method. even though i call repaint() method manually, still paintComponent() method is not called.

did i miss something? any help will be appreciated!

thx in advance. Jaeyong shin.


i added extra code.

public Test(OwPanel op) 
{
    super();
    Dimension monitor = Toolkit.getDefaultToolkit().getScreenSize();
    op.setBackground(Color.white);
    this.setBackground(Color.white);        
    this.setBounds(monitor.width / 2 - 200 , monitor.height / 2 - 200, 400, 400);       
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("test");      
    this.setLayout(null);
    this.getContentPane().add(op);
    //this.setContentPane(op);
    this.setVisible(true);
    this.validate();
}

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            OwPanel op = new OwPanel("d:\\java_workplace\\img\\img1.jpg", 100, 100);
            OwLabel ol = new OwLabel("d:\\java_workplace\\img\\img2.jpg", 300, 50);
            Test tst =  new Test(op);
            tst.add(ol);
        }
    });

still doesn't work if setContentpane() method replaced to getContentpane().add(). don't be confused. Owpanel and Orpanel is same :)

like image 1000
JayMuzie Avatar asked Sep 13 '11 07:09

JayMuzie


People also ask

How often is paintComponent called?

paintComponent(g) is an error. Instead this method is called automatically when the panel is created. The paintComponent() method can also be called explicitly by the repaint() method defined in Component class.

Who calls the paintComponent method of a component?

The call from JComponent. paint is probably the one you're looking for. Note that paintComponent is not called from any constructor. The paintComponent is called "on-demand" i.e. when the system decides that the component needs to be redrawn.

Does repaint call paintComponent?

repaint() calls the paintComponent() method. Every time you wish to change the appearance of your JPanel, you must call repaint().

What object is passed to paintComponent?

paintComponent ONLY receives a Graphics object. See this link for a short tutorial. If you need to access other objects in this method, make them variables of GraphicalBoard and pass them om during construction.


1 Answers

In your sample code, I see you have chosen NOT to use a LayoutManager, that's a very bad idea, but anyway, sicne you go this way, I see one reason for your Orpanel.paintComponent() not being called: it is probably not visible inside the frame!

If you have no LayoutManager, then you must explicitly set the size and location (through setBounds()) of all components you add to the frame.

It is likely you didn't do it, hence the size of Orpanel instance is probably 0 hence it never gets painted.

like image 148
jfpoilpret Avatar answered Sep 28 '22 04:09

jfpoilpret