好的,所以我正在尝试创建两个单独的JScrollPanes,它们有两个列表;一个用户列表和一个书籍列表,只是简单地呈现它们。问题是,现在,我的“bookData”-array中有19个项目,而我的“userData”数组中有15个 - 但是我的JScrollPanes都和我的钱包一样空。
使用getSize()检查我的DefaultListModels时,我发现它们的大小也是15和19。我觉得我在这里缺少一些至关重要的东西。百万美元的问题是什么?
!! - 编辑:
刚刚注意到ODDEST的行为!
我的列表不是空的 - 这是窗口在开始时的样子;
http://i60.tinypic.com/2nr0kyu.png
但是 - 这就是扭曲!一旦我通过拖动角落改变窗户的大小 - 我的LISTS出现了!因此,错误不在列表中,而是在图形中,即methinks。 :3
http://i61.tinypic.com/2ah7ac3.png
这是我的方法的样子;
public void newLoan(){
StringBuilder sb = new StringBuilder();
StringBuilder string = new StringBuilder();
JLabel userLabel;
JLabel bookLabel;
JButton btnRegister;
// Build a string of all users, separated with a "–"
for (int i = 1; i < Processes.userList.size()+1; i++) {
sb.append(Processes.userList.get(i).getFirstname()+" "+Processes.
userList.get(i).getSurname()+", "+Processes.userList.
get(i).getPersonalID()+"–");
System.out.println(Processes.userList.get(i).getFirstname()+" "+Processes.
userList.get(i).getSurname()+", "+Processes.userList.
get(i).getPersonalID());
}
// Build a string of all books, separated with a "–"
for (int i = 1; i < Processes.bookList.size()+1; i++) {
if (Processes.bookList.get(i).getAvailable()-
Processes.bookList.get(i).getOnLoan()!=0) {
string.append(Processes.bookList.get(i).getAvailable()-
Processes.bookList.get(i).getOnLoan()+" available - "+
Processes.bookList.get(i).getTitle()+" by "+Processes.
bookList.get(i).getAuthor_firstname()+" "+Processes.
bookList.get(i).getAuthor_surname()+" ("+Processes.
bookList.get(i).getIsbn()+")–");
System.out.println(Processes.bookList.get(i).getAvailable()-
Processes.bookList.get(i).getOnLoan()+" available - "+
Processes.bookList.get(i).getTitle()+" by "+Processes.
bookList.get(i).getAuthor_firstname()+" "+Processes.
bookList.get(i).getAuthor_surname()+" ("+Processes.
bookList.get(i).getIsbn());
}
}
// split sb at the "–"'s and fill an array.
String[] userData = sb.toString().split("–");
System.out.println(userData.length);
// split string at the "–"'s and fill an array.
String[] bookData = string.toString().split("–");
System.out.println(bookData.length);
/* Defining sizes and locations and initializing all variables. Also
* defining text on buttons and labels.*/
DefaultListModel userModel = new DefaultListModel();
for (int i = 0; i < userData.length; i++) {
userModel.addElement(userData[i]);
}
System.out.println(userModel.getSize());
final JList userJList = new JList();
userJList.setModel(userModel);
JScrollPane userList = new JScrollPane(); //f*cking JScrollPane! Work!
userList.setViewportView(userJList);
DefaultListModel bookModel = new DefaultListModel();
for (int i = 0; i < bookData.length; i++) {
bookModel.addElement(bookData[i]);
}
System.out.println(bookModel.getSize());
final JList bookJList = new JList();
bookJList.setModel(bookModel);
JScrollPane bookList = new JScrollPane();
bookList.setViewportView(bookJList);
userLabel = new JLabel("Select user to register loan onto:");
userLabel.setSize(250,20);
userLabel.setLocation(20, 50);
userList.setSize(150, 200);
userList.setLocation(70, 80);
bookList.setSize(150, 200);
bookList.setLocation(400, 80);
btnRegister = new JButton("Register loan");
btnRegister.setSize(150,50);
btnRegister.setLocation(235, 300);
// Adding functionality... Later
// Adding the different components to the panel "newLoan".
newLoan.add(userLabel);
newLoan.add(userList);
newLoan.add(bookList);
newLoan.add(btnRegister);
}
我的第一个想法是简单地使用我的String数组并将它们呈现在JScrollPanes中;因此;
final JList userJList = new JList(userData);
JScrollPane userList = new JScrollPane();
userList.setViewportView(userJList);
但这显然不起作用。
答案 0 :(得分:0)
解决方案非常复杂。在方法之外为每个列表添加了私有 DefaultListmodel ,并将所有内容作为元素添加到这些模型中。最后,但并非最不重要的是,我重新使用了JPanel;
newLoan.repaint();