Example usage for javax.swing JList getModel

List of usage examples for javax.swing JList getModel

Introduction

In this page you can find the example usage for javax.swing JList getModel.

Prototype

public ListModel<E> getModel() 

Source Link

Document

Returns the data model that holds the list of items displayed by the JList component.

Usage

From source file:DropDemo.java

public boolean importData(TransferHandler.TransferSupport info) {
    if (!info.isDrop()) {
        return false;
    }/*from w w w .ja  v  a  2s  .c  o m*/

    JList list = (JList) info.getComponent();
    DefaultListModel listModel = (DefaultListModel) list.getModel();
    JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
    int index = dl.getIndex();
    boolean insert = dl.isInsert();

    // Get the string that is being dropped.
    Transferable t = info.getTransferable();
    String data;
    try {
        data = (String) t.getTransferData(DataFlavor.stringFlavor);
    } catch (Exception e) {
        return false;
    }

    // Perform the actual import.
    if (insert) {
        listModel.add(index, data);
    } else {
        listModel.set(index, data);
    }
    return true;
}

From source file:ListCutPaste.java

/**
 * When the export is complete, remove the old list entry if the action was a
 * move.// www  .j  a  va  2 s  . c o m
 */
protected void exportDone(JComponent c, Transferable data, int action) {
    if (action != MOVE) {
        return;
    }
    JList list = (JList) c;
    DefaultListModel model = (DefaultListModel) list.getModel();
    int index = list.getSelectedIndex();
    model.remove(index);
}

From source file:ExtendedDnDDemo.java

protected void cleanup(JComponent c, boolean remove) {
    if (remove && indices != null) {
        JList source = (JList) c;
        DefaultListModel model = (DefaultListModel) source.getModel();
        //If we are moving items around in the same list, we
        //need to adjust the indices accordingly, since those
        //after the insertion point have moved.
        if (addCount > 0) {
            for (int i = 0; i < indices.length; i++) {
                if (indices[i] > addIndex) {
                    indices[i] += addCount;
                }//from   w w w  .j ava2  s .c om
            }
        }
        for (int i = indices.length - 1; i >= 0; i--) {
            model.remove(indices[i]);
        }
    }
    indices = null;
    addCount = 0;
    addIndex = -1;
}

From source file:ExtendedDnDDemo.java

protected void importString(JComponent c, String str) {
    JList target = (JList) c;
    DefaultListModel listModel = (DefaultListModel) target.getModel();
    int index = target.getSelectedIndex();

    //Prevent the user from dropping data back on itself.
    //For example, if the user is moving items #4,#5,#6 and #7 and
    //attempts to insert the items after item #5, this would
    //be problematic when removing the original items.
    //So this is not allowed.
    if (indices != null && index >= indices[0] - 1 && index <= indices[indices.length - 1]) {
        indices = null;/*from   w  w w .  j a  v a 2  s.c o m*/
        return;
    }

    int max = listModel.getSize();
    if (index < 0) {
        index = max;
    } else {
        index++;
        if (index > max) {
            index = max;
        }
    }
    addIndex = index;
    String[] values = str.split("\n");
    addCount = values.length;
    for (int i = 0; i < values.length; i++) {
        listModel.add(index++, values[i]);
    }
}

From source file:ListCutPaste.java

/**
 * Perform the actual data import./*from ww  w . j a  v  a  2  s . com*/
 */
public boolean importData(TransferHandler.TransferSupport info) {
    String data = null;

    // If we can't handle the import, bail now.
    if (!canImport(info)) {
        return false;
    }

    JList list = (JList) info.getComponent();
    DefaultListModel model = (DefaultListModel) list.getModel();
    // Fetch the data -- bail if this fails
    try {
        data = (String) info.getTransferable().getTransferData(DataFlavor.stringFlavor);
    } catch (UnsupportedFlavorException ufe) {
        System.out.println("importData: unsupported data flavor");
        return false;
    } catch (IOException ioe) {
        System.out.println("importData: I/O exception");
        return false;
    }

    if (info.isDrop()) { // This is a drop
        JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
        int index = dl.getIndex();
        if (dl.isInsert()) {
            model.add(index, data);
            return true;
        } else {
            model.set(index, data);
            return true;
        }
    } else { // This is a paste
        int index = list.getSelectedIndex();
        // if there is a valid selection,
        // insert data after the selection
        if (index >= 0) {
            model.add(list.getSelectedIndex() + 1, data);
            // else append to the end of the list
        } else {
            model.addElement(data);
        }
        return true;
    }
}

From source file:be.ugent.maf.cellmissy.gui.controller.analysis.singlecell.SingleCellStatisticsController.java

/**
 * Update the list with conditions./*from   w  ww. ja v  a  2s. com*/
 */
