0

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.

9
  • Initialize ArrayList at the time of declaration private List<String> list ==new ArrayList<String>(); remove from constructor Commented Dec 31, 2018 at 5:15
  • I tried that but the result is still empty array Commented Dec 31, 2018 at 5:19
  • at which point this method is called kButton1ActionPerformed? and try to print all objects added to arraylist in that method and check once Commented Dec 31, 2018 at 5:20
  • when appending the items to cart, from the GuestFrame class. After that the items should be displayed in CartFrame but the result is empty. The printouts are outputing the right results appended one by one. Commented Dec 31, 2018 at 5:22
  • 1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) See The Use of Multiple JFrames, Good/Bad Practice? 3) Don't extend components or windows unless there is a good reason to do so. I see none here. 4) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Commented Dec 31, 2018 at 7:22

1 Answer 1

1

I think there are 3 options to solve this case,
1. may use database to manipulate the list data. so GuestFrame is used to save data while CartFrame can get the data from database without have dependency on other class property.

2. second, may use java.util.Properties to manipulate list data.

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("xyz.properties");
prop.load(in);

save data in GuestFrame:

prop.setProperty("dataSize", "(list.lenght())");
prop.setProperty("data1", "...");
prop.setProperty("data2", "...");
prop.setProperty("...", "...");
....
prop.store(new FileOutputStream("xyz.properties"), null);

load data in CartFrame:

prop.getProperty("dataSize");
//loop i=0 until < dataSize
list.add(prop.getProperty("data"+i));


3. last option is to make private List<String> list = new ArrayList<String>(); become static private static List<String> list = new ArrayList<String>();, so then other classes can access the list property from GuestFrame directly without need to create the instance.

  public class GuestFrame extends javax.swing.JFrame {
    private static List<String> list = new ArrayList<String>();

    public GuestFrame(){
       initComponents();    

    }

    public static List<String> getList() {
       return list;
    }
 }


 public class CartFrame extends javax.swing.JFrame {

 public CartFrame() {     
   initComponents();
   List<String> list = GuestFrame.getList();
   cText.setText(list.toString());
   jLabel3.setText(list.toString());
 }
}

Static property are associated to the class directly. they can be called even without creating an instance of the class, ex: ClassName.propertyName

Sign up to request clarification or add additional context in comments.

10 Comments

btw you can also change private static into public static so then in the CarFrame can get the list value by accessing GuessFrame property directly List<String> list = GuestFrame.list; so that doesnt need to create getList() method
Well yeah It works this way. I am fairly new but while searching for the answer I saw a lot of answers explicitly saying that static should not be used when doing this kind of task, mainly because of OOP ways and security..?
"I think one of the option is to make .. become static" It might be an option, but it is a terrible one. Using static to make members of one class visible to another is bad design, and more often leads to problems, than solves them.
yeah I know, I offer this option because I thought it is only a simple case. than for more safer, may save data into the database. so GuestFrame is used to save data while inside CartFrame can get the data from database without have dependency on other class property.
or can save/load data using properties file, docs.oracle.com/javase/7/docs/api/java/util/Properties.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.