LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-22-2009, 12:27 PM   #1
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
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,
Code:
public class MainClass extends Applet implements MouseListener, MouseMotionListener
and a second DrawMenu class,
Code:
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?
 
Old 10-22-2009, 07:30 PM   #2
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
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.


WARNING - TECHNICAL ALERT
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 :-)
 
Old 10-24-2009, 04:14 PM   #3
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Original Poster
Rep: Reputation: 30
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...
Code:
    public void run(){
        while (true){
            System.out.print("");
            if (modified){
                repaint();
                modified=false;
            }
        }
    }
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java GUI and repaint() problem Cyhaxor Programming 4 03-05-2009 07:51 PM
java super() main class trscookie Programming 2 09-26-2007 06:34 PM
How to change GNOME main menu icon(Applications menu) deepclutch Linux - Software 7 06-25-2007 02:31 PM
Java - repaint on tabbedpane hunterfighter Programming 7 09-25-2006 07:46 AM
KDE W/ java doesn't display popup applet correctly gdsylvester Linux - Software 2 01-06-2004 12:02 PM


All times are GMT -5. The time now is 03:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration