0

I am relatively new in Java. In one of my project I have three jframes. They are Menuframe,that has list of menus; LoadDatafromExcelframe, that is use to select and read data from Excel file; and ShowDataFrame, that is use to display data that I read in LoadDatafromExcelframe. Actually its part of a big project. not the whole project! At the beginning of my project I am reading Excel file and load all the data in arraylist. Till this point I am good. Now I want to click the ShowDataFrame from the menulist to display the data that I have read in the LoadDatafromExcelframe. If I can access the arraylist then I think I can move forward. My question is "How can I access arraylist from one jframe (ShowDataFrame) to another frame(LoadDatafromExcelframe)?"I am using Netbeans IDE.

private void jMenuItemShowDataActionPerformed(java.awt.event.ActionEvent evt) {                                                   
            showDataFrame.setVisible(true);
}
utij2004
  • 453
  • 1
  • 5
  • 14
  • 4
    Can you show a little code? Specifically where you are creating your `ShowDataFrame`. – Luke Willis Aug 13 '14 at 20:32
  • 2
    Aside: Do you really need [two frames](http://stackoverflow.com/q/9554636/230513)? – trashgod Aug 13 '14 at 20:35
  • yes. actually i need to access those data (arraylist) from several jframes (not only from one frame). so if i can access that arraylist from one frame then i can access it from another frame too. – utij2004 Aug 13 '14 at 20:39
  • 1
    Code example would be good. If you're not using the List in different parts of the code at the same time and the List is a class variable of LoadDataFromExcelFrame then you could just make a getter function and call it from ShowDataFrame (the getter and List could be static if necessary). Should be simple unless I'm completely misunderstanding the Q. – kag0 Aug 13 '14 at 20:42
  • 2
    Actually it seems like a beginner question about getters and setters (or static fields). I don't mean to be rude, but you should maybe learn about Java important concepts before getting involved in a "big project". – Dici Aug 13 '14 at 20:44
  • it's a group work. but i wanna solve this problem. without involve in a project how can i learn and improve myself? – utij2004 Aug 13 '14 at 20:50

2 Answers2

3

The key issue has little to do with JFrames or Swing in fact, but is simply one of passing information from one object to another. The only way Swing gets involved is if you want to pass this information in response to an event of one sort or another.

The solution is usually one of simply giving your classes appropriate getter and setter methods, of separating out your "model", the logical portion of your program, from your "view", the GUI portion of your program, and of using some sort of listener or observer type interface (such as can be obtained via Swing event listeners) to notify one class when the other is ready to push or pull information.

More general recommendations:

  • Avoid making anything static that does not need to be static. That is a quick solution that usually causes more pain in the long run as it makes your code very hard to enhance later and to test.
  • Avoid using lots of JFrames. Most professional GUI's have but one master window, one "JFrame" if you will. Often it is better to swap views in this JFrame such as with a CardLayout or with tabbed panes if needed. Also information that needs to be obtained in another window in a modal fashion can be displayed in a modal dialog.
  • Get a good book on OOPs basics as it applies to Java programming, such as Bruce Eckel's "Thinking in Java".
  • And get a good book on use of design patterns with Java such as Head First Design Patterns. These two concepts are key to moving ahead in Java.
  • Avoid using code generation utilities such as NetBean's form creation utility as it shields you from learning Swing specifics, and hinders your ability to learn to create complex Swing GUI's. Sure, use these tools once your a Swing journeyman, but until then, use the Swing tutorials to learn the library and code by hand.

For more specific advice, you will need to provide more details and provide more code.


Edit 2

More advice based on your post:

In one of my project I have three jframes. They are Menuframe,that has list of menus; LoadDatafromExcelframe, that is use to select and read data from Excel file; and ShowDataFrame, that is use to display data that I read in LoadDatafromExcelframe.

This looks to be better implemented creating 3 JPanels and not 3 JFrames. Then you could display the JPanels as needed in a single JFrame using a CardLayout to help you swap them.

Actually its part of a big project. not the whole project! At the beginning of my project I am reading Excel file and load all the data in arraylist.

This ArrayList should not be "read into a JFrame" but rather the data belongs in a non-GUI class, one of your "model" classes to be exact. Then the view classes, your GUI code can ask the model for this data whenever necessary. Read up on Model-View-Control program design on this site to learn more about this useful pattern.

Till this point I am good. Now I want to click the ShowDataFrame from the menulist to display the data that I have read in the LoadDatafromExcelframe.

Here using MVC structure, one of your "view" classes, the one holding the "menulist" should notify the "control" class that your code needs the ArrayList data held by the "model" class. This could all be done by having the Control class hold references to both "model" and "view" and having the "view" class hold references to the control thus allowing communication between classes.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
-2

You can change your ArrayList in static an public in the properties of the object then just call the name of te class who contains de ArrayList and call the ArrayList and use it wherever you want.

Something like this:

JFrame1.ArrayList1.add("some stuff");

Obviously doing this in the JFrame2 class where you want to call the ArrayList