How to pass values between frames? (like login session)

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

45 Replies - 2823 Views - Last Post: 06 March 2013 - 05:31 AM Rate Topic: -----

#16 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 24 February 2013 - 06:41 PM

Is is possible to pass variable "user" from dbConnect method to jButtonActionPerformed?
Was This Post Helpful? 0
  • +
  • -

#17 pbl  Icon User is offline

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

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

Re: How to pass values between frames? (like login session)

Posted 24 February 2013 - 06:54 PM

I just joined that thread and it seems to me that you complicate your life a lot for nothing

YES you can pass whatever you want from one class to another what is your problem ?

String are immutable in Java is that whjat cause you a problem ?
If it is the case just create the class

class User {
   String username;
}



pass a User object to all the classes/method that require it
change the username value on the fly if require

If does not fix it, clearly explain your problem (or more exactly what you perceive as a problem) because there in no problem there !!!
Was This Post Helpful? 0
  • +
  • -

#18 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 25 February 2013 - 05:40 AM

View Postpbl, on 24 February 2013 - 06:54 PM, said:

I just joined that thread and it seems to me that you complicate your life a lot for nothing

YES you can pass whatever you want from one class to another what is your problem ?

String are immutable in Java is that whjat cause you a problem ?
If it is the case just create the class

class User {
   String username;
}



pass a User object to all the classes/method that require it
change the username value on the fly if require

If does not fix it, clearly explain your problem (or more exactly what you perceive as a problem) because there in no problem there !!!


I know that I'm really bugging programmers here. I'm looking for your kind consideration.
Please pardon me for I have a poor understanding java in a natural way like other programmers do.
I have my own way on understanding, more like by codes.

I'll try to explain how I really wanted the program to be.

In Frame1, variable user (username and user are totally different variables) is generated through user's input, which is:
user = jTextField1.getText();

And passes it to Frame2 by:
Manager f2 = new Manager();
f2.dbConnect(user);
f2.setVisible(true);
this.setVisible(false);


It passes to the Frame2's dbConnect() method. At first I thought that by passing to dbConnect() method,
all the methods in Frame2 can use it to pass it to Frame3, Frame4 and so on. What I want to know is that
how can I pass it to Frame2 but not just dbConnect() method can I use it, and all of Frame2's method can use
it so that it can also be passed on the following frames (Frame3, Frame4, Frame5, etc...).

I tried f2.Manager(user); because it's the name of the Frame2, I thought all the methods in Frame2 would be able
to use it. But as what g00se said, I can't use it at the moment.

I'm ashamed of myself, please close this thread if I've gone too far to ask for help. >.<
Was This Post Helpful? 0
  • +
  • -

#19 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 07:21 AM

I think there are a lot ways of getting a variable from private classes such as jButton1. I just don't know the proper way to do it on my case.
It seems that it is preventing me from accessing the variables declared in jButtonActionPerformed, can anyone tell me why and how to solve it?

Tried browsing on google on different ways of passing variables from private classes like jButton but most of them were too complicated or advanced and beyond my knowledge.

