[英]ArrayList into JList using MVC
I have 3 classes. 我有3节课。
ImpAppModel: ImpAppModel:
/* String array for saving members in the friendlist*/
public ArrayList<String> friendList = new ArrayList(10);
/**
* Method for retrieving elements (added friends) in the array for use in
* GUI.
* @return the elements in the ArrayList
*/
public ArrayList friendList() {
//method filling the array with 1 testing record
friendList.add(1, "petr");
return friendList;
}
I have viewing appPanel class (generated by fellow NetBeans GUI Builder): 我正在查看appPanel类(由NetBeans GUI Builder生成):
Users.setModel(new javax.swing.AbstractListModel() {
String[] strings = {"User1", "User2", "User3", "User4", "User5"};
@Override
public int getSize() {
return strings.length;
}
@Override
public Object getElementAt(int i) {
return strings[i];
}
});
/**
* Method for setting users to display in GUI (variable JList Users)
* @param user parameter for supplying JList
*/
public void setUser(JList user){
this.Users = user;
}
And finally I have controlling ImpAppContorller class: 最后,我可以控制ImpAppContorller类:
private final GuiPanel appPanel;
private final ImpAppModel impAppModel;
/**
* Main constructor method, creates variables for saving links on Data and
* GUI.
* @param appPanel Ensures communication between GUI panel and controller.
* @param impAppModel Ensures communication between Model and controller.
*/
public ImpAppController(GuiPanel appPanel, ImpAppModel impAppModel) {
this.appPanel = appPanel;
this.impAppModel = impAppModel;
appPanel.setUser(impAppModel.friendList.toArray());
}
And I have a Error: incopatible types: Object[] cannot be converted to Jlist. 而且我有一个错误:不兼容的类型:Object []无法转换为Jlist。
The question is (And yes, I did my research, the solutions I found weren't suitable for use in MVC pattern) how do I implement controller (or modify model/view) to supply JList with elements from arrayList using the controller class while maintaining the MVC pattern. 问题是(是的,我进行了研究,发现的解决方案不适合在MVC模式中使用)如何实现控制器(或修改模型/视图)以使用controller类在JList中提供arrayList中的元素维护MVC模式。
/Edit: I have heavy suspision that my problems are caused by setUser method in GUI class, but the question remains the same. /编辑:我非常怀疑我的问题是由GUI类中的setUser方法引起的,但问题仍然相同。
A JList
contains its data in the form of a ListModel
. JList
包含ListModel
形式的数据。 Define the data for your Jlist with the setModel()
member. 使用setModel()
成员为Jlist定义数据。
It's obviously not meaningful to cast an array to a Model object but there is a convenient class DefaultListModel that you can use to import an array to the model. 将数组强制转换为Model对象显然没有意义,但是有一个方便的类DefaultListModel可用于将数组导入模型。 So in your appPanel class you can add 因此,在您的appPanel类中,您可以添加
public void setUserData(Object [] data){
DefaultListModel model = new DefaultListModel();
model.copyInto(data);
Users.setModel(model); // Users must exist
}
appPanel.setUser(JList user)
method accepts object of type JList
whereas in appPanel.setUser(impAppModel.friendList.toArray());
appPanel.setUser(JList user)
方法接受JList
类型的对象,而在appPanel.setUser(impAppModel.friendList.toArray());
you are passing an array type. 您正在传递数组类型。
You should do something like appPanel.setUser(new JList(impAppModel.friendList.toArray()));
您应该执行类似appPanel.setUser(new JList(impAppModel.friendList.toArray()));
Or provide an overloaded setUser(String[] arr)
method in AppPanel class which takes an array and create JList object internally. 或在AppPanel类中提供一个重载的setUser(String[] arr)
方法,该方法采用数组并在内部创建JList对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.