Trying to paint into a parent class
i want my program to initialize another class like
JFrame frame = new JFrame()
GameTable table = new GameTable();
frame.add(table)
and then in a separate classfile thing
public class GameTable extends JPanel{
public GameTable(){
}
public void paint(Graphics g){
...
draw(something)
...
}
}
and then have the results of the Paint method in GameTable appear on the panel in the frame within the main program. so far i cant seem to get it to work out. Any thoughts? if it helps/hurts i'm trying to initialize the 'GameTable' from within an action listener because the frame has something else in it at first then when you push the button the previous stuff goes away and is eplaced with (or would be if it worked) the drawings of the GameTable's paint method.
[852 byte] By [
Ruljinna] at [2007-10-3 11:27:55]

> public class GameTable extends JPanel{
>public GameTable(){
> }
>public void paint(Graphics g){
>...
>draw(something)
>...
>
> }
>
I think the GameTable could just extend JComponent and override the paintComponent(Graphics g) method. This can be plugged into your JFrame. So in your actionlistener you can do something like
public void actionPerformed(...) {
.... // clear out the old stuff
frame.getContentPane().add(gameTable);
}
In your GameTable class, add a main() method. Something like this:
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setSize(500, 500);
f.setLocation(50, 50);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new GameTable());
f.setVisible(true);
}
Is that what you are talking about?
at first then when you push the button the previous stuff goes away and is replaced with (or would be if it worked) the drawings of the GameTable's paint method
When you change components like this you have to tell the parent Containers layout manager that its layout has become invalid which will cause it to run through its children and lay them out again. We have two convenient ways to do this in Swing:
The JComponent method revalidate
and the Container method validate.
Try calling one of these on the parent container after adding your graphic component (GameTable) to it.