0

I want to add the toStrings from items in an Arraylist of Objects to a Jlist through a for loop. I tried this.

for (int i = 0; i < customer.slist.size(); i++) {
        ((DefaultListModel) checkOutList.getModel()).addElement(customer.slist.toString());//slist is an array of objects.
    }

But it didnt work. Also NetBeans tells me .addElement()is not a method for a JList when I tried

checkOutList.addElement("anything")
2
  • I think you need to set the model again after you add to it... Can't remember, though. Been awhile... Commented Dec 10, 2016 at 22:57
  • @cricket_007, resetting the model defeats the whole point of MVC design. Whenever data in the model is changed, the model is supposed to notify the view that is has changed so the view can repaint itself. Commented Dec 10, 2016 at 23:43

2 Answers 2

1

instead of

customer.slist.toString()

try:

customer.slist.get(i).toString();

in your code.

Sign up to request clarification or add additional context in comments.

Comments

0

I have this implementation:

This is custom JList:

@SuppressWarnings("serial")
public class JListCustomered<T extends JListPresentedInterface> extends JList<T>{

private List<T> list;
private List<T> selectedList;

public JListCustomered(List<T> list){
    super();
    this.list=list;
    setCellRenderer(new JListRenderer());
    setModel(new ListModel());
}

public void loadData(List<T> list){
    getSelectedList().clear();
    this.list=list;
    setModel(new ListModel());
}


/** {@link JListCustomered#list} */
public List<T> getList() {
    if(list==null)
        list=new ArrayList<T>();
    return list;
}

/** {@link JListCustomered#selectedList} */
public List<T> getSelectedList() {
    if(selectedList==null)
        selectedList=new ArrayList<T>();
    return selectedList;
}


public class JListRenderer extends JLabel implements ListCellRenderer<T>{
    JSeparator separator;

    private JListRenderer(){
        separator=new JSeparator(JSeparator.HORIZONTAL);
        setOpaque(true);
    }

    /** @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean) */
    public Component getListCellRendererComponent(JList<? extends T> list, T value, int index, boolean isSelected, boolean cellHasFocus) {
        if(value.isSeparator()){
             return separator;
        }

        List<T> selected = getSelectedList();
        if (isSelected) {
            if (!selected.contains(value)) {
                getSelectedList().add(value);
            }
            this.setBackground(getSelectionBackground());
        } else {
            if (selected.contains(value)) {
                getSelectedList().remove(value);
            }
            setBackground(Color.WHITE);
        }
        setFont(list.getFont());
        setText(value.getJListName());
        return this;
    }
}

private class ListModel implements javax.swing.ListModel<T>{

    /** @see javax.swing.ListModel#getSize() */
    public int getSize() {
        return getList().size();
    }

    /** @see javax.swing.ListModel#getElementAt(int) */
    public T getElementAt(int index) {
        return getList().get(index);
    }

    /** @see javax.swing.ListModel#addListDataListener(javax.swing.event.ListDataListener) */
    public void addListDataListener(ListDataListener l) {
    }

    /** @see javax.swing.ListModel#removeListDataListener(javax.swing.event.ListDataListener) */
    public void removeListDataListener(ListDataListener l) {
    }

}
}

below interface for object, which you want add to JList:

public interface JListPresentedInterface {

public String getJListName();
public boolean isSeparator();
}

you can reaload data on JList using method loadData, get selected list getList

1 Comment

Example of use: private JListCustomered<ColumnType> listColumnsData; listColumnsData = new JListCustomered<ColumnType>(controller.getDataColumns()); List<ColumnType> selectedList = listColumnsData.getSelectedList(); listColumnsData.loadData(columns);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.