-1

I have a jFrame form called "gui.java" and basically im passing it to user class in user package like so:

 
import user.User;

public class gui extends javax.swing.JFrame {

    public gui() {
        initComponents();
    }                                                                      

    private void userFormRegisterBtnActionPerformed(java.awt.event.ActionEvent evt) {   
        // passed "this" to user class in another package but cannot access it's components
        User.registerUser(this);
    }                                                   

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                gui a = new gui();
                a.setVisible(true);
            }
        });
    }   
}

And here is the User Class:

import shopproject.gui;

public class User {    
    public static void registerUser(gui mainFrame) {
        System.out.println("checking registration credentials");
        // cannot access component of mainFrame called "userFormUsernameField".
        mainFrame.userFormUsernameField.getText();
    }
}

Here i cannot access userFormUsernameField or other components of the mainframe like JPanels JLabels etc.

SyedAfaq
  • 13
  • 6
  • 1
    Can you provide a [MCVE] to help others find a solution? – Alias Cartellano May 19 '22 at 19:30
  • Netbeans has *nothing* to do with this problem. Stop tagging it. But post that [mre] @AliasCartellano suggested over 11 hours age. – Andrew Thompson May 20 '22 at 06:37
  • @AndrewThompson what do you mean i created the gui in netbeans JFrame form ? and i pasted the code. – SyedAfaq May 20 '22 at 07:49
  • A *"netbeans JFrame form"* is a *"Java JFrame"*. The IDE should only be tagged if a different IDE gives a different result. *"and i pasted the code"* Yes you did. All **314 lines** of it. The thing is, that is not a [mre]. Read [Short, Self Contained, Correct (Compilable), Example](http://sscce.org/). It's essentially the same thing as an MRE, but it goes into more detail on how and *why* to make a short example that demonstrates the problem. An MRE of the problem can usually be created in under **50 lines of code.** – Andrew Thompson May 20 '22 at 08:36
  • @AndrewThompson it won't run without all that code thats why i included it it's the default code generated by netbeans when creating a new JFrame form file. I removed it now though it only has the code where the problems is.. – SyedAfaq May 20 '22 at 09:22
  • https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in – matt May 20 '22 at 09:27
  • Otherwise, we need the parts of the code where you create your objects and expose them. You might make a `public JTextField getFormUserNameField(){ return userFormUsernameField;}` although, you probably don't want to expose the JTextField, just the string it contains. – matt May 20 '22 at 09:28
  • @matt yes i think the gui.java is package private that is why i can't access it. i could just pass the userFormUsernameField String but that would mean i'd have to pass the dozen other components when i need them for something. i just want to pass the main gui class and handle all functionalities inside the user class reducing the amount of code in the main controller(gui) class. – SyedAfaq May 20 '22 at 10:03
  • You've missed my point. `gui` is not private it is a public class. `userFormUsernameField` is private. In your class `gui` you can create a method `public String getUserFormUsername(){ return userFormUsernameField.getText();}` that way you are exposing the text from a public method – matt May 20 '22 at 10:11
  • @matt i could just directly pass the getUserFormUsername().getText() as a parameter No need for a getter. Instead i made a getter for userFormJPanel so i can access all elements inside it including userFormUsername jField when i pass in the function but unfortunately i cannot access the child elements directly they also are private and need a getter to access them so i guess i just have to pass every element as parameter in order to use it.. – SyedAfaq May 20 '22 at 10:40
  • Or you make a public getter. " i could just directly pass the getUserFormUsername().getText() " you could make whatever you want a parameter, I don't get your point. ". Instead i made a getter for userFormJPanel so i can access all elements inside it " I don't see that, but even if you "get the panel" you cannot access it's private members. That's why you would make a getter. You're really going to need to make a compilable *concise* example because it is not clear what you want. – matt May 20 '22 at 10:45
  • @matt Ok i understand i need getters to access them. just one more thing: in the above code when i pass User.registerUser(this) can you tell me what is "this" ? is it the gui class or instance of gui class ? – SyedAfaq May 20 '22 at 11:06
  • https://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java it referes to the instance of the gui class that you're calling `userFormRegisterBtnActionPerformed` with. – matt May 20 '22 at 11:14

0 Answers0