Hi I am learning java and in my project I am trying to pass a data to another JFrame.
This is my Guest Frame class
public class GuestFrame extends javax.swing.JFrame {
private List<String> list = new ArrayList<String>();
public GuestFrame(){
initComponents();
}
}
The way I am adding data to arraylist is by adding selected item from JList to the cart appended one by one like so :
private void kButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String receiveList = lstEntitety.getSelectedValue().toString();
list.add(receiveList);
//System.out.println(list.toString()); outputs all the data added
And the getter function for that list :
public List<String> getList() {
return list;
}
What I am trying to do is display all the added food in my another JFrame
public class CartFrame extends javax.swing.JFrame {
private GuestFrame food;
public CartFrame() {
initComponents();
food= new GuestFrame();
List<String> list = food.getList();
//Here I am trying to output the arraylist that I appended in prevous frame
jTextArea1.setText(list.toString());
}
The result upon stepping into CartFrame is that array seems to output empty []
I figured it might be because in Guest frame constructor is overriding it ?
I am not sure how to resolve this issue.
ArrayListat the time of declarationprivate List<String> list ==new ArrayList<String>();remove from constructorkButton1ActionPerformed? and try to print all objects added to arraylist in that method and check once