Welcome to Dream.In.Code
Become a Java Expert!

Join 150,072 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,787 people online right now. Registration is fast and FREE... Join Now!




3 tier program, seperating code

 
Reply to this topicStart new topic

3 tier program, seperating code

nick2price
14 Jun, 2008 - 04:57 PM
Post #1

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
I have gone braindead!!! My project has to be in the pattern of a 3 tier, where i have to keep GUI code, logic and business code seperate. This isnt a problem to do, my problem lies in the way my code is set out, i cant create a class object without passing it a 'father' everytime. here is one of my classes.
CODE
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.*;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.*;

public class Login extends JDialog implements ActionListener
{
        ArrayList personsList;
        PersonDAO pDAO;
        
           JLabel userName, passWord;
           JTextField userName1;
           JPasswordField passWord1;
           JButton jbnClear, jbnSubmit, jbnCancel;
           
           String userName2, passWord2;
           Container cPane;

Login(JFrame father) {
        
        super(father);

    
        userName2  = "";
        passWord2   = "";

    
        createGUI();
    
        personsList = new ArrayList();
    
        // creating PersonDAO object
        pDAO = new PersonDAO();    
}
  
public void createGUI(){


        cPane = getContentPane();
        setLayout(new GridBagLayout());
           
           //Arrange components on contentPane and set Action Listeners to each JButton
           arrangeComponents();
           
           setSize(210,170);
           setTitle("Login");
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

       }
       
public void arrangeComponents(){
    
           userName = new JLabel("Username");
           passWord = new JLabel("Password");

           userName1   = new JTextField(20);
           passWord1   = new JPasswordField(20);

           jbnClear  = new JButton("Clear");
           jbnSubmit = new JButton("Submit");
           jbnCancel = new JButton("Cancel");
           
           GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
        gridBagConstraintsx01.gridx = 0;
        gridBagConstraintsx01.gridy = 0;
        gridBagConstraintsx01.insets = new Insets(5,5,5,5);
        cPane.add(userName, gridBagConstraintsx01);
        
        GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
        gridBagConstraintsx02.gridx = 1;
        gridBagConstraintsx02.insets = new Insets(5,5,5,5);
        gridBagConstraintsx02.gridy = 0;
        gridBagConstraintsx02.gridwidth = 2;
        gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
        cPane.add(userName1, gridBagConstraintsx02);
        
        GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
        gridBagConstraintsx03.gridx = 0;
        gridBagConstraintsx03.insets = new Insets(5,5,5,5);
        gridBagConstraintsx03.gridy = 1;
        cPane.add(passWord, gridBagConstraintsx03);
        
        GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
        gridBagConstraintsx04.gridx = 1;
        gridBagConstraintsx04.insets = new Insets(5,5,5,5);
        gridBagConstraintsx04.gridy = 1;
        gridBagConstraintsx04.gridwidth = 2;
        gridBagConstraintsx04.fill = GridBagConstraints.BOTH;
        cPane.add(passWord1, gridBagConstraintsx04);
        
        GridBagConstraints gridBagConstraintsx09 = new GridBagConstraints();
        gridBagConstraintsx09.gridx = 0;
        gridBagConstraintsx09.gridy = 4;
        gridBagConstraintsx09.insets = new Insets(5,5,5,5);
        cPane.add(jbnClear, gridBagConstraintsx09);

        GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
        gridBagConstraintsx10.gridx = 1;
        gridBagConstraintsx10.gridy = 4;
        gridBagConstraintsx10.insets = new Insets(5,5,5,5);
        cPane.add(jbnSubmit, gridBagConstraintsx10);
        
        GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
        gridBagConstraintsx11.gridx = 1;
        gridBagConstraintsx11.gridy = 5;
        gridBagConstraintsx11.insets = new Insets(5,5,5,5);
        cPane.add(jbnCancel, gridBagConstraintsx11);
        
           jbnClear.addActionListener(this);
           jbnSubmit.addActionListener(this);
           jbnCancel.addActionListener(this);
       }
    
public void actionPerformed (ActionEvent e){
           
           
                if (e.getSource() == jbnClear){
             clear();
        }

           else if (e.getSource() == jbnSubmit){            
                Submit();
           }
           
           else if (e.getSource() == jbnCancel){            
                cancel();
           }

       }
public void clear(){
    
           userName1.setText("");
           passWord1.setText("");
    
           personsList.clear();
     }

public void Submit(){
    
     userName2=userName1.getText();
     passWord2 = new String(passWord1.getPassword());

     PersonInfo person = new PersonInfo(userName2, passWord2);
    
     if(userName2.equals("") || passWord2.equals("")){
         
             JOptionPane.showMessageDialog(null, "Please complete all fields.");
      
     }
     else
     {
     pDAO.loginPerson(person);
     }    
        
    }

public void cancel(){
    
    this.dispose();
    
    }
    
}


All of my methods at the bottom, i have to basically move into another class. My PersonDAO pDAO; is a class object i had used in this class, and that is no problem to move about. But then if you lookat my submit method, how could i do this line
userName2=userName1.getText();
passWord2 = new String(passWord1.getPassword());
in another class? This post is problably getting a bit confusing now, i am very confused. So if you need anymore info, just lemme know.
cheers
User is online!Profile CardPM
+Quote Post

pbl
RE: 3 Tier Program, Seperating Code
14 Jun, 2008 - 05:28 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(nick2price @ 14 Jun, 2008 - 05:57 PM) *

I have to keep GUI code, logic and business code seperate.


