0

So, i'm building a login form in java with swing. and i did this test when login button is clicked

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String username = jTextField1.getText();
    String password = jTextField2.getText();
    if (username.equals("Admin")&&password.equals("Admin")) {
        //Launch admin panel
        AdminGui admin = new AdminGui();
        admin.setVisible(true);
    }
    else {
        //Some code
    }
} 

As you see if the username = Admin and the passowrd is also Admin AdminGui will launch, but i want this test to be performed at another class. How could i send the textfield values from this class to another ?

aaa
  • 57
  • 4
  • 11
  • 1
    It's not the responsibility of your login view to make decisions about what should occur once the the use is validated (in fact the actual validation should be handled by a separate controller). Instead, once the user clicks okay, and validation has occurred, you should raise and event/notification to tell interested parties that validation has occurred (sand possibly pass the user/session information to them) – MadProgrammer Jan 30 '15 at 22:35
  • 1
    Please provide more detail as to your problem -- where are you creating this "other class" instance? What is its function as opposed to this class? What problems are you having with your current attempt to pass information from one class to another. – Hovercraft Full Of Eels Jan 30 '15 at 22:35
  • For a detailed overview, you could have a look at [this example](http://stackoverflow.com/questions/26517856/java-swing-where-do-actionlisteners-belong-according-to-mvc-pattern/26518274#26518274) – MadProgrammer Jan 30 '15 at 22:37
  • Think about things like "how could I reuse this component?" and "how can I make it easy to change?" You want to separate the responsibility of collecting the user information from the validation of that information and the navigation decisions, all these are separate responsibilities which should be handled by different parts of the code. Focus on providing pluggable solitons to these problems through the use of interfaces, so the physical implementation is irrelevant. – MadProgrammer Jan 30 '15 at 22:47

3 Answers3

0

First option is to send it using the constructor like:

AnotherClass ac = new AnotherClass (yourInput);

or:

AnotherClass ac = new AnotherClass (yourInput);
ac.setYourInput(yourInput);
roeygol
  • 4,908
  • 9
  • 51
  • 88
0

Create another class, called maybe UserService, e.g.

public class UserService {
    public boolean isValidLogin(String username, String password) {
        return username.equals("Admin") && password.equals("Admin");
    }
}

Create an instance UserService in your class which has jButton1ActionPerformed method. e.g Note if you have another class that creates the view you should really pass this on instead with a getter/setter method.

private UserService userService = new UserService(); 

Then call in your jButton1ActionPerformed() method

if (userService.isValidLogin(username, password)) {
Adam
  • 35,919
  • 9
  • 100
  • 137
  • Nitpick: the view shouldn't be creating this, it should be part of the controller pass to the view so it can call it – MadProgrammer Jan 30 '15 at 22:42
  • I agree, but the questioner is struggling with basic concepts so didn't want to overwhelm them with architecture – Adam Jan 30 '15 at 22:43
  • That's fair, but if you're going to teach some to shoot gun, it's not enough just to show them where the trigger is, you should teach them proper gun handling and safety and where to point it first - but that's just me ;) – MadProgrammer Jan 30 '15 at 23:11
  • Again agreed, but original problem.is likely a 1000 line netbeans monster view class including entry point, so there wouldn't be anywhere to put it anyway, certainly not a builder, controller etc. – Adam Jan 30 '15 at 23:19
0

You could make a Utility-class which you should do, because of the MVC-pattern:

public static class YourUtilityClass{
  public static boolean checkLogin(String username, String password){
    if(username.equals("Admin") && password.equals("Admin")){
      return true;
    }else{
      return false;
    }
}

And in your GUI in your actionPerformer-method you have to do something like that:

boolean loginCorrect = YourUtilityClass.checkLogin(username,password);

if(loginCorrect){
  //login was okay and you can load the AdminGui
}else{
  //notCorrect
}
Christian
  • 22,585
  • 9
  • 80
  • 106