private void init() { listMultipleSelection = getMultipleSelection(); listModel = new DefaultListModel(); // create GUI if (listMultipleSelection.getPossibleValues().isEmpty()) { panel = null; itemsContainerList = null; return; } final Border padding = BorderFactory.createEmptyBorder(5, 10, 5, 10); panel = new JPanel(); BorderLayout layout = new BorderLayout(); panel.setLayout(layout); final JTextArea jta = new JTextArea(getDescription()); jta.setPreferredSize(new Dimension(200, 50)); jta.setLineWrap(true); jta.setWrapStyleWord(true); jta.setBorder(padding); panel.add(jta, BorderLayout.PAGE_START); jta.setBackground(null); jta.setEditable(false); // put the items in a list itemsContainerList = new JList(listModel); // new JList(listMultipleSelection.getPossibleValues().toArray()); for (T value : getMultipleSelection().getPossibleValues()) listModel.addElement(value); itemsContainerList.setFont(new Font("sansserif", Font.PLAIN, 11)); itemsContainerList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); itemsContainerList.addListSelectionListener(this); // selected items final List<T> selectedVals = listMultipleSelection.getSelectedValues(); final List<T> allValues = listMultipleSelection.getPossibleValues(); final int[] selectedIdx = new int[selectedVals.size()]; int index = 0; for (T selected : selectedVals) { for (int i = 0; i < allValues.size(); i++) { if (itemsContainerList.getModel().getElementAt(i).equals(selected)) { selectedIdx[index] = i; index++; } } } itemsContainerList.setSelectedIndices(selectedIdx); // use a JscrollPane to visualize the items final JScrollPane scrollpane = new JScrollPane(itemsContainerList); scrollpane.setAutoscrolls(true); scrollpane.setOpaque(false); scrollpane.setBorder(padding); panel.add(scrollpane, BorderLayout.CENTER); }
@Override public void update() { boolean reloadSelection = false; listMultipleSelection = getMultipleSelection(); // If the list of elements has changed, remove old elements and add new ones if (!Arrays.equals(listModel.toArray(), listMultipleSelection.getPossibleValues().toArray())) { listModel.removeAllElements(); reloadSelection = true; for (T value : listMultipleSelection.getPossibleValues()) listModel.addElement(value); } else { // if the list is the same but the selection has changed, remove all selections and select new // ones if (!Arrays.equals( itemsContainerList.getSelectedValues(), listMultipleSelection.getSelectedValues().toArray())) reloadSelection = true; } if (reloadSelection) { // selected items final List<T> selectedVals = listMultipleSelection.getSelectedValues(); final List<T> allValues = listMultipleSelection.getPossibleValues(); final int[] selectedIdx = new int[selectedVals.size()]; int index = 0; for (T selected : selectedVals) { for (int i = 0; i < allValues.size(); i++) { if (itemsContainerList.getModel().getElementAt(i).equals(selected)) { selectedIdx[index] = i; index++; } } } itemsContainerList.removeSelectionInterval(0, allValues.size() - 1); itemsContainerList.setSelectedIndices(selectedIdx); } }