Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Paint/Repaint not painting

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
0
0
CA
Hi,

I have an application that consists of a JFrame with a ScrollPanel and a GridLayout container nested within it (I made a simplified example below).

The problem that I am having is when I add new objects to the "contentPane" container, they won't appear on the form unless I resize the main JFrame. I've tried calling repaint() on each container and even overriding the paint() method! Any ideas?

Thanks in advance!

Code:
import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;

public class Example extends JFrame{
	private Container contentPane = new Container();
	private ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

	private class Pic extends Canvas{
		public Pic(){
		}

		public void paint(Graphics g){
			g.setColor(Color.RED);
			g.fillRect(0, 0, 100, 100);
		}
	}

	public Example(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		sp.add(contentPane = new Container());
		sp.setFocusable(false);
		add(sp, BorderLayout.CENTER);

		contentPane.setLayout(new GridLayout(0, 3));
		contentPane.setFocusable(false);

		setSize(400, 400);
		setLocationRelativeTo(null);
		setVisible(true);

//add some objects...
		addStuffToContentPanel();

//attempt to get the new objects to show up...
		contentPane.repaint();
		this.repaint();
		sp.repaint();
	}

	public static void main(String[] args){
		new Example();
	}

	public void addStuffToContentPanel(){
		contentPane.add(new Button("A"));
		contentPane.add(new Button("B"));
		contentPane.add(new Button("C"));
		contentPane.add(new Pic());
	}
}
 
Sorry, figured it out about 10 seconds after posting! I was missing a call to pack();!!! I've spent hours on this [3eyes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top