My program's Action Listener won't work?

PasswordDemo as shown in How to Use Password Fields would be a good starting point for your study, and it would make an effective sscce .

Up vote -2 down vote favorite share g+ share fb share tw.

It's me again and I just can't seem to get this code to work. I'm basically asking for any advice on why the button does nothing when clicked. Would you like me to attach the source code?

The method I'm trying to implement: public static void UserInput() { try { stmt = connect.createStatement(); ResultSet res = stmt. ExecuteQuery("SELECT * FROM " + tableName); while (res.next()) { if (res. GetString("Username").

Equals(usernameField.getText())) { if (res. GetString("Password"). Equals(passwordField.getPassword())) { JOptionPane.

ShowMessageDialog(null, "Correct", "Correct", JOptionPane. INFORMATION_MESSAGE); } else { JOptionPane. ShowMessageDialog(null, "Error.

Incorrect " + "username or password. ", "Error", JOptionPane. ERROR_MESSAGE); } } else { JOptionPane.

ShowMessageDialog(null, "Error. Incorrect " + "username or password. ", "Error", JOptionPane.

ERROR_MESSAGE); } } res.close(); stmt.close(); connect.close(); } catch (SQLException sqlExcept) { sqlExcept.printStackTrace(); } } And here's how I'm calling it: if(firstTime == false) { JavaDB jdb = new JavaDB(); } JavaDB window = new JavaDB(""); window. AddWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System. Exit(0); } }); } And here's the actionListner: submit = new JButton("Submit"); c.

Add(submit); submit. AddActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { if(e.getSource(). Equals(submit)) { UserInput(); } } }); ; If you need anymore let me know.

I've been teaching myself Java and I don't really know what to learn so any tips will be welcomed. I'm also new to stack overflow and posting code so any thing you can give me will be more than appreciated. Thanks in advance.

Edit: I now added a class for event handling with the Thread inside of it like this; public class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource(). Equals(submit)){ Thread th = new Thread(new JavaDB()); th.start(); th.run(); try { th.wait(); } catch (InterruptedException e1) { } } else{ System. Exit(0); } } And I changed UserInput to run().

However,now when I click the submit button,The GUI disappears. Just for a reference you might need, here's my main method: public static void main(String args) throws SQLException, InterruptedException { createConnection(); boolean firstTime = firstTime(); if (firstTime) { JavaDB db = new JavaDB(""); db.createAccount(); try { connect = DriverManager . GetConnection("jdbc:derby:\\KeithDB;shutdown=true"); } catch (SQLException XJ015) { } } if (firstTime == false) { JavaDB jdb = new JavaDB(); Thread th = new Thread(); } JavaDB window = new JavaDB(""); window.

AddWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System. Exit(0); } }); } Anything else you need,let me know java database swing actionlistener link|improve this question edited Jul 14 '11 at 23:50 asked Jul 13 '11 at 3:25Keith Mattix536.

5 I don't see a bit of ActionListener code in the code blocks that you've posted. Why is it titled thusly when it doesn't involve use of ActionListeners? Also, what specifically is your problem and your question?

All you're posting is a vague desire and some unrelated code. I think that we'll likely need more if we're to have a chance of helping you. – Hovercraft Full Of Eels Jul 13 '11 at 3:34 1 What exactly are you asking?

– Logan Jul 13 '11 at 9:33 OK, now we see some ActionListener code, so we're getting somewhere. I think that at this point you need to do some debugging including placing System.out. Println("at position 002 in code and foo variable is: " + foo); statements in your program to check if a method is being called that you think should be called and the state of its variables at that point in time.

– Hovercraft Full Of Eels Jul 13 '11 at 21:30 I added the System. Out clause as well as a Thread in the caller. I use Thread.

Start,Thread. Wait,and Thread.stop. Before the Thread,the System.

Out didn't show up. After it,it did,as well as an Illegal monitor exception. I placed the System.

Out in the UserInput by the way. However,I know have a runtime error. Every username and password I put in, even if it's in the database,shows the error message.

I can try to send you the source code so you can see for yourself. Anything else you need me to do? – Keith Mattix Jul 13 '11 at 0:33.

PasswordDemo, as shown in How to Use Password Fields, would be a good starting point for your study, and it would make an effective sscce. Addendum: Absent a complete example or knowledge of what database you are using, I got the following result, Version: H2 1.3.157 (2011-06-25) 1.3 by running the following modification to PasswordDemo against H2 Database: if (isPasswordCorrect(input)) { try { Connection conn = DriverManager. GetConnection( "jdbc:h2:mem:", "sa", "secret"); DatabaseMetaData metaData = conn.getMetaData(); System.out.

Println("Version:" + " " + metaData. GetDatabaseProductName() + " " + metaData. GetDatabaseProductVersion() + " " + metaData.

GetDatabaseMajorVersion() + ". " + metaData. GetDatabaseMinorVersion()); } catch (SQLException ex) { ex.

PrintStackTrace(System. Err); } } ... Addendum: I got the following result, Version: Apache Derby 10.6.2.1 - (999685) 10.6 by running the following modification to PasswordDemo against Apache Derby: if (isPasswordCorrect(input)) { try { EmbeddedDataSource ds = new EmbeddedDataSource(); ds. SetDatabaseName("/home/trashgod/.

Netbeans-derby/dbtest"); Connection conn = ds. GetConnection("sa", "secret"); DatabaseMetaData metaData = conn.getMetaData(); System.out. Println("Version:" + " " + metaData.

GetDatabaseProductName() + " " + metaData. GetDatabaseProductVersion() + " " + metaData. GetDatabaseMajorVersion() + ".

" + metaData. GetDatabaseMinorVersion()); } catch (SQLException ex) { ex. PrintStackTrace(System.

Err); } } ...

Scratch that. I had to take away all threads because,after some changes,I got an error that said(Result Set is not open. Please make sure autocommit is OFF).

Please help – Keith Mattix Jul 14 '11 at 3:23 @Keith: I've elaborated above. – trashgod Jul 14 '11 at 4:28 I don't get what you're doing. I'm using Apache derby for my database.

Can I pm you the source code? – Keith Mattix Jul 14 '11 at 14:19 I've updated the examples above to show comparable queries for both H2 and Derby. Now that you have a complete, working example, you may be able to determine where you were going wrong.

– trashgod Jul 14 '11 at 17:13 1 Sorry, this is not a free debugging service for people of any age. If you can't find a class at your local school, the tutorial is a good place to start. – trashgod Jul 14 '11 at 17:35.

I finally got it to work! I had another constructor with the same variable names and my call to the JTextFields were mistaken to be the call to the other constructor. The foo statements really helped!

Thank you everyone!

Thank you for sharing your result; I'm sure I've made a similar mistake. I'm up-voting your answer as useful, for which you will gain 10 point of reputation. Of course, accepting your own answer garners no points; but if you were to accept my answer instead, you would get an additional two points.

See the faq for more details. – trashgod Jul 16 '11 at 1:52 1 Thank you for the info and thank you again for all your help – Keith Mattix Jul 17 '11 at 21:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions