开发者

How List<t> binding to JList?

开发者 https://www.devze.com 2023-03-05 00:24 出处:网络
I have Jlist. I need binding info from List开发者_JAVA技巧<MyType> Mytype public class MyType {

I have Jlist. I need binding info from List开发者_JAVA技巧<MyType>

Mytype

public class MyType {

    String _title = "";
    String _description = "";
}


You need to bind your data to JList data model (ListModel).


If you mean binding as "if I update my List the JList will also be updated and vice-versa" you are looking for something like: Beansbinding or Glazed Lists. If you just want to display every object of List<MyType> as an item in the JList, override MyType toString method and write some bad code such as this:

  List<MyType> list = ...
  JList jList = new JList(list.toArray());

Or implement your own ListModel. I would definitely advice you to go with the libraries instead of the reiventing the wheel approach.


//First Create Get and Set Methods
public class MyType {
    String title = "";
    String description = "";

    public String getDescription() {
        return _description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

//Then set to List    
List<MyType> tmpList = new ArrayList<MyType>();

MyType tmpMyType = new MyType();
tmpMyType.setTitle("Maths");
tmpMyType.setDescription("Algibra");

tmpList.add(tmpMyType);


I am not quite sure if I understood your question, but are you looking for:

JList <MyType> myList = new JList <MyType> ();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号