I’m a fresh learner of OOP approach in java and I have some problems with my system.
I’m currently doing a simple GUI system. In a jForm, supposedly I have to write all my codes there as a procedural approach. But I want to try it to code in a separate class (OOP). And all I have to do in the main form is to invoke the methods I created on the other class.
But my main problem is how am I going to call or invoke an object from a form to another class.
Here’s an example…
public void checkUser(){
try {
sql = "SELECT accessLevel FROM user_accounts WHERE userName = 'admin'";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
rs.first();
int accessLvl = rs.getInt(1);
if (accessLvl != 1){
btnAddUser.setEnabled(false);
jPanel3.setEnabled(false);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Exception on Verifying User");
}
}
I want this method (checkUser()) to be written in another class instead of putting it inside the main form. But my problem is I cannot access the object btnAddUser (button name) and JPanel13 (panel name) which were initiated in the main form. And if I will directly use How can I possibly access those objects so that I access them to other classes.
You can write this method as a static into the another class, like below which accepts two parameters one if JButton and other is JPanel
}
and you can call this method from where you written the code before like
NewClass is the class in which you have written this checkUser() method.
you also need to pass parameters to method like sql string, Statement and ResultSet objects, or declare them as static so you can easily access them anywhere using class name.