How List<t> binding to JList?
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> ();
精彩评论