Frame1:
   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        user = jTextField1.getText();
        pass = new String(jPasswordField1.getPassword());
               
        try {
            if (user != null && pass != null) {
                sql = "Select * from users_table Where username='" + user + "' and password='" + pass + "'";
                rs = stmt.executeQuery(sql);
                if(rs != null && rs.next()){
                    if ("manager".equals(rs.getString("role"))) {
                        JOptionPane.showMessageDialog(null, "You are now logon as Manager!");
                        newUser = rs.getString("username");
                        newPass = rs.getString("password");
                        
                        Manager f2 = new Manager();
                        
                        f2.setVisible(true);
                        this.setVisible(false);
                    }
                    else if ("sales".equals(rs.getString("role"))) {
                        JOptionPane.showMessageDialog(null, "You are now logon as Sales Consultant!");
                        Sales f3 = new Sales();
                        f3.setVisible(true);
                        this.setVisible(false);
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "Invalid username or password!", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        } 
        catch (SQLException err) {
            JOptionPane.showMessageDialog(this, err.getMessage());
        }
    }



Frame 2:
public void profile() {
        System s1 = new System();    
        user1 = s1.newUser;
        pass1 = s1.newPass;
        try {
            //rs.next();
            sql = "Select * from users_table Where username='" + user1 + "' and password='" + pass1 + "'";
            rs = stmt.executeQuery(sql);
            rs.next();
            Name = rs.getString("Name");
            
            jTextField1.setText(Name);

        } 
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }


Was This Post Helpful? 0
  • +
  • -

#20 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 07:24 AM

Quote

Tried doing like this (for frame1):
Why? 'user' seems to be unused in dbConnect (unless i missed it)
Was This Post Helpful? 0
  • +
  • -

#21 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 07:38 AM

View Postg00se, on 26 February 2013 - 07:24 AM, said:

Quote

Tried doing like this (for frame1):
Why? 'user' seems to be unused in dbConnect (unless i missed it)



Sorry, my mind is really confused yesterday.. I supposed it should be passed from jButton to Frame 2's profile method.
Was This Post Helpful? 0
  • +
  • -

#22 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 09:13 AM

In broad terms, it should be passed to where it's going to be used. Sometimes that will be into the ctor of another class, sometimes to a method in that other class. That's of course assuming that another class is involved
Was This Post Helpful? 0
  • +
  • -

#23 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 03:22 PM

View Postg00se, on 26 February 2013 - 09:13 AM, said:

In broad terms, it should be passed to where it's going to be used. Sometimes that will be into the ctor of another class, sometimes to a method in that other class. That's of course assuming that another class is involved


Yeah, it should be passed to a method of other class but how? Based on my last attempt, it doesn't get the value of the variable.
Was This Post Helpful? 0
  • +
  • -

#24 pbl  Icon User is offline

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

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

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 03:34 PM

ou should avoid to mix Database access with GUI manipulation
Actually, you GUI should ignore if the data it is using is coming from a Database, a flat file, ...

Have classes that handles the GUI and class that Hndle the databse connection

class MyDB {
    Connection con;
...

    boolean isUserValid(String username, String password) {
        ... access DB
        ... validate 
        return true/false;
    }

class Frame1 extends JFrame {
   MyDB db;

   Frame1() {
      db = new MyDB();

   ...

   public void actionPerformed(ActionEvent e) {
     if(db.isUserValid(userTextField.getText(), passwTextField.getText()) {
        ... user authorized



If you don't do that then when you will change your database system, like from MySQL to Oracle, you will have to modify 100 of areas in your GUI and re-test everything.

Also making the classes completly different will help you test without the Database overhead, you can hardcode

    boolean isUserValid(String username, String password) {
      return true;
    }


Was This Post Helpful? 0
  • +
  • -

#25 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 26 February 2013 - 05:47 PM

View Postpbl, on 26 February 2013 - 03:34 PM, said:

ou should avoid to mix Database access with GUI manipulation
Actually, you GUI should ignore if the data it is using is coming from a Database, a flat file, ...

Have classes that handles the GUI and class that Hndle the databse connection

class MyDB {
    Connection con;
...

    boolean isUserValid(String username, String password) {
        ... access DB
        ... validate 
        return true/false;
    }

class Frame1 extends JFrame {
   MyDB db;

   Frame1() {
      db = new MyDB();

   ...

   public void actionPerformed(ActionEvent e) {
     if(db.isUserValid(userTextField.getText(), passwTextField.getText()) {
        ... user authorized



If you don't do that then when you will change your database system, like from MySQL to Oracle, you will have to modify 100 of areas in your GUI and re-test everything.

Also making the classes completly different will help you test without the Database overhead, you can hardcode

    boolean isUserValid(String username, String password) {
      return true;
    }




Thanks! Now I'm able to pass variables by using instance variables but when I tried to pass in three frames (from Frame1 to Frame2 then to Frame3 then back to Frame2 again),
server says "Data source rejected establishment of connection, message from server: "Too many connections"". It also prevents me from going to the Frame3.

Frame1:
Manager f2 = new Manager();
f2.profile(user, pass);
f2.setVisible(true);



Frame2:
public void profile(String user, String pass) {
        try {
            sql = "Select * from users_table Where username='" + user + "' and password='" + pass + "'";
            rs = stmt.executeQuery(sql);
            rs.next();
            name = rs.getString("Name");
            contact = rs.getString("Contact");
            address = rs.getString("Address");
            position = rs.getString("Position");
            
            jTextField1.setText(name);
            jTextField2.setText(contact);
            jTextField3.setText(address);
            jTextField4.setText(position);
            ManagerReportRecords m1 = new ManagerReportRecords();
            m1.profile(user,pass);

        } 
        catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
}



Frame3:
    public void profile(String user, String pass) {
        Manager f2 = new Manager();
        f2.profile(user,pass);
}



I think it's too redundant to use it that way, but I need to fill the jTextFields in Frame2 when I press the "back" button of Frame3.
Was This Post Helpful? 0
  • +
  • -

#26 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: How to pass values between frames? (like login session)

Posted 27 February 2013 - 04:14 AM

Using frames like that is messy. It's seldom seen in professional apps. Your app should have ONE frame and any other window should normally be of the dialog kind. See
http://technojeeves....with-cardlayout
That can be extended to include other views into your app
Was This Post Helpful? 0
  • +
  • -

#27 pbl  Icon User is offline

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

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

Re: How to pass values between frames? (like login session)

Posted 27 February 2013 - 04:49 AM

Despite the fact that you should use a CardLayout there is no reason to have more than one connection into your database. There are 2 solutions:

- make MyDB a singleton and thus always maintaining a single connection
- close the connection after each SQL call performed by a method of MyDB
Was This Post Helpful? 0
  • +
  • -

#28 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 27 February 2013 - 05:03 AM

View Postg00se, on 27 February 2013 - 04:14 AM, said:

Using frames like that is messy. It's seldom seen in professional apps. Your app should have ONE frame and any other window should normally be of the dialog kind. See
http://technojeeves....with-cardlayout
That can be extended to include other views into your app


Well, I'm not yet a professional.. But I think I would start learning about it later on after this project. Thank you for informing me about that. :)

View Postpbl, on 27 February 2013 - 04:49 AM, said:

Despite the fact that you should use a CardLayout there is no reason to have more than one connection into your database. There are 2 solutions:

- make MyDB a singleton and thus always maintaining a single connection
- close the connection after each SQL call performed by a method of MyDB


Just did, and it works, thanks! :)
Was This Post Helpful? 0
  • +
  • -

#29 Hypernal  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 125
  • Joined: 29-May 12

Re: How to pass values between frames? (like login session)

Posted 01 March 2013 - 04:45 PM

Can anyone help me with this, my last problem..
http://answers.yahoo...01152525AA6F55q
Was This Post Helpful? 0
  • +
  • -

#30 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: How to pass values between frames? (like login session)

Posted 01 March 2013 - 04:50 PM

We have been helping you with this for 20 - 30 posts. You've been given almost every possible perspective and answer on the topic. What more can we do, can anyone do? User getter and setter methods to access the variables between class instances.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4