Reputation: 31
I couldn't find any straightforward answer to my question, hopefully you guys can!
I've created a class called 'aclass' within 'aClass' is an action handler containing a bunch of if statements that check the entered value against a database.
What I wanted to do was hide the JFrame 'theFrame' that is created in 'aClass' by using theFrame.setVIsible(false) in one of the if statements, however it won't let me, 'theFrame' appears to be out of scope, and my IDE won't let me.
Current version of code (See 'THIS WILL NOT WORK' below):
public class aClass{
static JTextField USER_NAME;
static JPasswordField PASSWORD;
static JButton submit;
private static class Handler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String USER_NAME_I = USER_NAME.getText();
String PASSWORD_I = PASSWORD.getText();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(aClass.class.getName()).log(Level.SEVERE, null, ex);
}
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fake",
/*DB USER_NAME*/ "fakeuname",
/*DB password*/ "fakepassword");
Statement st = con.createStatement();
String FindQuery = "SELECT acc_pass FROM users where acc_name='"+USER_NAME_I+"'";
//System.out.println(FindQuery);
ResultSet rs = st.executeQuery(FindQuery);
if(rs.next())
{
String PASSWORD_DB = rs.getString(1);
if(PASSWORD_DB.equals(PASSWORD_I))
{
//THIS WILL NOT WORK
loginFrame.setVisible(false);
//do something
}
else
{
JOptionPane.showMessageDialog(null,
"Wrong Password",
"Error",1);
}//end else
}//end if (rs.next())
else
{
JOptionPane.showMessageDialog(null,
"Wrong Username",
"Error",1);
}//end else
} catch (SQLException ex)
{
Logger.getLogger(aClass.class.getName()).log(Level.SEVERE, null, ex);
}//end catch (SQLException ex)
}//end ActionEvent
}//end ActionListener
aClass(){
JFrame loginFrame = new JFrame("Logon");
loginFrame.setVisible(true);
loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
USER_NAME = new JTextField(10);
PASSWORD = new JPasswordField(10);
loginFrame.setLayout(new GridLayout(0,1));
JPanel loginPanel = new JPanel();
loginFrame.add(loginPanel);
loginPanel.add(new JLabel("User Name: "));
loginPanel.add(USER_NAME);
loginPanel.add(new JLabel("Password: "));
loginPanel.add(PASSWORD);
submit = new JButton("Submit");
loginPanel.add(submit);
submit.addActionListener(new Handler());
}//end aClass
public static void main(String args[])
{
new aClass();
}//end main
}//end class
EDIT: My knowledge on the subject is limited, the code above is an edited version of an online tutorial I found. I'll fiddle some and see if I can get it to worki with JOptionPane and edit again if i need any help!
Upvotes: 0
Views: 486
Reputation: 352
The method actionPerformed() does not know about existence of loginFrame, because the scope of loginFrame is local to the constructor. So, either move the declaration of loginFrame out of constructor (i.e. as a member variable like submit JButton) or pass it to Handler. And one way to pass is to Handle is to have a constructor in Handler class that takes a JFrame.
Upvotes: 1
Reputation: 5038
How can you use loginFrame
variable from aClass()
into actionPerformed
??
and yeah i'd suggest the same thing, don't use frame here, go for JDialog
or JOptionPane
Upvotes: 1