File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Adding a GUI to an existing program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding a GUI to an existing program" Watch "Adding a GUI to an existing program" New topic

Adding a GUI to an existing program

Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33

I have a simple inventory program that I will be adding a GUI to for an online programming class. I don't want to start off on the wrong foot, so before I do that, I will ask. Should I implement a new class for my GUI and invoke my other methods from there, or should I modify my existing class to incorporate the GUI elements? My best guess is that I should implement a new class, but being a newbie, don't want to unnecessarily complicate my life. Thanks in advance for the help.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Debra Simeroth wrote:My best guess is that I should implement a new class, but being a newbie, don't want to unnecessarily complicate my life.

Your instinct is a good one. Implementing a new class is less complicated than having a giant monstrosity of a file!

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Anybody who worries about their classes getting complicated is on the right path . . . but we usually discuss GUIs on a different forum, so I shall move you there.
Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
I was hoping this would be easier, but it doesn't appear to be. I have been at this since 10 am... stopped to get food and bring it back to my isolated room, and went at it again. I have read a myriad of materials, tutorials and even tried my hand at some code, but keep ending up with the same problems. How do I connect the code I have with this GUI? Specifically the code in my main method from //print welcome statement to //end method main which needs to print in a GUI. No bells and whistles yet, just a display. I figured that my code in my main method needed to be more modularized, so I tried that for a few hours.. getting nowhere. I also tried just referencing some other methods and also got nowhere. It is now 8:30 at night and I am humbly asking for help (again). I would insert the banghead at this point, but I am too tired to do this.. I will just cry Below is what I have accomplished... please don't laugh I am totally new at this.

There is one more class of code, however I don't think it is relevant to this discussion so I won't bore you... Thanks in advance.... and thanks from my husband and children who are missing me and want me to come out of this room.... Only two more weeks of this class, then I will study this at a more reasonable pace! I like it, but really need more time to absorb the wealth of information necessary to code properly!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this is just a single .java file, having changed a couple of things to be able to compile/run it.

copy/paste/compile/run it as-is and you should get roughly the same output as your existing program (total value $30.172.75)

I've added a very simple gui, just to start you off, so after running it as-is, comment out your InventoryTest class, and uncomment my Testing class
recompile/run then click the calculate button.

if you want to add components to the gui, only use swing components, not the awt ones you were mixing in with the code you posted.

Debra Simeroth
Ranch Hand

Joined: Jun 11, 2009
Posts: 33
I am on my knees thanking the Dear Lord for this forum! I will study what you have shown me here.... I may have questions later... In fact I am sure I will! Mr. Dunn, you are most excellent!
K. Tsang

Joined: Sep 13, 2007
Posts: 3130

Hello Debra. Your current approach can be simplify so much in my opinion. Given you have your old non-GUI version working as it. Now the main program/class is the non-GUI version. You building the GUI class. So all you need to do is 1) duplicate the non-GUI main class as its separate class then 2) modify your main program to call either non-gui or gui.

And if you really want modularity and OO - look into class design (eg sharing methods and stuff), separate the common stuff to its own class and modify the non-gui and gui class accordingly.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
I agree. Here's the link:
subject: Adding a GUI to an existing program
It's not a secret anymore!