[英]Using information from object selected in jList
我很难从jList中获取信息,因此单击按钮时可以将其用于创建其他类中的对象,
private void jButtonAddOrderActionPerformed(java.awt.event.ActionEvent evt) {
int noCopies;
String title, Name;
noCopies = Integer.parseInt(jTextFieldCopies.getText());
title = Book.bookInstances.get(jListPubBooks.getSelectedIndex()).getName();
Name = Book.bookInstances.get(jListPubBooks.getSelectedIndex()).getPublisherName();
new Order(noCopies, title, Name);
setjlistmodel(Order.orderItem);
我确信我的setjlistmodel方法没有问题,因为当仅从文本字段获取信息时,这在程序的其他地方也有效。 我认为我的问题在于这两行:
title = Book.bookInstances.get(jListPubBooks.getSelectedIndex()).getName();
Name = Book.bookInstances.get(jListPubBooks.getSelectedIndex()).getPublisherName();
}
这是我的订单类;
package bookstore;
import java.util.ArrayList;
public class Order {
int noOfBooks;
String bookTitle;
String pubName;
public static ArrayList<Order> orderItem = new ArrayList<>();
ArrayList<ArrayList<Order>> Order = new ArrayList<>();
public Order(int noBooks, String Title, String Name)
{
this.noOfBooks = noBooks;
this.bookTitle = Title;
this.pubName = Name;
orderItem.add(this);
}
public void addOrder(ArrayList ord)
{
Order.add(ord);
}
public int getNoBooks()
{
return noOfBooks;
}
public String getBookTitle()
{
return bookTitle;
}
public String getPubName()
{
return pubName;
}
}
setjlistmodel方法:
private void setjlistmodel(ArrayList<Order> orderInstances){
DefaultListModel OrderList = new DefaultListModel();
for(int i = 0; i<=OrderList.size()-1;i++){
OrderList.addElement(orderInstances.get(i).getNoBooks());
System.out.println(orderInstances.get(i).getBookTitle());
System.out.println(OrderList.firstElement());
}
jListOrder.setModel(OrderList);
}
问题是单击该按钮时jListOrder中没有显示任何内容。 我不认为该订单被添加到orderItem ArrayList。
“问题在于单击按钮时jListOrder中没有显示任何内容。我不认为该订单已添加到orderItem ArrayList中。”
我认为添加orderItem
很好。
首次初始化时, OrderList
大小为零,这意味着循环绝对不执行任何操作
DefaultListModel OrderList = new DefaultListModel();
for(int i = 0; i <= OrderList.size() - 1; i++)
你可能想要
for(int i = 0; i <= orderInstances.size() - 1; i++)
这是使用ArrayList
大小。
附带说明,请用空格分隔运算符。 它使阅读更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.