Java value assigned from within ActionPerformed not maintained

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 35
Reputation: kekkaishi is an unknown quantity at this point 
Solved Threads: 5
kekkaishi kekkaishi is offline Offline
Light Poster

Java value assigned from within ActionPerformed not maintained

 
0
  #1
Oct 21st, 2009
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
  1. public class Log{
  2. private JTextField user = new JTextField(10);
  3. private JPasswordField pass = new JPasswordField(10);
  4. private int usertype=0;
  5. private JPanel panel = new JPanel();
  6.  
  7. public Log(){
  8.  
  9. panel.add(user);
  10. panel.add(pass);
  11. panel.add(button);
  12.  
  13. button.addActionListener(new ActionListener() {
  14.  
  15. public void actionPerformed(ActionEvent e) {
  16. Login logs = new Login(user.getText(), pass.getText());
  17. //this one is querying the database and getting an int value for user type.
  18. usertype = logs.getUsertype();
  19. }
  20. });
  21. //some more codes to add to frame and stuff
  22.  
  23. }
  24. public int getUserType(){
  25. return usertype;
  26. }
  27. }
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
Last edited by kekkaishi; Oct 21st, 2009 at 8:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
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.
Last edited by BestJewSinceJC; Oct 21st, 2009 at 9:26 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 35
Reputation: kekkaishi is an unknown quantity at this point 
Solved Threads: 5
kekkaishi kekkaishi is offline Offline
Light Poster
 
0
  #3
Oct 22nd, 2009
Originally Posted by BestJewSinceJC View Post
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.
thanks mate,
nope, Log is not an extension of Login class, in fact, Log actually extends JInternalFrame, here is the two classes.

