d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Swing Help > Seperating Gui Logic?
Add Reply New Topic New Poll
Member
Posts: 29,363
Joined: Mar 27 2008
Gold: 504.69
Nov 28 2015 07:52am
Sorry, this might be long winded I will try to explain what I am trying to achieve to the best of my abilities.
I am not after any code, just the understanding of how to implement my objective.

In a previous lab we we explored the properties of inheritance.
We created a class named Employee, and two other classes named Salesman and Technician that extended Employee.
A Salesman is a Employee, a Technician is a Employee.

The objective of the next lab is to take those classes and create a Applet GUI with Swing.
Keeping the Applet GUI Logic and the Business Logic separated. Highly Cohesive with low coupling.

Here is a badly drawn UML diagram I worked up in paint that might help you visualize.


A little bit about the layout of the GUI. It will be border layout.
To the North, a text area that shows the list of employee's.
In the Center, fields for inputting data.
To the South, buttons for resetting the data, adding/deleting an employee, sorting the list, and exiting.


Right not I have Company. It contains the main method that does nothing but simply instantiate a new CompanyGUI object.

CompanyGUI creates the frame, layout, adds the panels to the frame. Creates the panel for the buttons. Has the class for the
button listeners as well. Right now I have the button listeners set-up, they just don't do anything.

An outer class that handles the top panel, which is the text area.

An outer class that handles the middle panel, which is the fields for inputting data.

My actual question: How can I go about creating a class (ie: UseCompany) that will handle the business logic. And what I mean by that is I want the top panel to simply create the text area, and have a method that takes a list and updates the text area.
I want the middle to have all the fields for inputting data, and methods that will get the data from each individual field.
And lastly, a class that will be called when you hit the buttons, that modifies a list.
Example: You hit the button to add an employee, the button calls a method in UseCompany called add. It gets the data from the middle panel and adds an element to an ArrayList. UseCompany passes the ArrayList to the top panel to update the display.
I am getting caught up on how to implement this without static methods. Any help would be greatly appreciated.
Member
Posts: 9,834
Joined: Nov 13 2007
Gold: 771.00
Nov 28 2015 09:34am
Quick question, are you using AWT for event handling?

And sure you can use one primary class (even the main) for the bulk of your logic. I tend to do it within the actual GUI class. As far as static methods go, are you creating instances of your classes and reference them that way? So you don't need to actually create static methods you can just use:

Quote

public class something(){

//constructor
public something(){

//class object instantiation
private Employee emp = new Employee();

//call some method
emp.methodName
}
}



So you can instantiate a class object and call a function, that why there is no need to make the method(s) static. Hope this wasn't all information you already knew, let me know if it helps.
Member
Posts: 29,363
Joined: Mar 27 2008
Gold: 504.69
Nov 28 2015 09:42am
Quote (Petrusk @ Nov 28 2015 11:34am)
Quick question, are you using AWT for event handling?

And sure you can use one primary class (even the main) for the bulk of your logic. I tend to do it within the actual GUI class. As far as static methods go, are you creating instances of your classes and reference them that way? So you don't need to actually create static methods you can just use:



So you can instantiate a class object and call a function, that why there is no need to make the method(s) static. Hope this wasn't all information you already knew, let me know if it helps.


Yes, to AWT.
Code
exitButton.addActionListener(new ExitButtonListener());

Code
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0); //exit the application
}
}


Would I instantiate an object of type UseCompany inside CompanyGUI and do something like topPanel.updateDisplay(useCompany.add());

UseCompany add method passes TopPanels display method a List.
It would work but doesn't seem right to me. But it might be.
Member
Posts: 9,834
Joined: Nov 13 2007
Gold: 771.00
Nov 28 2015 09:54am
Quote (ROM @ Nov 28 2015 03:42pm)
Yes, to AWT.
Code
exitButton.addActionListener(new ExitButtonListener());

Code
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0); //exit the application
}
}


Would I instantiate an object of type UseCompany inside CompanyGUI and do something like topPanel.updateDisplay(useCompany.add());

UseCompany add method passes TopPanels display method a List.
It would work but doesn't seem right to me. But it might be.


Yup! I would first declare the object as a global variable so you can call it anywhere within the class though (if you are definitely going to use the class you can instantiate via the constructor). So the TopPanel's display should display a list after the UseCompany add button is pressed? The logic can go inside of the listener, which can be implemented within the GUI's class.

edit - I bolded the class name in the quote because I'm referring to it in my post, sorry if that wasn't clear :p!

This post was edited by Petrusk on Nov 28 2015 09:56am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 28 2015 12:49pm
if i understand correctly, i think the reason you keep using static functions is because you're not sure how the button's action listener gets access to the ui fields in another panel? i would have the outer panel control all events and data inputs.

for your example:

Quote
You hit the button to add an employee, the button calls a method in UseCompany called add. It gets the data from the middle panel and adds an element to an ArrayList. UseCompany passes the ArrayList to the top panel to update the display.


some pseudocode

Code
void onAddEmployeeClick(){
CompanyBO.addEmployee(_middlePanel.getName(), _middlePanel.getDepartment(), ....);
refresh();
}
void refresh(){
List<Employee> employees = CompanyBO.getEmployees();
_topPanel.getEmployeeList().bind(employees); // or however you display them
}


there are different ways of doing it (mvc, multi-tier, etc), but this is kinda the way i'd do it.

i'm a fan of ui-bo-dao-to instead of mvc, but that's just me being old school.

This post was edited by carteblanche on Nov 28 2015 12:55pm
Member
Posts: 29,363
Joined: Mar 27 2008
Gold: 504.69
Nov 28 2015 09:11pm
Quote (Petrusk @ Nov 28 2015 11:54am)
Yup! I would first declare the object as a global variable so you can call it anywhere within the class though (if you are definitely going to use the class you can instantiate via the constructor). So the TopPanel's display should display a list after the UseCompany add button is pressed? The logic can go inside of the listener, which can be implemented within the GUI's class.

edit - I bolded the class name in the quote because I'm referring to it in my post, sorry if that wasn't clear :p!


Doesn't this couple the classes? And since the MiddlePanel was instantated in CompanyGUI, how would UseCompany be able to use it.
Quote (carteblanche @ Nov 28 2015 02:49pm)
if i understand correctly, i think the reason you keep using static functions is because you're not sure how the button's action listener gets access to the ui fields in another panel? i would have the outer panel control all events and data inputs.

for your example:



some pseudocode

Code
void onAddEmployeeClick(){
CompanyBO.addEmployee(_middlePanel.getName(), _middlePanel.getDepartment(), ....);
refresh();
}
void refresh(){
List<Employee> employees = CompanyBO.getEmployees();
_topPanel.getEmployeeList().bind(employees); // or however you display them
}


there are different ways of doing it (mvc, multi-tier, etc), but this is kinda the way i'd do it.

i'm a fan of ui-bo-dao-to instead of mvc, but that's just me being old school.


I don't think I am undertanding. The panels aren't together? So for instance, I can't call methods from TopPanel or MiddlePanel in UseCompany becuase they were instantiated in CompanyGUI.
I will google some of those terms.

/edit

MVC seems right. I want the user to manipulate data in the middle panel (controller), it get sent to UseCompany (model) to update a List, it sends the list to the top panel (view).

This post was edited by ROM on Nov 28 2015 09:14pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 28 2015 09:14pm
Quote (ROM @ Nov 28 2015 10:11pm)
Doesn't this couple the classes? And since the MiddlePanel was instantated in CompanyGUI, how would UseCompany be able to use it.

I don't think I am undertanding. The panels aren't together? So for instance, I can't call methods from TopPanel or MiddlePanel in UseCompany becuase they were instantiated in CompanyGUI.
I will google some of those terms.


oops. i used the name CompanyBO out of habbit, but in your example they'd be UseCompany (weird naming convention on UseCompany since it starts with a Verb). the methods i provided are in CompanyGUI

This post was edited by carteblanche on Nov 28 2015 09:16pm
Member
Posts: 29,363
Joined: Mar 27 2008
Gold: 504.69
Nov 28 2015 09:19pm
Quote (carteblanche @ Nov 28 2015 11:14pm)
oops. i used the name CompanyBO out of habbit, but in your example they'd be UseCompany (weird naming convention on UseCompany since it starts with a Verb). the methods i provided are in CompanyGUI


Ahh I see. So I can simplify it drastically and make it two files. One the creates the UI, and the methods for the top/middle panels,
and the other just to manipulate the list.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 28 2015 09:23pm
Quote (ROM @ Nov 28 2015 10:19pm)
Ahh I see. So I can simplify it drastically and make it two files. One the creates the UI, and the methods for the top/middle panels,
and the other just to manipulate the list.


this is the ui-bo-dao-to style that i'm used to, not mvc. not sure what your requirements are. i always found it most straight forward so i stuck with it, but mvc tends to be the trend these days
Member
Posts: 29,363
Joined: Mar 27 2008
Gold: 504.69
Nov 28 2015 09:26pm
Quote (carteblanche @ Nov 28 2015 11:23pm)
this is the ui-bo-dao-to style that i'm used to, not mvc. not sure what your requirements are. i always found it most straight forward so i stuck with it, but mvc tends to be the trend these days


This is kind of like the last lab for the year, so its more or less do whatever, then next semester we will go into more depth.
I like that style, makes the most sense to me.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll