-1

I have the following code

DefaultListModel<String> modelPacientes = new DefaultListModel<>();
JList <String> listPacientes = new JList<>( modelPacientes );
listPacientes.setToolTipText("Lista de los pacientes del doctor");
GridBagConstraints gbc_listPacientes = new GridBagConstraints();
gbc_listPacientes.fill = GridBagConstraints.BOTH;
gbc_listPacientes.gridx = 0;
gbc_listPacientes.gridy = 0;
pPacientes.add(listPacientes, gbc_listPacientes);
modelPacientes.addElement(raul.getName() + " " + raul.getSurname());
modelPacientes.addElement(paula.getName() + " " + paula.getSurname());
modelPacientes.addElement(sara.getName() + " " + sara.getSurname());

I am modifying the values of the objects (raul, paula and sara) in another frame, how could I update it in the JList or anywhere else (JLabel) after closing the other frame?

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
Ulises CT
  • 1,361
  • 1
  • 12
  • 21
  • 1
    1) *"..in another frame"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 29 '16 at 20:37

3 Answers3

2

You can either provide your own subclass of ListModel (or DefaultListModel) that returns patient.getName() + " " + patient.getSurname()) in method getElementAt

or you can move the creation of the modelPacientes to a new method:

ListModel createModel() {
  DefaultListModel<String> modelPacientes = new DefaultListModel<>();
  modelPacientes.addElement(raul.getName() + " " + raul.getSurname());
  modelPacientes.addElement(paula.getName() + " " + paula.getSurname());
  modelPacientes.addElement(sara.getName() + " " + sara.getSurname());
  return modelPacientes;
}

then build an empty JList instead and run listPacientes.setModel(createModel()) whenever the pacientes have changed.

This approach is fine if you have a couple dozens patients. If you have hundreds or thousands of patients, it is probably worthwhile to you have your own model classes that map from your patient data into whatever the Swing component needs.

Jochen Bedersdorfer
  • 4,093
  • 24
  • 26
2

I am modifying the values of the objects

Then the ListModel should contain the Objects not a String. So assuming your Objects are of type User you would do something like:

DefaultListModel<User> modelPacientes = new DefaultListModel<User>();
JList<User> listPacientes = new JList<User>( modelPacientes );

modelPacientes.addElement(raul);
modelPacientes.addElement(paula);
modelPacientes.addElement(sara);

Then in the User class you can override the toString() method to return the value you want to display in the JList:

@Override
public String toString()
{
    return getName() + " " + getSurname();
}

I am modifying the values of the objects (raul, paula and sara) in another frame

Then you get the User object you want to modify from the ListModel and pass this object to your JDialog (note you should be using a JDialog, not a JFrame for a popup child window).

Then you update the data in the User object and when the dialog closes you invoke repaint() on the JList.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Add the objects (raul, paula and sara) to the list modal. Then create a ListCellRenderer that will return a string that is the "display name" for example:

raul.getName() + " " + raul.getSurname()

That way when the name of raul, paula and/or sara changes it will automatically change in the list if it is the same instance of the object.

Jayfray
  • 415
  • 3
  • 12