You are not logged in.

#1 2009-03-13 16:10:10

xaff
Member
Registered: 2009-02-26
Posts: 64

Hit a wall with java

I'm writing a java application in netbeans and I've stumbled upon a problem I have no idea how to solve.

In my main code I'm starting a second, maximized frame and adding Canvas extending class that interacts with a user and draws stuff accordingly. The problem is at some point that canvas is supposed to tell the main application that it's finished, so the second frame can be closed and the data that was gathered during canvas action analyzed and I have no idea how to do that, since canvas can't see anything outside itself.

Any hints?

Offline

#2 2009-03-13 18:32:18

rebugger
Member
From: Germany
Registered: 2007-10-28
Posts: 229

Re: Hit a wall with java

You could create a isFinished()-method in your canvas-class. And in the class where you run the canvas-class you could have a
while(c.isFinished() == false) { Thread.sleep(1000); }
So it will ask each second if the canvas is finished.

I'm not really sure if its the best way for you. Maybe you should think about redesigning your classes. There is always a good solution.

Offline

#3 2009-03-13 19:19:59

xaff
Member
Registered: 2009-02-26
Posts: 64

Re: Hit a wall with java

I've tried that ealier, but unfortunately nothing in second frame is happening as if sleep() clogs everything.

Offline

#4 2009-03-13 21:06:05

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Hit a wall with java

Something with SwingWorker, perhaps? I am not a Java programmer.

Offline

#5 2009-03-14 07:48:42

xaff
Member
Registered: 2009-02-26
Posts: 64

Re: Hit a wall with java

Oh SwingWorker seems like something that might help. I'll try to get familiar with it today and post results. Thanks smile

Offline

#6 2009-03-14 08:28:33

drewbug01
Member
From: chicago
Registered: 2007-04-06
Posts: 85
Website

Re: Hit a wall with java

I'm not sure I completely understand the problem ....
Have you tried writing a short, static, public method in the main file that the canvas-class could call when finished? So, canvas could call the "imFinished()" method of the main class, and that "imFinished()" method could do the closing? Being static and public, there is no way(that I can think of) that the canvas class _couldnt_ see it.

-- Drew

Offline

#7 2009-03-14 10:09:51

xaff
Member
Registered: 2009-02-26
Posts: 64

Re: Hit a wall with java

I think you're completely right Drew, I was thinking about something like that too, but it didn't occur to me that I could make that method static to solve the visibility problem.
Thank you.


Edit:
However there's no way to call nonstatic methods or variables from the instance of my main class from the public static method.

Last edited by xaff (2009-03-14 10:22:20)

Offline

#8 2009-03-14 11:29:22

kakTuZ
Member
From: Hannover, Germany
Registered: 2007-10-20
Posts: 86

Re: Hit a wall with java

You could make the canvas part Observable (if it is a and simply add you main class (or an anonymous class inside your main routine) as a Observer.
http://en.wikipedia.org/wiki/Observer_pattern

Offline

#9 2009-03-15 22:50:59

drewbug01
Member
From: chicago
Registered: 2007-04-06
Posts: 85
Website

Re: Hit a wall with java

True. Getters/Setters, or maybe throw the canvas inside the main routine ?

--edit--
Or try playing around with making the variables/methods protected instead of private. that would open them up to members of the same package + subclasses.

Last edited by drewbug01 (2009-03-15 22:52:14)

Offline

Board footer

Powered by FluxBB