File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes using swing(GUI) in oop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "using swing(GUI) in oop" Watch "using swing(GUI) in oop" New topic
Author

using swing(GUI) in oop

F Merchant
Greenhorn

Joined: Oct 02, 2012
Posts: 11
I'm working on creating a loan calculator in Java and using Swing.
I have previously used Java swing to create GUI. And my general approach has been to use one JFrame per screen and perform all functionalities in that class itself.
But this time I want to use oop and separate the GUI classes from the main class.(unlike previously where everything is jumbled into one class)
But I'm confused on how to proceed. Like what will be my main class.....the main menu UI or another class?
The project wil have three screens:ask user for input,display a table with the monthly installments and remaining balance, and a graph of months vs payment.

If someone could please give me an overview on how to proceed, it would be great!
Thanks!!
F Merchant
Greenhorn

Joined: Oct 02, 2012
Posts: 11
This is what I've come up with so far.
Main class
main()
{
initialise main menu screen
}

Calculate
calcMonthlyPayment()
calcNoOfPayments()
calcTotalPayment()

CalcTable
calcPayment()

CalcGraph
calcMPayment()

MainMenuScreen
Table button clicked-->TableScreen
Graph button clicked-->GraphScreen

TableScreen
Take inputs from MainMenuScreen
Send to CalcTable.calcPayment()
Get values
Create JTable

GraphScreen
Take inputs from MainMenuScreen
Send to CalcGraph.calcMPayment()
Get values
Draw Graph


I've tried to separate the GUI from the calculations and conform to OOP principles.
Please let me know if I'm on the right track.
Thanks!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1966
    
    7

In fact you can split the classes or should I say redesign/refactor your classes by a) functions or use cases, b) component or layout (tables, panels, menubar, action etc)

From the look of your app, using a component-design is probably better. One down-side I can think of is your main window/frame will still need those necessary components such that if needed different panels can interact with. Eg if a graph gets updated when the table data is selected ... then you need the table's selected data or the table itself in your graph class.

This approach may make the number of classes in your app grow depending on how small a component each class is.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
F Merchant
Greenhorn

Joined: Oct 02, 2012
Posts: 11
Hmmm...but for right now I'll just try and build a basic system and keep the communication between screens to a minimum.
Main Menu Screen asks user for Principal,rate and term.
It has two buttons:chart and graph.
On clicking chart button, a table is generated(Payment No, Monthly interest, Monthly payment, Remaining balance)
On clicking graph button, a graph of months vs amount is displayed.

Let me work on this, then I'll try and expand the scope of the project.
One more question....I have to validate the values entered by the user, so will Validation be a separate class?

Thanks!
Steve Myers
Ranch Hand

Joined: Dec 08, 2012
Posts: 47
F Merchant wrote:Hmmm...but for right now I'll just try and build a basic system and keep the communication between screens to a minimum.

Let me work on this, then I'll try and expand the scope of the project.
One more question....I have to validate the values entered by the user, so will Validation be a separate class?

Thanks!


You can validate the input in the listeners for the components, as nested classes or anonymous classes in the class where you're drawing the parent container
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using swing(GUI) in oop
 
Similar Threads
Use custom components?
Swing GUI class architecture
GUI classes, implementing ActionListener.
What's it all about man (this Java thang)
options in jar files....