• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

First Desktop App Question  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - long time programmer, but brand new to Java, and newly back to programming after almost 8 years!

I'm trying to build a simple desktop (Windows) GUI app, and am struggling to understand the "entry point" and "Event loop" concepts as implemented in Java, that I am used to in past languages. I know how to create a class with the "main" method in it and use that as the entry point, but I'm a little lost on the "Event loop" part. So - using some pseudo-code, here is what I've done in the past and am trying to do in Java main program.


------------------------------------------------------------------------------------------------------------
- main program starts

## any initialization stuff I need  happens

## I instantiate my "main GUI form" (I built a Swing form - works great!) and show it
MyMainform frmMain = new MyMainform();
frmMain.setVisible(true);

## at this point - the form appears and should be "in control" -  sitting and waiting for user to click buttons, make menu choices, etc.
## So I would expect to go into an "Event Loop" and wait for the form to get closed/exited by the user, which should somehow cause the loop to end

Event Loop code goes here - but what is it?

## AFTER user exits form, now the rest of the code runs
Clean up code would go here

## program exits
------------------------------------------------------------------------------------------------------------

So - how do I do this? Is there a different approach/paradigm in java I need to learn?

As it is, without a loop,  the main program just runs all the way through and exits and the MyMainForm still stays up. I don't even understand how that is possible since the variable holding it (frmMain) would have gone out of scope right? HELP?

Thanks so much!
KenM
 
Master Rancher
Posts: 3002
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ken,

welcome to the ranch and enjoy the stay!

Your story reminds me of the end of the '80s. when some instructor learned me about SAS. The code was processing all records, but there was no loop in sight! How the heck was that possible?? Well, there was a loop somewhere out of sight, and it took me a while to get used to this style and especially, to get trust in what I saw (or not saw).

For java there is probably a very interesting and technical explanation, but I always trust the loop behind the scenes that is watching for possible events and will inform me of such an event when that happens. Having learned SAS, the culture shock was minimal for me.
 
Bartender
Posts: 19989
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, things have moved on. Java tends to favor event listeners over event loops per se. Also, graphics rendering is done by a completely different thread than the application proper. You don't "draw" stuff, you write rendering code and it gets scheduled when the main app (or a windowing event) schedules a redraw.

This isn't exclusively a Java thing - OLE was leading in that direction as well. Part of it is that it's easier to modularize complex graphical apps. Part is because much of the heavy lifting for graphics is now done in hardware (GPUs) and this approach insulates the functions from dependencies on CPU and graphics co-processors.

The Model/View/Controller approach is the norm for almost all GUI platforms these days, even web-based ones and regardless of language. Here, again, someone else is in control and you just get called as needed.
 
Rancher
Posts: 3337
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some quotes from the API doc for the Thread class:


When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.


The Java Virtual Machine exits when the only threads running are all daemon threads.


The "Event Loop" is buried in the JVM.  You write code as  listeners to handle all the events you are interested in.
 
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you read the Swing Tutorial for Swing basics to get you started.

Each section has working code you can download and play with.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and thanks for the responses!

OK - so it seems like something (Swing?) is registering my form somewhere else (JVM?) and that's hanging on to my form in a separate thread, even once my main program exits. OK - I can get that. Reading some of the concurrency stuff in the Swing tutorial helped a bit with that, though i don't have my head fully around it. In particular, I'm a little unsure if most of this thread stuff is happening automagically behind the scenes for me vs. how much I need to be aware of vs. how much I need to actively control. Gonna have to study up more on that.

As far as my original issue though - how do I setup my application to have a main management class that runs everything else and stays "alive" so that it can clean up at the end. Example:

(Think in terms of a client-server app - a desktop GUI app with a SQL database for instance)

My main method does nothing but instantiates a management class and turns control over to it. Something like this:

AppMgr myMgr = new AppMgr();
myMgr.runTheShow()

now, the "runtheShow()" method is going to roughly do something like the following:

- instantiate a "logging" object - oLog we'll call it - which includes opening and holding a connection to the database -
- present a login screen for user
- validate the user's login
- retrieve user info/credentials from a database and STORE THAT in a oUser object
- instantiate the main form -PASSING somehow the oLog and oUser objects to the form since they'll be needed there. I usually just pass them to the constructor and let the form (class) store the reference in a property/field/whatever you call them in java.
- show the form - user does all their work - form references the oUser object as needed for rights and such, and the oLog object as needed to log whatever it needs to.
- when user exits form, I would HOPE that the "runTheShow" method could pick up where it left off and do the NECESSARY CLEANUP WORK - making final log entries, updating user data if needed, closing database connections, etc. before exiting and the entire app ending.
- in the model you guys describe ... how best to deal with the need for the form (and sub-forms of that I might add) to reference myMgr created objects like the oUser and OLog I describe?
- even more importantly - where does the finalization and cleanup code go if my main program has already ended?