public void updateConditionList() {
    JList conditionList = singleCellAnalysisController.getAnalysisPanel().getConditionList();
    DefaultListModel model = (DefaultListModel) conditionList.getModel();
    if (singleCellAnalysisController.isFilteredData()) {
        singleCellAnalysisController.getFilteringMap().keySet().stream().forEach((conditionDataHolder) -> {
            model.addElement(conditionDataHolder.getPlateCondition());
        });
    } else {
        singleCellAnalysisController.getPreProcessingMap().values().stream().forEach((conditionDataHolder) -> {
            model.addElement(conditionDataHolder.getPlateCondition());
        });
    }
}

From source file:com.gdc.nms.web.mibquery.wizard.ciscavate.cjwizard.WizardPage.java

/**
 * Sets the value of a component./*from  w  ww  . j  a  v a  2s.c  o  m*/
 * 
 * @param c The component.
 * @param o The value.
 */
private void setValue(Component c, Object o) {

    if (null == o) {
        // don't set null values
        return;
    }
    if (c instanceof CustomWizardComponent) {
        ((CustomWizardComponent) c).setValue(o);
    } else if (c instanceof JTextComponent) {
        String text = (String) o;
        if (!text.isEmpty()) {
            ((JTextComponent) c).setText((String) o);
        }
    } else if (c instanceof AbstractButton) {
        ((AbstractButton) c).setSelected((Boolean) o);
    } else if (c instanceof JComboBox) {
        ((JComboBox) c).setSelectedItem(o);
    } else if (c instanceof JList) {
        List<Object> items = Arrays.asList((Object[]) o);
        JList list = (JList) c;
        int[] indices = new int[items.size()];
        int i = 0;
        for (int j = 0; j < list.getModel().getSize(); j++) {
            Object e = list.getModel().getElementAt(j);
            if (items.contains(e)) {
                indices[i++] = j;
            }
        }
        list.setSelectedIndices(indices);
    } else {
        log.warn("Unknown component: " + c);
    }

}

From source file:com.github.cjwizard.WizardPage.java

/**
 * Sets the value of a component./*from   ww  w .  jav a 2s  .co  m*/
 * 
 * @param c The component.
 * @param o The value.
 */
private void setValue(Component c, Object o) {

    if (null == o) {
        // don't set null values
        return;
    }
    if (c instanceof CustomWizardComponent) {
        ((CustomWizardComponent) c).setValue(o);
    } else if (c instanceof JFormattedTextField) {
        ((JFormattedTextField) c).setValue(o);
    } else if (c instanceof JTextComponent) {
        String text = (String) o;
        if (!text.isEmpty()) {
            ((JTextComponent) c).setText((String) o);
        }
    } else if (c instanceof AbstractButton) {
        ((AbstractButton) c).setSelected((Boolean) o);
    } else if (c instanceof JComboBox) {
        ((JComboBox) c).setSelectedItem(o);
    } else if (c instanceof JList) {
        List<Object> items = Arrays.asList((Object[]) o);
        JList list = (JList) c;
        int[] indices = new int[items.size()];
        int i = 0;
        for (int j = 0; j < list.getModel().getSize(); j++) {
            Object e = list.getModel().getElementAt(j);
            if (items.contains(e)) {
                indices[i++] = j;
            }
        }
        list.setSelectedIndices(indices);
    } else {
        log.warn("Unknown component: " + c);
    }

}

From source file:FontChooser.java

private void setListValues(JList list, String[] values) {
    if (list.getModel() instanceof DefaultListModel) {
        DefaultListModel model = (DefaultListModel) list.getModel();
        model.removeAllElements();//from w ww. j  a  v  a2s .com
        for (String value : values) {
            model.addElement(value);
        }
    }
}

From source file:edu.ku.brc.af.ui.forms.validation.DataChangeNotifier.java

/**
 * Returns a string value for a control to compare to see if the value has changed.
 * @param component the component to get the value fromn
 * @return Returns a string value for a control to compare to see if the value has changed
 *//*from   w  w w.  ja va  2 s.c  om*/
public String getValueForControl(Component component) {
    //log.debug("DataChangeNotifier - getValueForControl "+component);

    if (component instanceof JTextComponent) {
        return ((JTextComponent) component).getText();

    } else if (component instanceof JToggleButton) {
        return Boolean.toString(((JToggleButton) component).isSelected());

    } else if (component instanceof JComboBox) {
        return ((JComboBox) component).getSelectedItem().toString();

    } else if (component instanceof JList) {
        JList list = (JList) component;
        int inx = list.getSelectedIndex();
        return inx == -1 ? "" : list.getModel().getElementAt(inx).toString();

    } else if (component instanceof JCheckBox) {
        return ((JCheckBox) component).isSelected() ? "true" : "false";

    } else if (component instanceof GetSetValueIFace) {
        Object value = ((GetSetValueIFace) component).getValue();
        return (value != null) ? value.toString() : null;

    } else if (component instanceof MultiStateIconButon) {
        return String.valueOf(((MultiStateIconButon) component).getState());
    } else {
        throw new RuntimeException("Can't get a value for componentonent: " + component);
    }
}