RamVijay
RamVijay

Reputation: 21

Paint request not executed on working with two JPanels on a JFrame?

I am trying to work with two JPanel on a single Jframe . I am adding them to frame using the container . so when i add first Jpanel to frame, the Paint Component method is called but when i add second JPanel to frame , the paint Component method is not called by the JVM.

here is the code ,

public class Frame {

     public Frame() {
        // TODO Auto-generated constructor stub
      }

    public static void main(String[] args)  {
         JFrame gui = new JFrame();
         gui.setTitle("Chain Reaction ;-) ");
         gui.setSize(650,650);
         gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         gui.setLocationRelativeTo(null);
         gui.setVisible(true);  
         Container Pane = gui.getContentPane();
         Panel1 myPane1 = new Panel1(Pane); 
    }
 }

here i am adding Panel1(JPanel) object to JFrame in constructor. I am passing container reference to constructor .

public class Panel1 extends JPanel {

    Container myPane;

    public Panel1(Container myPane) {
        this.myPane = myPane;
        myPane.add(this);
        addMouseListener(new event(myPane));
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawRect(200, 200, 400, 400);
    }

    public class event extends MouseAdapter {

        Container myPane;

        public event(Container myPane) {
            this.myPane = myPane;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            Panel2 myPanel2 = new Panel2(myPane);
        }
    }
}

(Panel1 class has event class which listens for mouse click ,when mouse is clicked it Creates object of Panel2)

public class Panel2 extends JPanel {

    Container myPane;

    public Panel2(Container myPane) {
        this.myPane = myPane;
        myPane.add(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawRect(400, 400, 200, 200);
    }

}

Panel2 receives reference Container through constructor , and then draws a rectange, this paint request is not being called by JVM .

Help me . what i should do now ?

Upvotes: 0

Views: 107

Answers (1)

camickr
camickr

Reputation: 324118

By default a JFrame uses a BorderLayout. When you add a component to the frame and don't specify a constraint the component is added to the CENTER. Only one component can be added to the center, so only the last component is displayed.

However, when you add a component to a visible GUI you also need to invoke revalidate() and repaint() on the panel that you add the component to so the layout manager can be invoked.

In your case you don't invoke revalidate() so the second panel has a size o (0, 0) so there is nothing to be painted.

if you do invoke revalidate() then the second panel will have a size but now the first panel won't be painted.

You need to redesign you app or use a different layout to get your desired effect.

Also, in your main() method you should be adding the panel to the frame BEFORE you make the frame visible.

Upvotes: 3

Related Questions