All my online searches show lots of examples of building the Swing forms, handling events, etc. - things I understand pretty well. I see little or no examples of how to wrap them in a management class to "run the app" where the form presentation is just one part of what needs to go on?

Thanks so much for indulging me ... old brain cells trying to learn new tricks I guess ....




 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wrap them in a management class to "run the app"  


I don't think of it that way.  My thoughts on the order of execution are:
setup the GUI
and exit the method
wait for user
the user interacts with the GUI
the JVM reacts to the user's inputs and calls one of your listeners
the listener does something and if needed starts a thread for a longer running task
the listener exits
waits for user action
etc
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

wrap them in a management class to "run the app"  


I don't think of it that way.  My thoughts on the order of execution are:
setup the GUI
and exit the method
wait for user
the user interacts with the GUI
the JVM reacts to the user's inputs and calls one of your listeners
the listener does something and if needed starts a thread for a longer running task
the listener exits
waits for user action
etc



Ok - but what if you need code to run before the forms appears to get info that the form will need .... and ....
you need code to run AFTER the form is exited that needs info that might have been changed by the form?
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

code to run before the forms appears


Do that before building the GUI and calling setVisible

code to run AFTER the form is exited  


The time to exit is normally decided by the user.  So the code reacts to some user action that says to exit the program.  The listener will be called to do whatever, turn off the GUI and exit.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:
The time to exit is normally decided by the user.  So the code reacts to some user action that says to exit the program.  The listener will be called to do whatever, turn off the GUI and exit.



but its the original calling method that needs to do something after the form closes - not the form itself. So the issue is keeping that calling method alive in a wait state until the user exits the form.
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

issue is keeping that calling method alive


I don't think it can be done that way. What would be the purpose? What would that bit of code be executed?
When the user indicates the program is to end, the listener is called and it takes care of ending the program.
If any values need to be preserved from the code that started the execution, those values would have to be at the class level because any local variables would be lost when the starting method (eg. main) exits.


after the form closes


I'm not sure what is meant by "form"?  I don't see a form as executable code.  To me the GUI is what is displayed on the screen and is build by bits of code that write lines and fill in spaces.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

issue is keeping that calling method alive


I don't think it can be done that way. What would be the purpose? What would that bit of code be executed?
When the user indicates the program is to end, the listener is called and it takes care of ending the program.
If any values need to be preserved from the code that started the execution, those values would have to be at the class level because any local variables would be lost when the starting method (eg. main) exits.



I understand what you're saying, and maybe it's just a paradigm shift for me that I need to get over ... but in my mind the calling class/method "owns" the form along with owning other objects it has created. It seems counter intuitive to me that the form should somehow be responsible for cleaning up things that had nothing to do with the form in the first place ... e.g. other objects the calling class had created.
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the form should somehow be responsible for ...


We cross-posted.
I'll repeat:
I'm not sure what is meant by "form"?  I don't see a form as executable code.  To me the GUI is what is displayed on the screen and is build by bits of code that write lines and fill in spaces.  Certain parts can receive the focus and send user input to the program via its listeners.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

the form should somehow be responsible for ...


We cross-posted.
I'll repeat:
I'm not sure what is meant by "form"?  I don't see a form as executable code.  To me the GUI is what is displayed on the screen and is build by bits of code that write lines and fill in spaces.  Certain parts can receive the focus and send user input to the program via its listeners.



The form is just a class - a class that gets instantiated and becomes visible and interactive. Like any class, it has methods, properties, etc. The listeners (event handlers) are just methods on the form class right? I'm using netbeans and their jframe form builder, so maybe some stuff is hidden from me, but digging in - the code is there.

The point is that the calling program may have set up and instantiated stuff, independent of the form, BEFORE the form was created or opened, that it needs to clean-up AFTER the form has closed - which is the signal from the user that they application should end.
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok the form is the class that builds the GUI.  I haven't separated that out from my other code so I don't have form classes.  I usually have a method that builds the GUI.  I don't use a fancy IDE like NB.
The code can create instances of methods and call methods as needed to execute and preform the task.

The listener that is called when the user says to exit is where I place the code for cleanup.  It can call methods in other classes as needed.

it needs to clean-up AFTER the form has closed  


If special code needs to be called on exit, the listener can call that code.

The listeners (event handlers) are just methods on the form class right?


Not necessarily.  The listeners are usually separate, independent classes.  They can be an inner class to allow access to the outer class's stuff.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would love to see a small section of your code that builds a very simple form if you don't mind posting it?
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There must be dozens of examples of code here in this section of forum.

