[SWING] Help in implementing AbstractListModel

H

Hole

Hi there,

I would like to use my collections (Map<String, MyClass> or
List<MyClass>) directly to fill the lists (JList) I have in my swing
application.

So, how to implement get and addElement methods for a custom ListModel
(extending AbstractListModel) and how to write a proper
ListCellRenderer? What else you need to do to have a JList with a
model that deal with your collections in a direct way?

This could help me in adding and removing elements in an efficent
manner (no duplicates of objects, you can add your objects to the
ListModel without dealing with indexes and native types).

I tried to do it but lists are being showed as empty, even if the
collections holded in the ListModel are correctly filled.

Some code:

<code>
//the renderer
public class VariableGroupsCellRenderer extends JLabel implements
ListCellRenderer {
//
private static final Color HIGHLIGHT_COLOR = new Color(0,0,128);
public VariableGroupsCellRenderer() {
setOpaque(true);

}
public Component getListCellRendererComponent(JList list, Object
value, int index, boolean isSelected, boolean cellHasFocus) {
VariableGroup var = (VariableGroup)value;
setText(var.getVariableGroupName()+ " "+var.getTiming());
if (isSelected) {
setBackground(HIGHLIGHT_COLOR);
setForeground(Color.white);
}
else {
setBackground(Color.white);
setForeground(Color.black);
}
return this;

}
//
}
</code>

<code>
//the ListModel
//the code smells since I modified it to try to make it work (adding
an array of objects called data....)
public class VariableGroupListModel extends AbstractListModel {


private Map<String,VariableGroup> map = new
HashMap<String,VariableGroup>();
private VariableGroup[] data = {};

public void setItems(Map<String,VariableGroup> map) {
this.map = map;
setData();
}
//
public int getSize() {
return data.length;
}
//
public VariableGroup getElement(String name) {
return map.get(name);
}
//
public void addElement(VariableGroup var) {
map.put(var.getVariableGroupName(), var);
setData();
}
//
public void removeElement(String name) {
map.remove(name);
setData();
}
public void removeElement(int index) {
VariableGroup[] newData = new VariableGroup[data.length -1];

System.arraycopy(data, 0, newData, 0, index);
if (data.length != index) {
System.arraycopy(data, index + 1, newData, index,
data.length - index - 1);
}

data = newData;
map = null;
for (VariableGroup var: data) {
map.put(var.getVariableGroupName(), var);
}
}

public void remove(int index) {
removeElement(index);
}
//
public VariableGroup getElementAt(int index) {
return get(index);
}
//
public VariableGroup get(int index) {
return data[index];
}

private void setData() {

data = (VariableGroup[])map.values().toArray(data);
}


}
</code>
 
H

Hole

Hi there,

I would like to use my collections (Map<String, MyClass> or
List<MyClass>) directly to fill the lists (JList) I have in my swing
application.

So, how to implement get and addElement methods for a custom ListModel
(extending AbstractListModel) and how to write a proper
ListCellRenderer? What else you need to do to have a JList with a
model that deal with your collections in a direct way?

This could help me in adding and removing elements in an efficent
manner (no duplicates of objects, you can add your objects to the
ListModel without dealing with indexes and native types).

I tried to do it but lists are being showed as empty, even if the
collections holded in the ListModel are correctly filled.

Some code:

<code>
//the renderer
public class VariableGroupsCellRenderer extends JLabel implements
ListCellRenderer {
//
    private static final Color HIGHLIGHT_COLOR = new Color(0,0,128);
    public VariableGroupsCellRenderer() {
        setOpaque(true);

    }
    public Component getListCellRendererComponent(JList list, Object
value, int index, boolean isSelected, boolean cellHasFocus) {
        VariableGroup var = (VariableGroup)value;
        setText(var.getVariableGroupName()+ " "+var.getTiming());
        if (isSelected) {
            setBackground(HIGHLIGHT_COLOR);
            setForeground(Color.white);
        }
        else {
            setBackground(Color.white);
            setForeground(Color.black);
        }
        return this;

    }
//}

</code>

<code>
//the ListModel
//the code smells since I modified it to try to make it work (adding
an array of objects called data....)
public class VariableGroupListModel extends AbstractListModel {

    private Map<String,VariableGroup> map = new
HashMap<String,VariableGroup>();
    private VariableGroup[] data = {};

    public void setItems(Map<String,VariableGroup> map) {
        this.map = map;
        setData();
    }
//
    public int getSize() {
        return data.length;
    }
//
    public VariableGroup getElement(String name) {
        return map.get(name);
    }
//
    public void addElement(VariableGroup var) {
        map.put(var.getVariableGroupName(), var);
        setData();
    }
//
    public void removeElement(String name) {
        map.remove(name);
        setData();
    }
    public void removeElement(int index) {
        VariableGroup[] newData = new VariableGroup[data.length -1];

        System.arraycopy(data, 0, newData, 0, index);
        if (data.length != index) {
            System.arraycopy(data, index + 1, newData, index,
data.length - index - 1);
        }

        data = newData;
        map = null;
        for (VariableGroup var: data) {
            map.put(var.getVariableGroupName(), var);
        }
    }

    public void remove(int index) {
        removeElement(index);
    }
//
    public VariableGroup getElementAt(int index) {
        return get(index);
    }
//
    public VariableGroup get(int index) {
        return data[index];
    }

    private void setData() {

        data = (VariableGroup[])map.values().toArray(data);
    }

}

</code>

Sorry,

I've just solved. You simply need to call "fire changes" methods that
are already implemented in the AbstractListModel class.

Here the new code:

<code>
public class VariableGroupListModel extends AbstractListModel {


private Map<String,VariableGroup> map = new
HashMap<String,VariableGroup>();
private Vector<VariableGroup> data = new Vector<VariableGroup>();

public void setItems(Map<String,VariableGroup> map) {
this.map = map;
this.data.addAll(map.values());

}
//
public int getSize() {
return data.size();
}
//
public VariableGroup getElement(String name) {
return map.get(name);
}
//
public void addElement(VariableGroup var) {
map.put(var.getVariableGroupName(), var);
data.add(var);
int index = data.indexOf(var);
//fireChange
fireContentsChanged(this, index, index);
}
//
public void removeElement(String name) {
VariableGroup var = map.get(name);
map.remove(name);
int idx = data.indexOf(var);
this.removeElement(idx);
}
public void removeElement(int index) {
data.remove(index);
//fireChange
fireIntervalRemoved(this, index, index);
}

public void remove(int index) {
removeElement(index);
}
//
public VariableGroup getElementAt(int index) {
return get(index);
}
//
public VariableGroup get(int index) {
return data.elementAt(index);
}
}

</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top