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