TheInsider
Programmer
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!
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());
}
}