Reputation: 189
I have 3 classes.
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):
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:
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.
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.
/Edit: I have heavy suspision that my problems are caused by setUser method in GUI class, but the question remains the same.
Upvotes: 0
Views: 375
Reputation: 14705
A JList
contains its data in the form of a ListModel
. Define the data for your Jlist with the setModel()
member.
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. So in your appPanel class you can add
public void setUserData(Object [] data){
DefaultListModel model = new DefaultListModel();
model.copyInto(data);
Users.setModel(model); // Users must exist
}
Upvotes: 1
Reputation: 10987
appPanel.setUser(JList user)
method accepts object of type JList
whereas in appPanel.setUser(impAppModel.friendList.toArray());
you are passing an array type.
You should do something like 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.
Upvotes: 0