How to access LinkedList inside main from my Jframe button call

:: UPDATE :::

I tried updating mine JFrame

to take a LinkedList as an argument, it looks like ...

 public userLogin(LinkedList<dataUser> ll) {
    initComponents();
 }

      

My main thing now is to call him ...

userLogin frame = new userLogin(userLL);
frame.setVisible(true);

      

Still can't manage to use LinkListLLLLL inside my JFrame

:: END UPDATE ::

This is my first time using netbeans and the GUI builder. I have a class TaskManager

that I am using as my main class. This class creates several LinkedList

and calls my first JFrame

GUI like this:

public static void main(String[] args) {
    LinkedList<dataUser> userLL = new LinkedList<dataUser>(); //creates a LL for userData
    LinkedList<task> taskLL = new LinkedList<task>(); //creates a LL for taskData
    LinkedList<task> progressLL = new LinkedList<task>(); //creates a LL for in progress tasks
    LinkedList<task> completeLL = new LinkedList<task>(); //creates a LL for completed tasks

    userLogin frame = new userLogin();
    frame.setVisible(true);

      

However, inside mine, userLogin

I cannot access the userLL

one I created. Here is the code inside my submit button:

private void submitBActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String user = jTextField1.getText();
    String userPW = jTextField2.getText();

    try {
        //below userLL is not accessible because it can't be found.
        dataUser.userDataSearch(userLL, userPW);
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(this, "was not found", "error", JOptionPane.ERROR_MESSAGE);

        return;
    }
}   

      

As a comment in the code, I cannot run the function because I do not have access to the userLL

( LinkedList

which I created in my main program that runs this one JFrame

).

Do I have to pass in LinkedList

in my JFrame

as an argument so it can use it? I assumed that declaring this on the master would allow access, but now it is local to the master.

+3


source to share


4 answers


You have to pass the userLL

as parameter to your class userLogin

like this:

public class userLogin {

    LinkedList<dataUser> userLL

    public userLogin(List userLL){
        this.userLL = userLL;
    }

    //.......
}

      



And in your main class, create it like this: userLogin frame = new userLogin(userLL);

+3


source


You've created a userlogin class that extends the JFrame (the magic was done by Netbeans). The main class is only the entry point for your program, and the scope of main is unknown to the userlogin class.

You have options (you have others with static declarations, but we shouldn't consider them):

  • Pass the data you need to be handled by the class using a constructor like @Maraboc post

  • Create a setter method in the UserLogin class to set the list or other data, but you'll need to make sure the data is set up when you need it.



There are other options for providing external data to a class in the context of a program. Using some Singleton pattern (ex: GlobalDataStorageSingleton), which might be bad design according to SOLID paradigms.

In OOP, you can see the principles of SOLID vs STUPID.

Best Pedro

+2


source


There are two options for solving this problem.

  • You already mentioned the first one: pass LinkedList

    (either in the constructor or later via some setter method).

  • Second option: declare an LinkedList

    object variable of your main class and access it using a getter. If you don't have a reference to the main object of the class, a static variable should do the trick (if only one instance LinkedList

    .

I would definitely recommend option 1, as this is the standard way to navigate and access to the list is better limited.

+1


source


You will not have access to the lists created in the main () class. You will have to pass them as parameters to the UserLogin class you created.

Something like:

public class UserLogin
{
     private final LinkedList<datauser> ll1;
     private final LinkedList<task> taskLL;
     private final LinkedList<task> progressLL;
     private final LinkedList<task> completeLL;
     public class UserLogin(LinkedList<datauser> ll1, LinkedList<task> taskLL, LinkedList<task> progressLL, LinkedList<task> completeLL)
     {
        this.ll1 = ll1;
        this.taskLL = taskLL;
        this.progressLL = progressLL;
        this.completeLL = completeLL;
     }  

    //now you should be able to use the linked lists within the event handler
    private void submitBActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String user = jTextField1.getText();
    String userPW = jTextField2.getText();

    try {
        //below userLL is not accessible because it can't be found.
        dataUser.userDataSearch(userLL, userPW);
    } 
    catch (Exception e) {
        JOptionPane.showMessageDialog(this, "was not found", "error", JOptionPane.ERROR_MESSAGE);

        return;
    }
}   

      

UPDATE based on comment from OP. In the main method, you must initialize the UserLogin as shown below:

public static void main(String[] args)
{
   LinkedList<datauser> ll1 = new LinkedList<datauser>();
   LinkedList<datauser> taskLL = new LinkedList<datauser>();
   LinkedList<datauser> progressLL = new LinkedList<datauser>();
   LinkedList<datauser> completeLL = new LinkedList<datauser>();

   //feed data within the above linked lists..
   ...

  //initialize the user login class with the linked lists you created
   UserLogin userLogin = new userLogin(ll1,taskLL, progressLL, completeLL);
}

      

In general - you can take a look at basic Java concepts related to object creation if you haven't worked with java before.

+1


source







All Articles