[英]Using information from object selected in jList
I am having difficulty getting information from a jList so that it can be used to create an object in a different class when a button is clicked, 我很难从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);
I am sure there are no problems with my setjlistmodel method as this works elsewhere in my program when only getting information from text fields. 我确信我的setjlistmodel方法没有问题,因为当仅从文本字段获取信息时,这在程序的其他地方也有效。 I think my problem is with these two lines:
我认为我的问题在于这两行:
title = Book.bookInstances.get(jListPubBooks.getSelectedIndex()).getName();
Name = Book.bookInstances.get(jListPubBooks.getSelectedIndex()).getPublisherName();
}
This is my order class; 这是我的订单类;
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 method: 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);
}
The problem is that it is not displaying anything in jListOrder when the button is clicked. 问题是单击该按钮时jListOrder中没有显示任何内容。 I don't think the Order is being added to the orderItem ArrayList.
我不认为该订单被添加到orderItem ArrayList。
"The problem is that it is not displaying anything in jListOrder when the button is clicked. I don't think the Order is being added to the orderItem ArrayList."
“问题在于单击按钮时jListOrder中没有显示任何内容。我不认为该订单已添加到orderItem ArrayList中。”
I think adding an orderItem
is fine. 我认为添加
orderItem
很好。
OrderList
size is zero when you first initialize it, which means your loops does absolutely nothing 首次初始化时,
OrderList
大小为零,这意味着循环绝对不执行任何操作
DefaultListModel OrderList = new DefaultListModel();
for(int i = 0; i <= OrderList.size() - 1; i++)
You probably want 你可能想要
for(int i = 0; i <= orderInstances.size() - 1; i++)
Which is using the ArrayList
size. 这是使用
ArrayList
大小。
As a side note, please separate operators with space. 附带说明,请用空格分隔运算符。 It makes it easier to read.
它使阅读更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.