我正在尝试从数组中添加一些元素。我迷失了java API。它必须采用这种格式,以便我可以按顺序进行操作。
listOfItem = new JList();
for (int i = 0; i < car.length; ++i) {
listOfItem.add(car[i].getId() + ": ", car[i].getDescription(),
(car[i].getPrice()), car[i].getQuantity());
}
最佳答案
您不能直接向 JList 添加元素。使用 ListModel
为您的 JList
提供数据:
listOfItem = new JList();
// create list model
DefaultListModel<String> listModel = new DefaultListModel<String>();
for (int i = 0; i < car.length; ++i) {
listModel.addElement(car[i].getId() + ": " + car[i].getDescription() + ", " +
car[i].getPrice() + ", " + car[i].getQuantity();
}
// register model so that the list shows the data of the model
listOfItem.setModel(listModel);
关于java - Swing JList 和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40905941/