0

I'm trying to get that name string from LoginForm to MainForm so I may now the user who logged in with their name. My problem is that, I cant pass the retrieved name from database in LoginForm class to MainForm.

Why is that if I perform this code;

public class LoginForm extends JFrame{
    ...
    .
    public LoginForm(){
       ...
       .
       MainForm thisoh = new MainForm();
       thisoh.getthisnow(rs.getString("Given_Name"));
       ...
       .
    }
}

and then, in the other class;

public class MainForm extends JFrame {
   private String fname;
       String getthisnow(String usrname) {
          this.fname = usrname;
          return fname;
          }
       public MainForm() {
          initComponents();
          txt_HomeWelcome.setText(fname);//JLabel
          ...
          .
       }
}

The JLabel become null.. :-( It suppose to show the name from the LoginForm, right? Did I miss something?? Please help! >.< I need that fname from LoginForm. That's my problem.

DjayPediA
  • 17
  • 1
  • 9
  • What's the question? – Dragan Bozanovic Aug 18 '15 at 14:06
  • Does: rs.getString("Given_Name"); give an error or something? Or what do you mean? – Gilian Joosen Aug 18 '15 at 14:09
  • If you're trying to set the label to be `Jlabel.setText(user)` on the new `MainForm()`, shouldn't you assign a variable to the `MainForm`? `myForm = new MainForm()`, myForm.setVisible(true), then `myForm.setText(user)`? – matrixanomaly Aug 18 '15 at 14:10
  • If "Given_Name" is column name, yes you can. And actually that is how you read a resultSet object. [Here is](http://stackoverflow.com/a/187425/381897) a more detailed answer. But, if you try to filter by user name using result set, I do not think it is possible. Also please be more clear about what you are trying to achieve and what is the error your are getting. – bhdrkn Aug 18 '15 at 14:13
  • When you say "System" do you mean like Windows? Or your own solution? – CarefreeCrayon Aug 18 '15 at 14:15
  • @matrixanomaly no errors at all... its a String user declared. but inside the LoginForm, the "user" is called by JOption to message window to welcome the user and specify its name, and that is "Given_Name" from database... – DjayPediA Aug 18 '15 at 14:16
  • @bhdrkn ok thank you, but i'll seek my own with you guys so I may develop my skills in programming.. I'm an student studying this kind of program. – DjayPediA Aug 18 '15 at 14:18
  • @CarefreeCrayon Oops! my bad, I'm sorry... the "System" I am talking about is the MainForm... I'm sorry for my terms. LoLz – DjayPediA Aug 18 '15 at 14:19
  • @DraganBozanovic I'm making a LoginForm that whoever logged in the MainForm will be recognize by itself(MainForm). is that even possible? – DjayPediA Aug 18 '15 at 14:21
  • You already have everything you need. The result of the SQL query has the user name, just pass it to the main form. new MainForm(rs.getString("Given_name")).setVisible(true); – CarefreeCrayon Aug 18 '15 at 14:23
  • @CarefreeCrayon okay? and then I got this MainForm(String string) { } – DjayPediA Aug 18 '15 at 14:31
  • see my answer for more details – CarefreeCrayon Aug 18 '15 at 14:40
  • sir @CarefreeCrayon I got an error! it says "java.lang.NullPointerException" – DjayPediA Aug 18 '15 at 14:41

1 Answers1

0

What you need to do here is pass your username to your Main form and set the value to a label somewhere.

new MainForm(rs.getString("Given_name")).setVisible(true);

then in MainForm

public class MainForm extends JFrame {
    private String username;
    public MainForm(String username){
        this.username = username;

        //you set the value of your label to username
        // here or somewhere else in your form
         mLabel.setText(username);
    }
}
CarefreeCrayon
  • 249
  • 2
  • 7