-   Programming (
-   -   JAVA: Popup menu in diffrent class then main Applet, how to repaint()? (

Valkyrie_of_valhalla 10-22-2009 12:27 PM

JAVA: Popup menu in diffrent class then main Applet, how to repaint()?
In an attempt to better organise my code, I decided to have a main class,

public class MainClass extends Applet implements MouseListener, MouseMotionListener
and a second DrawMenu class,

public class DrawMenu extends PopupMenu implements MouseListener, MouseMotionListener, ActionListener
And it works perfect, except that after issuing a command in the menu, I need a repaint() in MainClass... ofc I can't create an instance of MainClass in DrawMenu, can't call MainClass.repaint() as it is not static... so I'm kind of stuck. Is there a proper way to do this, or do I have to put them in the same class?

gzunk 10-22-2009 07:30 PM

I assume that your first class creates the instance of the second class.

In which case, simply pass a reference of MainMenu to DrawMenu - either in the constructor, or as a method call after the constructor, and store the reference in an instance variable.

Then, when you need to call MainMenu "repaint" in DrawMenu, you can call it - by using the reference you have saved.

But if I was to be honest, I would have to say that this will only work if the routine in DrawMenu was being invoked in the GUI thread, but it might not be - so ignore this caveat :-)

Valkyrie_of_valhalla 10-24-2009 04:14 PM

Thanks for your reply :)

Found another solution, sort of... Made MainClass runnable, and in it, created a "boolean modified", which will be set true if i click something in the menu in the other class.
Then I have a...

    public void run(){
        while (true){
            if (modified){

What's with the System.out.print("");? Well, apparently, if I don't have something outside the if that executes on each run, it doesn't work...

So... new question: why is this, and how is it usually done?

All times are GMT -5. The time now is 05:47 AM.