| | |
Java value assigned from within ActionPerformed not maintained
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 35
Reputation:
Solved Threads: 5
Hi,
ive been trying to assign a value to a variable from within the ActionPerformed method, but the value seems to be lost as soon as ActionPerformed method ends. here's the code
When I call the getUserType method, it always returns 0. i've checked if the usertype is assigned any value within the ActionPerformed method and it does. what am i doing wrong here?
thanks in advance
ive been trying to assign a value to a variable from within the ActionPerformed method, but the value seems to be lost as soon as ActionPerformed method ends. here's the code
Java Syntax (Toggle Plain Text)
public class Log{ private JTextField user = new JTextField(10); private JPasswordField pass = new JPasswordField(10); private int usertype=0; private JPanel panel = new JPanel(); public Log(){ panel.add(user); panel.add(pass); panel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Login logs = new Login(user.getText(), pass.getText()); //this one is querying the database and getting an int value for user type. usertype = logs.getUsertype(); } }); //some more codes to add to frame and stuff } public int getUserType(){ return usertype; } }
thanks in advance
Last edited by kekkaishi; Oct 21st, 2009 at 8:43 pm.
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
0
#2 Oct 21st, 2009
In actionPerformed, you create a new "Login", not a new "Log". Why didn't you post your Login class? (Post it
)
PS: It seems to me from the way that you created a new "Login" and then called a method identical to one I see in the Log class, that Login extends Log, and that in the Login class, you did not override the getUserType method. But that's just a guess. I'm pretty sick right now, but post your class and someone will take a look at it.
)PS: It seems to me from the way that you created a new "Login" and then called a method identical to one I see in the Log class, that Login extends Log, and that in the Login class, you did not override the getUserType method. But that's just a guess. I'm pretty sick right now, but post your class and someone will take a look at it.
Last edited by BestJewSinceJC; Oct 21st, 2009 at 9:26 pm.
Out.
•
•
Join Date: Oct 2009
Posts: 35
Reputation:
Solved Threads: 5
0
#3 Oct 22nd, 2009
•
•
•
•
In actionPerformed, you create a new "Login", not a new "Log". Why didn't you post your Login class? (Post it)
PS: It seems to me from the way that you created a new "Login" and then called a method identical to one I see in the Log class, that Login extends Log, and that in the Login class, you did not override the getUserType method. But that's just a guess. I'm pretty sick right now, but post your class and someone will take a look at it.
nope, Log is not an extension of Login class, in fact, Log actually extends JInternalFrame, here is the two classes.
log class
Java Syntax (Toggle Plain Text)
public class LogUI extends JInternalFrame{ private JTextField user = new JTextField(10); private JPasswordField pass = new JPasswordField(10); private JPanel panel = new JPanel(); private JButton button = new JButton("Login"); private int usertype; public LogUI(){ panel.add(user); panel.add(pass); panel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Login lg = new Login(user.getText(), pass.getText()); usertype = lg.getUsertype(); } }); this.add(panel); this.setTitle("Login"); this.pack(); this.setVisible(true); } public int getUsertype() { return usertype; }
Java Syntax (Toggle Plain Text)
public class Login { private int usertype; private ResultSet rs; private Statement st; public Login(String user, String pass){ try{ SgmDb db = new SgmDb(); st = db.connectToDB().createStatement(); String query = "SELECT username, password, userType FROM user WHERE username='"+user+"' AND password = '"+pass+"'"; st.executeQuery(query); rs = st.getResultSet(); while(rs.next()){ usertype = Integer.parseInt(rs.getString("userType")); } rs.last(); if(rs.getRow()==0){ JOptionPane.showMessageDialog(null, "Wrong username or password"); } }catch(Exception e){ JOptionPane.showMessageDialog(null, "Contact Admin, there is a problem"); } } public int getUsertype(){ return this.usertype; } }
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
0
#4 Oct 22nd, 2009
check s:
Java Syntax (Toggle Plain Text)
String s = rs.getString("userType"); System.out.println("|"+s+"|"); usertype = Integer.parseInt(s);
•
•
Join Date: Oct 2009
Posts: 35
Reputation:
Solved Threads: 5
0
#5 Oct 22nd, 2009
•
•
•
•
check s:
Java Syntax (Toggle Plain Text)
String s = rs.getString("userType"); System.out.println("|"+s+"|"); usertype = Integer.parseInt(s);
Java Syntax (Toggle Plain Text)
usertype = Integer.parseInt(rs.getString("userType"));
say for example the following code.
Java Syntax (Toggle Plain Text)
Log logObject = new Log(); int x = logObject.getUserType(); // int x is always 0. this is my problem.
Last edited by kekkaishi; Oct 22nd, 2009 at 11:06 am.
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
0
#7 Oct 22nd, 2009
•
•
•
•
Hmm... try defining that 'ActionListener' outside the constructor.
Java Syntax (Toggle Plain Text)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class RandomTest extends JPanel implements WindowListener{ int random = 0; public RandomTest(){ JButton button = new JButton("Click me"); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub random = 5; System.out.println("Variable random is " + random); } }); this.add(button); } public static void main(String[] args){ JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); RandomTest test = new RandomTest(); frame.add(test); frame.addWindowListener(test); frame.setSize(300,300); frame.setVisible(true); } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub System.out.println("Window is closing. Variable random = " + random); } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } }
Out.
•
•
Join Date: Oct 2009
Posts: 35
Reputation:
Solved Threads: 5
0
#8 Oct 22nd, 2009
•
•
•
•
Wrong. Run my code if you don't believe me.
Java Syntax (Toggle Plain Text)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class RandomTest extends JPanel implements WindowListener{ int random = 0; public RandomTest(){ JButton button = new JButton("Click me"); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub random = 5; System.out.println("Variable random is " + random); } }); this.add(button); } public static void main(String[] args){ JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); RandomTest test = new RandomTest(); frame.add(test); frame.addWindowListener(test); frame.setSize(300,300); frame.setVisible(true); } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub System.out.println("Window is closing. Variable random = " + random); } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } }
thannks •
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
0
#9 Oct 22nd, 2009
Ok.
If you do that in your actionPerformed, then you print userType from within another method, after you have clicked on your button, then you will see that usertype changed. The problem you are having is that you must have done something like this:
usertype (in the println that I commented next to) will always print 0 because it is in the constructor. You added an action listener to the button, but you didn't click the button yet. So after the code I posted above adds the action listener, it immediately goes and prints "usertype" - but you haven't clicked the button yet, and therefore usertype has not been changed yet, so it prints 0!
Basically, I don't think you don't have a problem, you just think you have a problem. If you *really* want to verify that "usertype" is being set correctly, just do a System.out.println(getUsertype()); inside of the actionPerformed method at some point after you set the usertype.
Java Syntax (Toggle Plain Text)
usertype = logs.getUsertype(); System.out.println(usertype);
If you do that in your actionPerformed, then you print userType from within another method, after you have clicked on your button, then you will see that usertype changed. The problem you are having is that you must have done something like this:
Java Syntax (Toggle Plain Text)
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Login logs = new Login(user.getText(), pass.getText()); //this one is querying the database and getting an int value for user type. usertype = logs.getUsertype(); } }); //some more codes to add to frame and stuff System.out.println(usertype); //WILL ALWAYS PRINT 0 }
usertype (in the println that I commented next to) will always print 0 because it is in the constructor. You added an action listener to the button, but you didn't click the button yet. So after the code I posted above adds the action listener, it immediately goes and prints "usertype" - but you haven't clicked the button yet, and therefore usertype has not been changed yet, so it prints 0!
Basically, I don't think you don't have a problem, you just think you have a problem. If you *really* want to verify that "usertype" is being set correctly, just do a System.out.println(getUsertype()); inside of the actionPerformed method at some point after you set the usertype.
Last edited by BestJewSinceJC; Oct 22nd, 2009 at 5:55 pm.
Out.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
0
#10 Oct 22nd, 2009
Usage of LogUI and Login:
start both main(..)
//////////////////////
Other expected behavior requires changes in the code
Java Syntax (Toggle Plain Text)
/** * * @author j3c */ public class Starter extends JFrame { public Starter() { final LogUI logUI = new LogUI(); JPanel panel = new JPanel(); final JLabel label = new JLabel("usertype"); JButton button = new JButton("get login"); panel.add(label); panel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText("" + logUI.getUsertype()); } }); add(panel); setTitle("Starter"); setDefaultCloseOperation(3); pack(); setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new Starter(); } }
Java Syntax (Toggle Plain Text)
package kekkaishi; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; class LogUI extends JFrame { private JTextField user = new JTextField(10); private JPasswordField pass = new JPasswordField(10); private JPanel panel = new JPanel(); private JButton button = new JButton("Login"); private int usertype; public LogUI() { panel.add(user); panel.add(pass); panel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Login lg = new Login(user.getText(), pass.getText());// getText -->deprecated Login lg1 = new Login(user.getText(), pass.getPassword()); usertype = lg1.getUsertype(); int i = getUsertype(); System.out.println("LogUI i=" + i); } }); this.add(panel); this.setTitle("Login"); this.pack(); this.setDefaultCloseOperation(3); this.setVisible(true); } public int getUsertype() { System.out.println("LogUI getUsertype()" + usertype); return usertype; } public static void main(String[] args) { LogUI lo = new LogUI(); System.out.println(lo.getUsertype()); System.out.println("end of main(...)"); ///} // but while (true) { try { Thread.sleep(1000); } catch (InterruptedException ex) { // } System.out.println(lo.getUsertype()); } } }
Java Syntax (Toggle Plain Text)
package kekkaishi; //import java.sql.*; //import javax.swing.JOptionPane; public class Login { private int usertype; //private ResultSet rs; //private Statement st; public Login(String text, char[] password) { ///..............simplified usertype = text.length();// to view result dependent of human action } public int getUsertype() { System.out.println("Login getUsertype()" + usertype); return this.usertype; } }
//////////////////////
Other expected behavior requires changes in the code
Last edited by quuba; Oct 22nd, 2009 at 6:16 pm. Reason: added last line
![]() |
Other Threads in the Java Forum
- Previous Thread: Help with homework
- Next Thread: How to Generate Unique Random Numbers?
| Thread Tools | Search this Thread |
.net 6 actuate addball ajax apache apple applet array asp automation beginner binary binarytree block busy_handler(null) c++ c/c++ class classes code component constructor convert design detection development dragging dynamic eclipse error event firefox forms fractal functiontesting game google gui homework html ide image infinite innodb input insight integer intellij java javafx javascript jetbrains jsp julia linux login loop looping loops mergers method microsoft mysql netbeans newbie nextline numbers opensource oriented pearl php pong problem programming project python radio random set software sort sortedmaps sourcelabs spring staticcodeanalysis storm string subclass sun swing text-file threads time tomcat tree variablebinding web websphere windows







