Java – how to access LinkedList in main from my JFrame button call
:::::: update:::::
I tried to update my JFrame to take LinkedList as a parameter. It looks like
public userLogin(LinkedList<dataUser> ll) {
initComponents();
}
My main is to call it
userLogin frame = new userLogin(userLL); frame.setVisible(true);
Still can't use LinkedList userll in my JFrame
:::::: END UPDATE ::::::
This is my first time using NetBeans and GUI builder I have a class taskmanager that I am using as my main class This class creates some linkedlists and calls my first JFrame GUI, as shown below:
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, in my Userlogin, I cannot access the userll I created This is the code in 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 the comments in the code say, I can't run this function because I can't access userll (the LinkedList I created in my main program, which starts the JFrame)
Do I have to pass LinkedList into my JFrame as a parameter that it can use? I assume that it is declared in main that it will allow access, but it now seems to be the main local
Solution
You must pass userll as a parameter to the Userlogin class, as follows:
public class userLogin {
LinkedList<dataUser> userLL
public userLogin(List userLL){
this.userLL = userLL;
}
//.......
}
Instantiate in your main class as follows: Userlogin frame = new Userlogin (userll);
