tags:

views:

115

answers:

2

I have a main frame that has a list and "add" button. When I click on the 'add' button, one frame will be shown that I can enter name, family and id.

And by clicking on the "addAPerson", the name will be stored in the SQL. But when I close the frame, the new person will not be added to my list which is in my main frame but if I close the main frame and run it again, the new person will be added to the list.

How can I update the JList without running the main frame?

My main frame (a part of that):

    public class ListFrame extends javax.swing.JFrame {

        private InformationClass client;
        private DefaultListModel model = new DefaultListModel();

        /** Creates new form ListFrame. */
        public ListFrame(InformationClass client) {
            initComponents();
            this.client = client;
            fillTable();
        }

        public  void fillTable() {
            try {
                List<InformationClass> list = null;
                list = Manager.getClientListFromMySQL();
                if (list == null) {
                    JOptionPane.showMessageDialog(
                        this,
                        "You should add a person to your list",
                        "Information",
                        JOptionPane.OK_OPTION);
                    return;
                }
                else {
                    for (int i = 0; i < list.size(); i++) {
                        InformationClass list1 = list.get(i);
                        model.add(i, list1.getId());
                    }
                    jList1.setModel(model);
                }
            }
            catch (SQLException ex) {
                Logger.getLogger(
                  ListFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

My frame which will be shown when you click on the add button (a part of that):

    public class Add extends javax.swing.JFrame {

        private InformationClass client;

        public Add(InformationClass client) {
            initComponents();
            this.client = client;
        }
        private void addAPersonActionPerformed(java.awt.event.ActionEvent evt) {
            submit();
            clear();
        }
        private void submit() {
            String s1 = nameF.getText();
            String s2 = familyF.getText();
            String s3 = iDf.getText();

            if (s1.equals("") || s2.equals("") || s3.equals("")) {
                JOptionPane.showMessageDialog(
                    this,
                    "fill the empty name/family/id text fields",
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
            else {
                try {
                    boolean b = Manager.isAddPerson(s1, s2, s3);
                    if (b == false) {
                        Manager.addPerson(s1, s2, s3);
                        JOptionPane.showMessageDialog(
                            this,
                            "The person has been added successfully",
                            "Information",
                            JOptionPane.INFORMATION_MESSAGE);
                    }
                    else {
                        JOptionPane.showMessageDialog(
                            this,
                            "These datas has been added before!!",
                            "Information",
                            JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }
    }

My Manager class (a part of that):

    public static void addPerson(String name, String family, String yahooId) {
        PreparedStatement pstmt;
        String query;

        try {
            query = ("INSERT INTO newpersontable(name,family,yahooId) VALUES(?,?,?)");

            pstmt = (PreparedStatement) conn.prepareStatement(query);
            pstmt.setString(1, name);
            pstmt.setString(2, family);
            pstmt.setString(3, yahooId);
            pstmt.executeUpdate();
        }
        catch (SQLException e) {
            Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, e);
        }
    }
A: 

In the submit method save the created person and create a field for it which you can 'get' from the actionPerformed method for the 'add' button in the ListFrame class. Then just add it to the list model.

willcodejavaforfood
+1  A: 

An implementation of the Observer pattern will do the trick.

Your InformationClass calls the Manager to add a person, if it is not already known. But you want that new person appear in the ListFrame. With the Observer pattern, the ListFrame will observe the Manager if it has some added, changed or deleted records. If it is the case, the ListFrame can update itself with the actual values, that it just requests again from the Manager.

We need one additional interface, a Listener and some methods on the Manager, that's all.

Here's a basic implementation:

public interface PersonsModelChangeListener {
  public void modelHasChanged();
}

In Manager, add the following fields and methods:

List<PersonsModelChangeListener> listeners = new ArrayList<PersonsModelChangeListener>();

public void addListener(PersonsModelChangeListener listener) {
  listeners.add(listener);
}

private void fireModelChangeEvent() {
  for (PersonsModelChangeListener listener:listeners) {
    listener.modelHasChanged();
  }
}

Add the following line to the add method of Manager:

public void add(String s1, String s2, String s3) {
  // existing code
  fireModelChanged();
}

Next step: make ListFrame implement the PersonsModelChangeListener interface and implement the modelHasChanged method so that ListFrame can 'get' the actual values whenever the Managers data set has changed.

Edit

public class Manager {

    // existing code

    private static List<PersonsModelChangeListener> listeners = new ArrayList<PersonsModelChangeListener>();

    public static void addListener(PersonsModelChangeListener listener) {
      listeners.add(listener);
    }

    private static void fireModelChangeEvent() {
      for (PersonsModelChangeListener listener:listeners) {
        listener.modelHasChanged();
      }
    }

    public static void addPerson(String name, String family, String yahooId) {
      PreparedStatement pstmt;
      String query;
      try {
        query = ("INSERT INTO newpersontable(name,family,yahooId) VALUES(?,?,?)");
        pstmt = (PreparedStatement) conn.prepareStatement(query);
        pstmt.setString(1, name);
        pstmt.setString(2, family);
        pstmt.setString(3, yahooId);
        pstmt.executeUpdate();

        fireModelChangedEvent();

    } catch (SQLException e) {
        Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, e);
    }
}

public class ListFrame extends javax.swing.JFrame implements PersonsModelChangeListener {

  // your fields

  /** Creates new form ListFrame */
  public ListFrame(InformationClass client) {
    initComponents();
    this.client = client;
    fillTable();
    Manager.addListener(this);
  }

  @Override
  public void modelHasChanged() {
    // this method will be called when you add something with the
    // Manager.
    // Add your code here to get the actual data from the Manager
    // and update this component
  }
}

Hope it helps :)

Andreas_D
i couldn't do what you have written,i have edited my post(I added a part of my Manager class),also your modelHasChanged() method has no body in the Manager class.
Johanna
would you please help me with this Manger class??Thanks
Johanna
thanks a lot for your edition,but I can not use the modelHasChanged method in the Manager class!
Johanna
Oooops - sorry!! edited my code...
Andreas_D
It doesn't work!! also this is my modelHasChanged method: public void modelHasChanged() { try { model.clear(); List<InformationClass> list = null; list = Manager.getClientListFromMySQL(); for (int i = 0; i < list.size(); i++) { InformationClass list1 = list.get(i); model.add(i, list1.getId()); } jList1.setModel(model); } catch (SQLException ex) { Logger.getLogger(ListFrame.class.getName()).log(Level.SEVERE, null, ex); } }
Johanna
I'm not an expert on swing but maybe you just have to refresh/repaint your jList1 or the complete frame after changing the model.
Andreas_D