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

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    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
    Code:
    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;
        initComponents();
    }
    
    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
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    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