开发者

My program's Action Listener won't work

开发者 https://www.devze.com 2023-03-19 16:38 出处:网络
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?

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 actionPe开发者_JS百科rformed(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


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);
    }
} ...


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号