This the way to do it. I have spent 35 years teaching that to new programmers.

There is nothing wrong by passing a "father" to the constructor of another class. Anyhow the other class has not to know if the information is coming from (JTextField, OptionPane, ....)

so do not write code like:

String username = login.loginTextField.getText();

but write in your Login a method getUsername() that would like like

CODE

String getUsername() {
   return loginTextField().getText();
}


so the son does not know it is coming from a JTextField or whatever else

Good programming means keeping GUI, business rules and data access separated.
If you switch from Oracle database to an MS-Acces databse neither the GUI and the business rules part has to know about it an the code should not have to change.
If you switch from a console mode application to a GUI application the data access code should not change neither the business rules part.

This post has been edited by pbl: 14 Jun, 2008 - 05:32 PM
User is offlineProfile CardPM
+Quote Post

nick2price
RE: 3 Tier Program, Seperating Code
14 Jun, 2008 - 06:40 PM
Post #3

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
hmmm, gone and confused myself again. I have made a few changes to my Login class. I created a new constructor with no arguement.
CODE
public Login(JFrame father) {
        
        super(father);

    
        createGUI();
    
}
public Login() {
        
    
        userName2  = "";
        passWord2   = "";
    
        personsList = new ArrayList();
        pDAO = new PersonDAO();    
        
}  


Then my new method class for this is
CODE
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class LogRegMethods
{
        ArrayList personsList;
        PersonDAO pDAO;
        Login log;
        String userName2, passWord2;

public LogRegMethods()
{
        userName2  = "";
        passWord2   = "";
        
        personsList = new ArrayList();
        pDAO = new PersonDAO();    
        log = new Login();

}

public void Submit(){
    
     userName2 = log.getUsername();
     passWord2 = log.getPassword();

     PersonInfo person = new PersonInfo(userName2, passWord2);
    
     if(userName2.equals("") || passWord2.equals("")){
         
             JOptionPane.showMessageDialog(null, "Please complete all fields.");
      
     }
     else
     {
     pDAO.loginPerson(person);
     }    
        
    }


}


Now, because i have to call up this method in my login class, i have to create an object of this class in my login class. So i done
CODE
public class Login extends JDialog implements ActionListener
{
        ArrayList personsList;
        PersonDAO pDAO;
        LogRegMethods lrM = new LogRegMethods();


The finally i changed one of my action events in my login class too
CODE
    else if (e.getSource() == jbnSubmit){            
                lrM.Submit();
           }


Everything compiles fine, but the login button no longer works in my application. Can you see my mistake?
User is online!Profile CardPM
+Quote Post

pbl
RE: 3 Tier Program, Seperating Code
14 Jun, 2008 - 07:57 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Just told you nothing was wrong about passing a father to an object during its creation...
Your reaction to that ? You decided to create a constructor without argument (father) don't follow you at all.
Would have been better if I didn't say a word at all.

This post has been edited by pbl: 14 Jun, 2008 - 08:02 PM
User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: 3 Tier Program, Seperating Code
14 Jun, 2008 - 11:11 PM
Post #5

Be my fiduciary ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 2,139



Thanked: 27 times
Dream Kudos: 400
My Contributions
QUOTE(pbl @ 14 Jun, 2008 - 10:57 PM) *


Would have been better if I didn't say a word at all.


Your advice probably helps lots of people who lurk and never say anything.
I for one appreciate every programmer who gives their time and advice to help those of us who want to learn. smile.gif
User is offlineProfile CardPM
+Quote Post

mensahero
RE: 3 Tier Program, Seperating Code
15 Jun, 2008 - 01:05 AM
Post #6

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(OliveOyl3471 @ 15 Jun, 2008 - 12:11 AM) *

Your advice probably helps lots of people who lurk and never say anything.
I for one appreciate every programmer who gives their time and advice to help those of us who want to learn. smile.gif


Well Said. I learn a lot from reading replies from different problems. The only thing I can't understand is that why those who just lurk and read replies understand the replies but the Original poster who has the problem don't.

It's like; It's there problem and someone gave a good answer then someone who don't know the problem found the answer as valuable information while the OP who has the problem doesn't even take the time to read and understand the answer in front of them. Ironic.

Keep up those good replies. Some members find them valuable.

This post has been edited by mensahero: 15 Jun, 2008 - 01:06 AM
User is offlineProfile CardPM
+Quote Post

nick2price
RE: 3 Tier Program, Seperating Code
15 Jun, 2008 - 04:37 AM
Post #7

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
I appreciate your replies so much, i have no idea what i would do without you. Ignore my previous post, i was just trying to take a bit of initiative and try to do it on my own. The problem i was having with your reply about passing it my father frame, i am not sure how to do this.
CODE
public LogRegMethods()
{
        userName2  = "";
        passWord2   = "";

        personsList = new ArrayList();
        pDAO = new PersonDAO();    
        log = new Login();

}


What will my father frame be? I have tried doing
[code]public class LogRegMethods
{
ArrayList personsList;
PersonDAO pDAO;
Login log;
String userName2, passWord2;
JFrame jf;

public LogRegMethods()
{
userName2 = "";
passWord2 = "";

personsList = new ArrayList();
pDAO = new PersonDAO();
log = new Login(jf);

}

But i get a really long stack trace when i run my program and click on my login button.
User is online!Profile CardPM
+Quote Post

nick2price
RE: 3 Tier Program, Seperating Code
15 Jun, 2008 - 04:28 PM
Post #8

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Any idea which father frame i should pass it?
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month