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

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




How to pass parent frame into method

 
Closed TopicStart new topic

How to pass parent frame into method, I still need a father (frame)

nick2price
16 Jun, 2008 - 02:40 PM
Post #1

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
pbl, so confused, tried everything. I have my Login class where i create my GUI, and i am trying to move the methods to their own class.
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;

public Login(JFrame father) {

super(father);


createGUI();


userName2  = "";
passWord2   = "";

personsList = new ArrayList();
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");

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();
}

}

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

String getPassword() {
return new String(passWord1.getPassword());
}

public void clear(){

userName1.setText("");
passWord1.setText("");

personsList.clear();
}

public void cancel(){

this.dispose();

}

}


I removed all the layout to shorten the code. Now i am creating a new class with the methods associated with this class.
CODE
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;

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);
}    

}
}

My problem relates to this
CODE
log = new Login();

because in my Login class, its set like
CODE
public Login(JFrame father)

I have no idea what frame to pass it in my method class or how to do it.
cheers
User is offlineProfile CardPM
+Quote Post

pbl
RE: How To Pass Parent Frame Into Method
16 Jun, 2008 - 02:51 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
And where are your PersonInfo and PersonDAO classes ?

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

nick2price
RE: How To Pass Parent Frame Into Method
16 Jun, 2008 - 02:55 PM
Post #3

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
I have them in my package. Passing instances of these classes around is no problem because they have no frame, its just the Login class i cant move about.
User is offlineProfile CardPM
+Quote Post

pbl
RE: How To Pass Parent Frame Into Method
16 Jun, 2008 - 03:11 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(nick2price @ 16 Jun, 2008 - 03:55 PM) *

I have them in my package. Passing instances of these classes around is no problem because they have no frame, its just the Login class i cant move about.


There is no frame in Login neither in LogRegMethods
You said there is no frame in neither PersonalInfo or PersonDAO
So which frame do you want to pass to Login ?

I guess Login does not need a father at all

This post has been edited by pbl: 16 Jun, 2008 - 03:12 PM
User is offlineProfile CardPM
+Quote Post

nick2price
RE: How To Pass Parent Frame Into Method
16 Jun, 2008 - 03:23 PM
Post #5

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
The class with my father frame is
CODE

public class Title extends JFrame {

public Title() {

JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());

Icon image = new ImageIcon("london.png");
JLabel centerPanel1 = new JLabel(image);

cp.add(centerPanel1, BorderLayout.CENTER);
cp.add(createTitlePanel(), BorderLayout.NORTH);
cp.add(createButtonPanel(), BorderLayout.SOUTH);

setContentPane(cp);
setTitle("Nerk");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();

}

(Not whole class) Now basically, i had GUI and methods together in my Login class, which is a dialog. i removed the methods to create a new class, so that its not all together. In my new class with the methods, say if i want to get the user input from the textfield from my Login class, i cant use Login.getText as you cant mix static and non static methods. So in my methods class, i need to create a reference to my Login class, simular to how i have made a reference to my PersonDAO class. So to do this, i do
CODE
Login log = new Login():

but i cant use this, because in my Login class, the constructor is set too
CODE

public Login(JFrame father)

So what frame should i pass it in my methods class?
User is offlineProfile CardPM
+Quote Post

pbl
RE: How To Pass Parent Frame Into Method
16 Jun, 2008 - 03:26 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(nick2price @ 16 Jun, 2008 - 04:23 PM) *

The class with my father frame is


So who is doing a "new Title()" ?

User is offlineProfile CardPM
+Quote Post

pbl
RE: How To Pass Parent Frame Into Method
16 Jun, 2008 - 03:33 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Worst case, if there is only one instance of Title you can always do
Not a very good practice (OO purits will be against it) but il will work:

CODE

class Title extends JFrame {
    static Title instance;

    Title() {
        instance = this;
        ....
    }
}


or even in Login

CODE

    Login() {
       super(Title.instance);
       ....


and later on to create Login:

CODE

    Login login = new Login(Title.instance);


This post has been edited by pbl: 16 Jun, 2008 - 03:42 PM
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/9/09 01:21AM

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