Java 如何列出<;t>;绑定到JList?
我有一份清单。我需要Java 如何列出<;t>;绑定到JList?,java,jlist,Java,Jlist,我有一份清单。我需要列表中的绑定信息 Mytype public class MyType { String _title = ""; String _description = ""; } 我不确定我是否理解你的问题,但你是否在寻找: JList <MyType> myList = new JList <MyType> (); JList myList=new JList(); 您需要将数据绑定到JList数据模型(ListModel)。如果您的意
列表中的绑定信息
Mytype
public class MyType {
String _title = "";
String _description = "";
}
我不确定我是否理解你的问题,但你是否在寻找:
JList <MyType> myList = new JList <MyType> ();
JList myList=new JList();
您需要将数据绑定到JList
数据模型(ListModel
)。如果您的意思是绑定为“如果我更新我的列表,JList也会更新,反之亦然”,则您正在寻找类似于:或的内容。如果您只想将列表
中的每个对象显示为JList
中的一个项目,请重写MyType
到字符串
方法,并写入一些错误代码,例如:
List<MyType> list = ...
JList jList = new JList(list.toArray());
List=。。。
JList JList=新JList(list.toArray());
或者实现你自己的。我肯定会建议您使用这些库,而不是重新使用轮子方法。//首先创建Get和Set方法
//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);
公共类MyType{
字符串标题=”;
字符串说明=”;
公共字符串getDescription(){
返回描述;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
}
//然后设置为列表
List tmpList=new ArrayList();
MyType tmpMyType=新的MyType();
tmpMyType.setTitle(“数学”);
tmpMyType.setDescription(“Algibra”);
tmpList.add(tmpMyType);
对于JList,请定义自己的列表模型(可能继承自DefaultListModel),以处理您的数据类型。将自定义单元渲染器(实现ListCellRenderer)添加到JList中,以定义数据集的显示方式。这真管用!因为我在java.net上找不到原始Beansbinding jar的下载链接。下面是一个更活跃的fork:D的链接