I don't write much code with GUI. Most of my stuff does simple file conversion. For example: create my wps files from kml files and vica versa.   Analyze played bridge hands generated by my bridge program (written in C++) . Build html pages for our HOA records.
Most of the code I have is copied from students' postings to forums like this one, so it is not my code to display.
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also take a look at the java tutorial:  https://docs.oracle.com/javase/tutorial/uiswing/index.html
 
Sheriff
Posts: 5448
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a reason the OP shouldn't use the newer JavaFX technology?  

https://docs.oracle.com/javase/8/javase-clienttechnologies.htm
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No reason, the more the better. Let the OP decide which  to study.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I appreciate all the replies, and yes I'm looking at lots of different tutorials and ways of doing things. I feel like this thread has gotten a little off topic though. I'm not having any problem understanding how to build, display, manipulate, handle events on, or use a form or call it from my main program. All the tutorials seem to focus on these things

My issue is in understanding how to keep my calling program alive until the form has exited so that I can run cleanup code in that main calling program after the user has decided to exit the application, or if that is not the Java-like way to do it in this day and age, understanding where I would put that clean up code.

Thanks for all the responses and I'm certainly open to any other ideas anyone has on how to handle this!
 
Norm Radder
Rancher
Posts: 3337
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how to keep my calling program alive until the form has exited


Review my earlier posts copyied from the API doc for the Thread class explaining when the program will exit.
Normally that is not a problem.  It takes special code to change all the threads so the JVM will exit.
Calling a class a form has no meaning to the JVM.  Execution goes through  listeners to handle user events.
It's not necessary to have a GUI for a program to run.  The GUI can be closed and the program can continue running.
And it's not necessary for the GUI to exit for the program to end.  A timer can be set to call the System.exit method to end the program after a certain time.  I use that when debugging some student code that has a loop to save having to use the Task Manager.

where I would put that clean up code.


Connect it to the listener code that responds to a user's request to exit the program.
 
Sheriff
Posts: 23874
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ken Matson wrote:My issue is in understanding how to keep my calling program alive until the form has exited so that I can run cleanup code in that main calling program after the user has decided to exit the application, or if that is not the Java-like way to do it in this day and age, understanding where I would put that clean up code.



In Swing you'd use a JFrame to contain your GUI, and when you get a window-closing event from it you do that clean-up code. Tutorial here, although I would expect it's likely you read this tutorial already. In JavaFX there's no doubt something similar.
 
Ken Matson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Is there a reason the OP shouldn't use the newer JavaFX technology?  

https://docs.oracle.com/javase/8/javase-clienttechnologies.htm



Thank you! Yes, being new to Java I didn't realize that swing was somewhat outdated and that javafx was the new technology. I have read up on it now,  and will immediately start learning and using that instead of swing. Thank you.
 
Paul Clapham
Sheriff
Posts: 23874
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaFX it is, then. And could I suggest, do yourself a favour and start by going through the tutorials. Don't try to imprint your preconceptions on what you see there, just go with what you see. What I usually do is to take the example code from a Java tutorial and fiddle around with it, making it do different things, to get a handle on how it works.
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken,

In year 2012, I had got my Java 7 certification. I created a Java quiz to share some exam prep notes as quiz (or flash cards). I used a Swing GUI application to build a quiz app.

How did I go about it? I had a copy of Head First Java, 1st edition (from which I learned Java initially) and I recollected there was an example Quiz Card Builder / Player application and code for that in that book. So, I went on using that as a code base and built the quiz app I wanted to build. It uses Java Swing GUI.

The app is accessible at javaquizplayer.com

I found a free copy of Head First Java ebook online [Editor: the rest of this line has been deleted, see message below.]

The following can be referred to get the technical info and code you might be looking for to design and build your app.

Chapter 12: getting gui
Chapter 13: using swing

There is example source code for Quiz Card Builder in Chapter 14: Saving Objects, serialization and file

I hope this is useful to you, in some way, to build your quiz app.
Staff note (Knute Snortum):

Be sure that the "free copy" of any material you post here is not, in fact, an illegal copy. Please support the authors by purchasing the book.

 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an article with simple example (I had written sometime back), a TODO app using JavaFX which stores its data in a database. There is downloadable code and description how the app was built. Here is a link to that: https://www.javacodegeeks.com/2015/01/javafx-list-example.html

Also, look take at the JavaFX examples (over 100 of them) from Oracle website - this is an ensemble application which shows usage of all JavaFX GUI components or widgets with code and demo binaries. This can be found at the Oracle's web page: http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html

Building GUI apps can be fun and exciting; it has a lot do with user interfaces, user experience, and of course the app functionality. When I build a GUI app, I usually try to prepare a user guide which each function separately, which includes the screen sketches. This helps me to understand what I need to do and then design the technical aspects.

Hope this is helpful.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!