11 Replies - 823 Views - Last Post: 17 June 2008 - 09:38 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

moving methods from gui into their own class

Posted 17 June 2008 - 07:47 PM

I think the best way to sort this problem out, is to find out how i do it on only 2 classes and then impliment this throughout my work. I have my Title class which holds my JFrame and is my parent class;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.*;

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("Ner");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();

}

private JPanel createButtonPanel() {

JButton btnRegister = new JButton("REGISTER");
JButton btnLogin = new JButton("LOGIN");
JButton btnExit = new JButton("Exit");

btnRegister.addActionListener(new RegisterListener(this));
btnLogin.addActionListener(new LoginListener(this));


btnExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) { 
cancel();
}
});

JPanel panel = new JPanel();
JPanel temp = new JPanel(new GridLayout(1,2, 5,5));
temp.add(btnRegister, BorderLayout.LINE_START);
temp.setPreferredSize(new Dimension(300, 40));
temp.add(btnExit, BorderLayout.CENTER);
temp.setPreferredSize(new Dimension(300, 40));
temp.add(btnLogin, BorderLayout.LINE_END);
temp.setPreferredSize(new Dimension(300, 40));
panel.add(temp);
return panel;
}



private JPanel createTitlePanel()  {

JLabel titleLabel = new JLabel("OFFICIAL RANKINGS");
titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 48));
titleLabel.setForeground(Color.black);
JPanel titlePanel = new JPanel();
titlePanel.setOpaque(false);
titlePanel.add(titleLabel);	
return titlePanel;
}

public void cancel(){

this.dispose();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Title().setVisible(true);
}
});
}

class LoginListener implements ActionListener {
JFrame jf;
LoginListener(JFrame jf) {
this.jf = jf;
}
public void actionPerformed(ActionEvent evt) {
new Login(jf);	
}	
}

class RegisterListener implements ActionListener {
JFrame jf;
RegisterListener(JFrame jf) {
this.jf = jf;
}
public void actionPerformed(ActionEvent evt) {
new Register(jf);	
}	
}

}



This is just to show you which class holds the JFrame. Next i have my Login class which extends a dialog and has the above class set as its father.
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");

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

}

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

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

public void Submit(){

userName2 = getUsername();
passWord2 = 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 clear(){

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

personsList.clear();
} 

public void cancel(){

this.dispose();

}

}



Now in the simplest form, i need to move the methods from my Login class into their own class. When i have tried this, i cant refer back to my Login class by doing Login.getUsername() due to static issues. How else could i do this?
(Ignore any references to other classes, these hold no gui code and work fine) cheers

This post has been edited by nick2price: 17 June 2008 - 07:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: moving methods from gui into their own class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: moving methods from gui into their own class

Posted 17 June 2008 - 08:11 PM

Nick2Price I can't beleive you are still on that problem !!! I am really sorry for you

I am really impressed by your code... Newbie never used GidBadLayout() it is the most complicated one... actually tried it once myself and quit...

Commented out all PersonInfo, PersonDAO and Register stuff and the GUI is nice... really I am impressed...

OK what is wrong about ?