log class
  1. public class LogUI extends JInternalFrame{
  2. private JTextField user = new JTextField(10);
  3. private JPasswordField pass = new JPasswordField(10);
  4. private JPanel panel = new JPanel();
  5. private JButton button = new JButton("Login");
  6. private int usertype;
  7.  
  8. public LogUI(){
  9. panel.add(user);
  10. panel.add(pass);
  11. panel.add(button);
  12. button.addActionListener(new ActionListener() {
  13. public void actionPerformed(ActionEvent e) {
  14. Login lg = new Login(user.getText(), pass.getText());
  15. usertype = lg.getUsertype();
  16. }
  17. });
  18. this.add(panel);
  19. this.setTitle("Login");
  20. this.pack();
  21. this.setVisible(true);
  22. }
  23. public int getUsertype() {
  24. return usertype;
  25. }
and the Login class
  1. public class Login {
  2. private int usertype;
  3. private ResultSet rs;
  4. private Statement st;
  5. public Login(String user, String pass){
  6. try{
  7. SgmDb db = new SgmDb();
  8. st = db.connectToDB().createStatement();
  9. String query = "SELECT username, password, userType FROM user WHERE username='"+user+"' AND password = '"+pass+"'";
  10. st.executeQuery(query);
  11. rs = st.getResultSet();
  12. while(rs.next()){
  13. usertype = Integer.parseInt(rs.getString("userType"));
  14. }
  15. rs.last();
  16. if(rs.getRow()==0){
  17. JOptionPane.showMessageDialog(null, "Wrong username or password");
  18. }
  19. }catch(Exception e){
  20. JOptionPane.showMessageDialog(null, "Contact Admin, there is a problem");
  21.  
  22. }
  23. }
  24. public int getUsertype(){
  25. return this.usertype;
  26. }
  27. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
Oct 22nd, 2009
check s:
  1. String s = rs.getString("userType");
  2. System.out.println("|"+s+"|");
  3. usertype = Integer.parseInt(s);
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 35
Reputation: kekkaishi is an unknown quantity at this point 
Solved Threads: 5
kekkaishi kekkaishi is offline Offline
Light Poster
 
0
  #5
Oct 22nd, 2009
Originally Posted by quuba View Post
check s:
  1. String s = rs.getString("userType");
  2. System.out.println("|"+s+"|");
  3. usertype = Integer.parseInt(s);
hey thanks for ya reply. But I've already done this. In the Login class. see line 13.
  1.  
  2. usertype = Integer.parseInt(rs.getString("userType"));
and when i call to this value from Log class, see line 15 in class Log, I get the value. and if i print out the usertype within the actionPerformed method, the value is there. But if I call it outside this method, the value is not there. meaning I cannot retrieve the usertype value in the Log class.

say for example the following code.
  1. Log logObject = new Log();
  2. int x = logObject.getUserType();
  3.  
  4. // int x is always 0. this is my problem.
Last edited by kekkaishi; Oct 22nd, 2009 at 11:06 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
Clawsy Clawsy is offline Offline
Junior Poster in Training
 
0
  #6
Oct 22nd, 2009
Hmm... try defining that 'ActionListener' outside the constructor.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #7
Oct 22nd, 2009
Originally Posted by Clawsy View Post
Hmm... try defining that 'ActionListener' outside the constructor.
Wrong. Run my code if you don't believe me.

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.WindowEvent;
  4. import java.awt.event.WindowListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. public class RandomTest extends JPanel implements WindowListener{
  11.  
  12. int random = 0;
  13. public RandomTest(){
  14. JButton button = new JButton("Click me");
  15.  
  16. button.addActionListener(new ActionListener(){
  17.  
  18. @Override
  19. public void actionPerformed(ActionEvent e) {
  20. // TODO Auto-generated method stub
  21. random = 5;
  22. System.out.println("Variable random is " + random);
  23. }
  24. });
  25. this.add(button);
  26. }
  27.  
  28. public static void main(String[] args){
  29. JFrame frame = new JFrame();
  30. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  31. RandomTest test = new RandomTest();
  32. frame.add(test);
  33. frame.addWindowListener(test);
  34. frame.setSize(300,300);
  35. frame.setVisible(true);
  36. }
  37.  
  38. @Override
  39. public void windowActivated(WindowEvent e) {
  40. // TODO Auto-generated method stub
  41.  
  42. }
  43.  
  44. @Override
  45. public void windowClosed(WindowEvent e) {
  46. // TODO Auto-generated method stub
  47. System.out.println("Window is closing. Variable random = " + random);
  48. }
  49.  
  50. @Override
  51. public void windowClosing(WindowEvent e) {
  52. // TODO Auto-generated method stub
  53.  
  54. }
  55.  
  56. @Override
  57. public void windowDeactivated(WindowEvent e) {
  58. // TODO Auto-generated method stub
  59.  
  60. }
  61.  
  62. @Override
  63. public void windowDeiconified(WindowEvent e) {
  64. // TODO Auto-generated method stub
  65.  
  66. }
  67.  
  68. @Override
  69. public void windowIconified(WindowEvent e) {
  70. // TODO Auto-generated method stub
  71.  
  72. }
  73.  
  74. @Override
  75. public void windowOpened(WindowEvent e) {
  76. // TODO Auto-generated method stub
  77.  
  78. }
  79.  
  80.  
  81. }
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 35
Reputation: kekkaishi is an unknown quantity at this point 
Solved Threads: 5
kekkaishi kekkaishi is offline Offline
Light Poster
 
0
  #8
Oct 22nd, 2009
Originally Posted by BestJewSinceJC View Post
Wrong. Run my code if you don't believe me.

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.WindowEvent;
  4. import java.awt.event.WindowListener;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. public class RandomTest extends JPanel implements WindowListener{
  11.  
  12. int random = 0;
  13. public RandomTest(){
  14. JButton button = new JButton("Click me");
  15.  
  16. button.addActionListener(new ActionListener(){
  17.  
  18. @Override
  19. public void actionPerformed(ActionEvent e) {
  20. // TODO Auto-generated method stub
  21. random = 5;
  22. System.out.println("Variable random is " + random);
  23. }
  24. });
  25. this.add(button);
  26. }
  27.  
  28. public static void main(String[] args){
  29. JFrame frame = new JFrame();
  30. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  31. RandomTest test = new RandomTest();
  32. frame.add(test);
  33. frame.addWindowListener(test);
  34. frame.setSize(300,300);
  35. frame.setVisible(true);
  36. }
  37.  
  38. @Override
  39. public void windowActivated(WindowEvent e) {
  40. // TODO Auto-generated method stub
  41.  
  42. }
  43.  
  44. @Override
  45. public void windowClosed(WindowEvent e) {
  46. // TODO Auto-generated method stub
  47. System.out.println("Window is closing. Variable random = " + random);
  48. }
  49.  
  50. @Override
  51. public void windowClosing(WindowEvent e) {
  52. // TODO Auto-generated method stub
  53.  
  54. }
  55.  
  56. @Override
  57. public void windowDeactivated(WindowEvent e) {
  58. // TODO Auto-generated method stub
  59.  
  60. }
  61.  
  62. @Override
  63. public void windowDeiconified(WindowEvent e) {
  64. // TODO Auto-generated method stub
  65.  
  66. }
  67.  
  68. @Override
  69. public void windowIconified(WindowEvent e) {
  70. // TODO Auto-generated method stub
  71.  
  72. }
  73.  
  74. @Override
  75. public void windowOpened(WindowEvent e) {
  76. // TODO Auto-generated method stub
  77.  
  78. }
  79.  
  80.  
  81. }
oi mate, thanks for the reply. i see that the value is maintained in ur code, but im still having trouble figuring out what I have done wrong. could you point me in a bit more like "idiots guide to.." style thannks
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #9
Oct 22nd, 2009
Ok.

  1. usertype = logs.getUsertype();
  2. 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:

  1. button.addActionListener(new ActionListener() {
  2.  
  3. public void actionPerformed(ActionEvent e) {
  4. Login logs = new Login(user.getText(), pass.getText());
  5. //this one is querying the database and getting an int value for user type.
  6. usertype = logs.getUsertype();
  7. }
  8. });
  9. //some more codes to add to frame and stuff
  10.  
  11. System.out.println(usertype); //WILL ALWAYS PRINT 0
  12.  
  13. }

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #10
Oct 22nd, 2009
Usage of LogUI and Login:
  1. /**
  2.  *
  3.  * @author j3c
  4.  */
  5. public class Starter extends JFrame {
  6.  
  7. public Starter() {
  8.  
  9. final LogUI logUI = new LogUI();
  10.  
  11. JPanel panel = new JPanel();
  12. final JLabel label = new JLabel("usertype");
  13. JButton button = new JButton("get login");
  14. panel.add(label);
  15. panel.add(button);
  16. button.addActionListener(new ActionListener() {
  17.  
  18. public void actionPerformed(ActionEvent e) {
  19. label.setText("" + logUI.getUsertype());
  20. }
  21. });
  22. add(panel);
  23. setTitle("Starter");
  24. setDefaultCloseOperation(3);
  25. pack();
  26. setLocationRelativeTo(null);
  27. this.setVisible(true);
  28. }
  29.  
  30. public static void main(String[] args) {
  31. new Starter();
  32. }
  33. }
  1. package kekkaishi;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import javax.swing.*;
  8.  
  9. class LogUI extends JFrame {
  10.  
  11. private JTextField user = new JTextField(10);
  12. private JPasswordField pass = new JPasswordField(10);
  13. private JPanel panel = new JPanel();
  14. private JButton button = new JButton("Login");
  15. private int usertype;
  16.  
  17. public LogUI() {
  18. panel.add(user);
  19. panel.add(pass);
  20. panel.add(button);
  21. button.addActionListener(new ActionListener() {
  22.  
  23. public void actionPerformed(ActionEvent e) {
  24. //Login lg = new Login(user.getText(), pass.getText());// getText -->deprecated
  25. Login lg1 = new Login(user.getText(), pass.getPassword());
  26. usertype = lg1.getUsertype();
  27. int i = getUsertype();
  28. System.out.println("LogUI i=" + i);
  29. }
  30. });
  31. this.add(panel);
  32. this.setTitle("Login");
  33. this.pack();
  34. this.setDefaultCloseOperation(3);
  35. this.setVisible(true);
  36. }
  37.  
  38. public int getUsertype() {
  39. System.out.println("LogUI getUsertype()" + usertype);
  40. return usertype;
  41.  
  42. }
  43.  
  44. public static void main(String[] args) {
  45. LogUI lo = new LogUI();
  46. System.out.println(lo.getUsertype());
  47. System.out.println("end of main(...)");
  48. ///}
  49. // but
  50. while (true) {
  51. try {
  52. Thread.sleep(1000);
  53. } catch (InterruptedException ex) {
  54. //
  55. }
  56.  
  57. System.out.println(lo.getUsertype());
  58. }
  59. }
  60. }
  1. package kekkaishi;
  2.  
  3. //import java.sql.*;
  4. //import javax.swing.JOptionPane;
  5. public class Login {
  6.  
  7. private int usertype;
  8. //private ResultSet rs;
  9. //private Statement st;
  10.  
  11. public Login(String text, char[] password) {
  12. ///..............simplified
  13. usertype = text.length();// to view result dependent of human action
  14. }
  15.  
  16. public int getUsertype() {
  17. System.out.println("Login getUsertype()" + usertype);
  18. return this.usertype;
  19. }
  20. }
start both main(..)
//////////////////////
Other expected behavior requires changes in the code
Last edited by quuba; Oct 22nd, 2009 at 6:16 pm. Reason: added last line
Reply With Quote Quick reply to this message  
Reply

Tags
java

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC