[SWING] Help in implementing AbstractListModel

Discussion in 'Java' started by Hole, Oct 9, 2009.

  1. Hole

    Hole Guest

    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>
    Hole, Oct 9, 2009
    #1
    1. Advertising

  2. Hole

    Hole Guest

    Re: Help in implementing AbstractListModel

    On Oct 9, 11:29 am, Hole <> wrote:
    > 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>
    Hole, Oct 9, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    488
    Pete Barrett
    Apr 16, 2005
  2. mkrause
    Replies:
    0
    Views:
    661
    mkrause
    May 6, 2005
  3. lizard
    Replies:
    0
    Views:
    1,750
    lizard
    Jan 30, 2006
  4. Timasmith
    Replies:
    0
    Views:
    388
    Timasmith
    Dec 2, 2006
  5. S.T
    Replies:
    2
    Views:
    565
Loading...

Share This Page