How to link a list (one or more times)?

I follow a sample view model for coding some screens.

  • I store several Beans in an ArrayList
  • I will show the contents of this list in JTable , thanks to AbstractTableModel
  • I also want to display some entries from this list in a combo box (in form) and some others in JList, at the same time
  • These three screens (and their model) are independent of each other

How do I manage the addition of {one or more} / remove {one or more} in my list and see the changes in real time everywhere?

I am going to write my own ObservableList or implement it around EventDispatcher ... What do you think?


PS:

  • I know that in C # BindingList helps with this, but what about Java?
  • I can already display updates for each bean thanks to PropertyChangeSupport .
+4
source share
2 answers

Let your AbstractTableModel implement a ListModel , which can be used as with JComboBox and JList. You can forward default methods if required.

Appendix: SharedModelDemo , mentioned in How to Use Tables , is an example that can run you. This is extends DefaultListModel implements TableModel , while you should do extends AbstractTableModel implements ListModel

Appendix: for reference, here is an example of a minimal implementation and three test instances. I used the default combo and list implementation, but you can use the appropriate abstract implementations if necessary.

 public class SharedModel extends AbstractTableModel implements ComboBoxModel, ListModel { private ComboBoxModel comboModel = new DefaultComboBoxModel(); private ListModel listModel = new DefaultListModel(); //ComboBoxModel @Override public void setSelectedItem(Object anItem) { comboModel.setSelectedItem(anItem); } @Override public Object getSelectedItem() { return comboModel.getSelectedItem(); } // ListModel @Override public int getSize() { return listModel.getSize(); } @Override public Object getElementAt(int index) { return listModel.getElementAt(index); } @Override public void addListDataListener(ListDataListener l) { listModel.addListDataListener(l); } @Override public void removeListDataListener(ListDataListener l) { listModel.removeListDataListener(l); } // TableModel @Override public int getRowCount() { return 0; } @Override public int getColumnCount() { return 0; } @Override public Object getValueAt(int rowIndex, int columnIndex) { return null; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { SharedModel sm = new SharedModel(); JTable table = new JTable(sm); JList list = new JList(sm); JComboBox check = new JComboBox(sm); } }); } } 
+4
source

For JComboBox and JList, you can simply refer to sections of the ArrayList array using the subList () method. This will work if you can easily determine the start and end locations in the ArrayList, and the elements you need are sequential.

If the situation is more dynamic than you could implement the custom List classes that took the ArrayList in the constructor, and then applied any logic needed to return the corresponding records.

+2
source

Source: https://habr.com/ru/post/969898/


All Articles