• Post Reply Bookmark Topic Watch Topic
  • New Topic

call to repaint() in paintComponent()

 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of my Java experience is with Servlets so, in order to get more familiar with Swing, I was going through the chapter on Graphics Programming from Horstmann and Cornell's Core Java Vol. 1. What I found was that none of the sample programs displayed properly on my machine (I'm running Windows 98). The frame displayed, but nothing that was supposed to be painted on the panel showed up until I resized the window. What actually worked was adding a call to the repaint() method at the end of the paintComponent() method. Why would I need this call and why does it work? Doesn't repaint() call paintComponent(), giving me an infinite loop? I've had quite a few display problems with Swing components. Has anyone else out there noticed display problems with Swing in a Windows environment?
Even the simplest program didn't display properly until I included this method call. Example:
import javax.swing.*;
import java.awt.*;
public class NotHelloWorld {
public static void main(String[] args) {
NotHelloWorldFrame frame = new NotHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class NotHelloWorldFrame extends JFrame {
public NotHelloWorldFrame() {
setTitle("NotHelloWorld");
setSize(WIDTH, HEIGHT);
NotHelloWorldPanel panel = new NotHelloWorldPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
}
class NotHelloWorldPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y);
// Only when the following call to repaint() is included does the panel display properly.
//repaint();
}
public static final int MESSAGE_X = 50;
public static final int MESSAGE_Y = 70;
}
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've gotten rid of the infinite loop by moving the call to repaint() into the main method of the "main" class:
public class NotHelloWorld {
public static void main(String[] args) {
NotHelloWorldFrame frame = new NotHelloWorldFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
frame.getContentPane().repaint();
}
}
However, it still doesn't seem like this call to repaint() should be necessary. Furthermore, I'm still having display problems when I resize and move the window around. Any ideas? (Thanks)
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out sun's article on paint/repaint. Note that multiple calls on repaint may be combined into one call on paint.
In my main I normally call setBounds or setSize and then setVisible (true).
I think that in your case the show call does not call the layout managers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!