class Title extends JFrame {
	Login login;
....
....
	class LoginListener implements ActionListener {
		JFrame jf;
		LoginListener(JFrame jf) {
			this.jf = jf;
		}
		public void actionPerformed(ActionEvent evt) {
			login = new Login(jf);   
// validate login info
// if ok start new frame allowing that new user
 
		}	
	}


Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: moving methods from gui into their own class

Posted 17 June 2008 - 08:35 PM

I know, this problem is encouraging me to take up cooking instead!!! I am not too sure what you mean by 'what is wrong about' as this can potentially have a double meaning.
This is where i have gotten stuck. I am creating a new class and moving the methods from the bottom of my Login class to it.
public class LogRegMethods
{
ArrayList personsList;
PersonDAO pDAO;
Login log;

public LogRegMethods()
{
personsList = new ArrayList();
pDAO = new PersonDAO();	
log = new Login(???);
}


You can problably tell by the question marks what i am confused about. In this new class, i need to relate to the Login class for instance when i want to retrieve the getUsername() method. i cant use Login.getUsername() as this dont work due to staic issues, so in my new class, i need to create an object of Login. I also cant just do
log=new Login() as its arguement is set to recieve a frame. So this is where i am stuck.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: moving methods from gui into their own class

Posted 17 June 2008 - 08:40 PM

And who is making

new LogRegMethods()

don't see it in your code...
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: moving methods from gui into their own class

Posted 17 June 2008 - 08:57 PM

new LogRegMethods() will be created in my Login class.
public class Login extends JDialog implements ActionListener
{
ArrayList personsList;
PersonDAO pDAO;
LogRegMethods lrM;

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();	
lrM = new LogRegMethods();
}  


But then how would i create Login in my LogRegMethods class when Login is set too public Login(JFrame father)?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:05 PM

lrM = new LogRegMethods(this);
if LogRegMethods expects a JDialog as parameter
or
lrM = new LogRegMethods(father);
if LogRegMethods expoects a JFrame as parameter... in that case you'll have to save "father" in an instance variable

class Login {
   JFrame fathe;
   Login(JFrame father) {
	   this.father = father;
	   ....


Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:18 PM

LogRegMethods doesnt require anything as a parameter. Its the Login that requires the parameter. Let me show you
public class LogRegMethods
{
ArrayList personsList;
PersonDAO pDAO;
Login log;

public LogRegMethods()
{
personsList = new ArrayList();
pDAO = new PersonDAO();	
log = new Login();
}
}


Error produced: Cannot find symbol constructor Login()
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:23 PM

View Postnick2price, on 17 Jun, 2008 - 09:18 PM, said:

LogRegMethods doesnt require anything as a parameter. Its the Login that requires the parameter. Let me show you
public class LogRegMethods
{
ArrayList personsList;
PersonDAO pDAO;
Login log;

public LogRegMethods()
{
personsList = new ArrayList();
pDAO = new PersonDAO();	
log = new Login();
}
}


Error produced: Cannot find symbol constructor Login()


Yes because the constructor of Login expects a JFrame as parameter
so you have to pass this JFrame as parameter to LogRegMethods so that LogRegMethod can passed it to Login

STOP STOP STOP
But if Login calls LogRegMethods that calls Login that calls LogRegMethod you are in an infinite loop
major design/architecture problem

This post has been edited by pbl: 17 June 2008 - 09:28 PM

Was This Post Helpful? 1
  • +
  • -

#9 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:32 PM

I was thinking there was a design problem! I have to create it in a 3 tier model and this is problably where my design went bad. I was thinking why does this have to call that to call this etc? I will take another look at the design and see if i can make improvements.
cheers
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:34 PM

View Postnick2price, on 17 Jun, 2008 - 09:32 PM, said:

I was thinking there was a design problem! I have to create it in a 3 tier model and this is problably where my design went bad. I was thinking why does this have to call that to call this etc? I will take another look at the design and see if i can make improvements.
cheers

no problem... 7h30 in UK... or you wake up early or you worked very late
Thanks for the thank always appreciated... for what It would worth

This post has been edited by pbl: 17 June 2008 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:36 PM

5.30 in UK. While i am not at uni, i live off American time because i play poker semi professionally, so, nearly bedtime.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: moving methods from gui into their own class

Posted 17 June 2008 - 09:38 PM

View Postnick2price, on 17 Jun, 2008 - 09:36 PM, said:

5.30 in UK. While i am not at uni, i live off American time because i play poker semi professionally, so, nearly bedtime.

Impressive wish I can do that for leaving... some days I'am tempted
Usually when I go down Souht I pay my travel with Poker.... but far from being a professionnal
Hey !! did you tried my Carribean poker game ? Free of charge :D

This post has been edited by pbl: 17 June 2008 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1