45 Replies - 821 Views - Last Post: 06 March 2013 - 05:31 AM
#16
Re: How to pass values between frames? (like login session)
Posted 24 February 2013 - 06:41 PM
#17
Re: How to pass values between frames? (like login session)
Posted 24 February 2013 - 06:54 PM
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 !!!
#18
Re: How to pass values between frames? (like login session)
Posted 25 February 2013 - 05:40 AM
pbl, on 24 February 2013 - 06:54 PM, said:
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.
#19
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 07:21 AM
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());
}
#20
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 07:24 AM
Quote
#21
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 07:38 AM
#22
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 09:13 AM
#23
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 03:22 PM
g00se, on 26 February 2013 - 09:13 AM, said:
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.
#24
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 03:34 PM
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;
}
#25
Re: How to pass values between frames? (like login session)
Posted 26 February 2013 - 05:47 PM
pbl, on 26 February 2013 - 03:34 PM, said:
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.
#26
Re: How to pass values between frames? (like login session)
Posted 27 February 2013 - 04:14 AM
http://technojeeves....with-cardlayout
That can be extended to include other views into your app
#27
Re: How to pass values between frames? (like login session)
Posted 27 February 2013 - 04:49 AM
- make MyDB a singleton and thus always maintaining a single connection
- close the connection after each SQL call performed by a method of MyDB
#28
Re: How to pass values between frames? (like login session)
Posted 27 February 2013 - 05:03 AM
g00se, on 27 February 2013 - 04:14 AM, said:
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.
pbl, on 27 February 2013 - 04:49 AM, said:
- 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!
#29
Re: How to pass values between frames? (like login session)
Posted 01 March 2013 - 04:45 PM
#30
Re: How to pass values between frames? (like login session)
Posted 01 March 2013 - 04:50 PM
|
|

New Topic/Question
Reply





MultiQuote



|