1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Rep Power

    Add new item to ArrayList in JPanel from another JPanel

    Hello, id like to know how can i add item from one JPanel to another. I have JPanel
    public class Program extends javax.swing.JFrame {
     public ArrayList<Contact> contacts = new ArrayList<Contact>();
     public int pos;
    public Program() {
        contacts.add(new Contact("John Tavares", "736426436", "Prague"));
        contacts.add(new Contact("John Doe", "725847896", "NY"));
        contacts.add(new Contact("Damien Smith", "725589963", "London"));
        pos = 0;
    private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {                                    
          new NewContact(); 
    class NewContact is also new JPanel where i have text fields for name, tel and city and ok button. Can anybody help me, how can i add this data from JPanel NewContact, to ArrayList in class Program? I know function textField.getText(), but i dont know, how can i add it direct to ArrayList.

    Thanks for any advices...
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Mobile, Alabama
    Rep Power
    You can create an mutator method in the class where you want the data. Then in the first class get a reference to the instance of the second class. Then you can call the mutator method.

IMN logo majestic logo threadwatch logo seochat tools logo