Repaint a panel inside frame

Hello! I need help with updating graphics in the inner panel of a frame. No matter what I do, I cannot display graphics.  The code looks something like this:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.awt.Container;

public class TestFrame extends JFrame{
      
             //MyPanel is a class that extends JPanel      
              private MyPanel panel;

      public TestFrame(Object o, boolean flag){
      
            Container container = getContentPane();
            container.setLayout( new BorderLayout(0,0) );      
            
            if (!flag)
                  //empty panel
                  panel = new MyPanel();            
            else
                  //panel with graphics
                  panel = new MyPanel(o);
            
            container.add(panel, BorderLayout.CENTER);      
            setVisible(true);
      }
      
      public void showPanel(Object o){
      
            panel = new MyPanel(o);
                                container.add(panel, BorderLayout.CENTER);
            panel.repaint();      
      }
      
      public static void main(String args[]){
      
            //empty pane expected - OK
            TestFrame frame = new TestFrame(o, false);
            
            Object myObject = new Object();

            //show graphics - doesn't work
            frame.showPanel(myObject);            
      
      }
      
}
LVL 1
njavaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Please post MyPanel source
CEHJCommented:
Also replace

>>panel.repaint();    

by

validate();
repaint();
njavaAuthor Commented:
When I added validate(); repaint(); the graphics appeared very quickly and then switched back to the empty frame.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

hoomanvCommented:
possibly you did not override the paint method of MyPannel properly
CEHJCommented:
You need to post the MyPanel code, as i mentioned
CEHJCommented:
You need potentially to override paintComponent in MyPanel, as opposed to paint
CEHJCommented:
You can try this, e.g. in MyPanel

public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawLine(0, 0, getWidth(), getHeight());
}
njavaAuthor Commented:
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;

public class MyPanel extends JPanel{
      
      public MyPanel(){
            
            setPreferredSize(new Dimension(600,600));
            setLayout(new FlowLayout());
            setBackground(Color.WHITE);      
      }
      
      public MyPanel(Object o){
      
            setPreferredSize(new Dimension(600,600));
            setLayout(new BorderLayout());
            add(new GraphicsPanel(), BorderLayout.CENTER);
      
      }

}
njavaAuthor Commented:
import java.awt.Graphics;
import javax.swing.JPanel;

public class GraphicsPanel extends JPanel{
      
      GraphicsPanel(){}

      public void paintComponent(Graphics g) {
           super.paintComponent(g);
           g.drawLine(0, 0, getWidth(), getHeight());      
                              
      }            
}
hoomanvCommented:
add this method

     public void paint(Graphics g) {
          g.drawLine(0, 0, getWidth(), getHeight());    
     }  
njavaAuthor Commented:
hoomanv: I added this method to GraphicsPanel, but it makes no difference. I really have to split MyPanel and GraphicsPanel classes.
hoomanvCommented:
panel.repaint(); just repaints the MyPanel not GraphicsPanel
you need to call paint on the component that needs to be repainted
hoomanvCommented:
add this to mypanel

     public void paint(Graphics g) {
            graphicspanel.repaint(); // instantiate a local variable called graphicspanel in mypanel
     }  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Your code works fine for me with the alterations i supplied. You need to

// show graphics - does work now
            frame.pack();

or

// show graphics - does work now
            frame.setSize(200, 200);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.