0

I ma new in OOD and working on Academic project. I am facing a problem while programming it.

The scenario is that I have a main class in which I have made an object of "login" class and call its function.

Now in that "login" class function I create an object of jFrame class to show a login view, where user enters the login info and press login button.

Now I want that when user press login button I should pass that info to an authenticate function in my "login" class.

The problem is that how can I call that function (its a non-static function), and upon invalid info call a function of jFrame class to show error message.

And I want to do this function calling all over in my project.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • 1
    You don't "pass messages" in Java. ;) – Madbreaks Dec 08 '12 at 18:24
  • I am new in java so I wrote message passing / function calling ... edited it –  Dec 08 '12 at 18:26
  • Read [the swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/). Really. – JB Nizet Dec 08 '12 at 18:28
  • I guess it has nothing to do with swing library . its a OOP concept .. I am just using the library to view graphically –  Dec 08 '12 at 18:30
  • If you want to use swing without learning it and understanding how it works, and the fundamental OO concepts behind it, you're doomed to fail miserably. – JB Nizet Dec 08 '12 at 18:46
  • lol .. I asked a OOP concept ... why are stressing upon learning swing library ?? .. what if I am doing OOP in web ? –  Dec 08 '12 at 18:53
  • Actually @Madbreaks even though you would not call it "passing messages" when you are talking about a Java program, that is essentially what you are doing. When discussing OO concepts the term passing a message from one object to another translates to calling a method in Java. Someone new to OOP will be thinking in terms of passing messages because that is what they would be taught in class. Later, in Java they will correct the terminology. – Vincent Ramdhanie Dec 08 '12 at 19:01

2 Answers2

2

Maybe you need to redesign your program a bit. The login class should not be creating a JFrame. Let the login class concentrate on authenticating users and its related functions. So something like this:

  public class Login{

      public boolean authenticate(String uname, String pword){
           return .......
      }

  }

Whenever you need to use the functions of Login, you can instantiate it and call the function. You can create a JFrame for instance that prompts the user for username and password and in the action of the button:

         loginBtn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){

                      Login login = new Login();
                      if(login.authenticate(txtUname.getText(), txtPWord.getText())){
                              //display success on JFrame
                      }else{
                              //display failure on JFrame
                      }
                }
          });

Where txtUname and txtPWord may be 2 JTextFields on you JFrame, and lginBtn may be a button on your JFrame.

If you need to perform the same function somewhere else then you instantiate Login again and call the function.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • thanks for that help .. another question though ... now what if I want to call a showerror message of view in authenticate function should I create another object of that class ?? and in success should I create the object of new view ?? and then how can I hide the previous view , because now I don't have its object .. –  Dec 08 '12 at 18:50
  • Since you are trying to learn OOP then you have to understand that each class should be responsible for one thing only. You should not be showing anything to the user in the Login class' authenticate function. Rather just focus on authenticating the user. All the show error logic should go in the various view classes such as your JFrame. – Vincent Ramdhanie Dec 08 '12 at 18:58
  • so can you propose a solution of the given scenario .. what should I do if I have to show an error or on success hide the current view and create a new one ? –  Dec 08 '12 at 19:01
  • That happens inside the actionListener of the button in the code that I posted. You can hide the JFrame or create a new JFrame or whatever you like to do within that if statement. This way, if you reuse your Login class somewhere else then you can decide to react differently to a failed authentication. – Vincent Ramdhanie Dec 08 '12 at 19:03
  • But i cant do any logic like if/else in that JFrame, thats what business layer is for ??? isn't it ? –  Dec 08 '12 at 19:08
0

Here is a nice & working example of How-to-create-a-login-form-in-javax.swing

A swing tutorial link for Java Beginner Swing

vels4j
  • 11,208
  • 5
  • 38
  • 63
  • LOL, that not what I asked .. I asked that how can I call a function of a class form a jFrame function which has no relation with it ? –  Dec 08 '12 at 18:46
  • nothing to lough. i have given some links. answer is already given here. If you are getting any error, you must post your code. – vels4j Dec 08 '12 at 18:52
  • I am not getting errors and the link you have provided is not relevent –  Dec 08 '12 at 18:57