• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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);            
      
      }
      
}
0
njava
Asked:
njava
  • 6
  • 4
  • 4
1 Solution
 
CEHJCommented:
Please post MyPanel source
0
 
CEHJCommented:
Also replace

>>panel.repaint();    

by

validate();
repaint();
0
 
njavaAuthor Commented:
When I added validate(); repaint(); the graphics appeared very quickly and then switched back to the empty frame.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawLine(0, 0, getWidth(), getHeight());
}
0
 
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);
      
      }

}
0
 
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());      
                              
      }            
}
0
 
hoomanvCommented:
add this method

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

     public void paint(Graphics g) {
            graphicspanel.repaint(); // instantiate a local variable called graphicspanel in mypanel
     }  
0
 
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);
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now