需要有关列表选择侦听器的帮助

时间:2015-05-06 11:27:27

标签: java textfield jlist

我试图用代码填充我的列表选择监听器方法,但我不知道如何处理它。基本上,我希望该方法在从JList中选择一个项目时填充多个文本字段。

我的JList看起来像这样:

private JList <String> contactList;
private DefaultListModel<String> model;

//Textfields
comboBookType = new JComboBox(BookType.values());
nameText = new JTextField("", 17);   
authorText = new JTextField("", 17);   
genreText = new JTextField ("", 17);
comboCategory = new JComboBox (readCategory());

//initialize defaultlistmodel and jlist
model = new DefaultListModel <String>();
bookList = new JList (model);
bookList.setVisibleRowCount(10);
bookList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
bookList.setFixedCellHeight (20);
bookList.setFixedCellWidth (130);
bookList.setBorder (BorderFactory.createLineBorder(Color.BLACK,1));
JPanel left = new JPanel(new BorderLayout());
left.add (new JScrollPane(bookList), BorderLayout.NORTH);
bookList.addListSelectionListener (new ListSelection());

//populating Jlist
class openFileListener implements ActionListener
{

    public void actionPerformed(ActionEvent e) 
    {

      book.getBooks();
      for (String name : addressBook.getBooks())
      {
        model.addElement(name);
      }
    }
}

//the getBooks() method in my Library class 
public ArrayList<String> getBooks ()
{
  File file;
  Scanner input;
  BookType type;
  ArrayList<String> books;
  ArrayList<String> names = new ArrayList<String>();
  try
  {
    file = new File ("Books.txt");
    input = new Scanner (file);     

    while (input.hasNext())
    {

      String line = input.nextLine ( );
      String [] book = line.split(",");
      type = BookType.valueOf(info[0]);
      String name = book[1];
      b = new Book (type, book[1], book[2], book[3], book[4], info[5]);
      list.add (b);
      information.add (name);

      numOfBooks++;
    }
    input.close ( );
  }
  catch (FileNotFoundException e)
  {
    JOptionPane.showMessageDialog (null, "File not found");
  }
  return information;
}

这是我目前在列表选择监听器中所拥有的内容:

private class ListSelection implements ListSelectionListener
{
  public void valueChanged (ListSelectionEvent e) 
  {
    book.getInfo ( );
    int index = bookList.getSelectedIndex ( );
}


//getInfo() method in Library class

public ArrayList<Book> getInfo ()
{
  File file;
  Scanner input;
  BookType type;
  ArrayList<String> book;
  try
  {
    file = new File ("Book.txt");
    input = new Scanner (file);     

    while (input.hasNext())
    {
      String line = input.nextLine ( );
      String [] info = line.split(",");
      type = BookType.valueOf(info[0]);
      String name = info[1];
      Book b = new Contact (type, info[1], info[2], info[3], info[4],info[5]);
      list.add (b);
    }
    input.close ( );
  }
  catch (FileNotFoundException e)
  {
  JOptionPane.showMessageDialog (null, "File not found");
  }
  return list;

它并不多,但我不知道从哪里开始。我知道我必须使用index来自getSelectedIndex的{​​{1}},但我不知道如何,请帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

  

我知道我必须利用我从getSelectedIndex获得的索引,但我不知道如何

您可以使用索引从ListModel获取选择项:

String book = model.getElementAt(index);

或者更简单的方法是使用JList方法从getSelectedValue()获取元素:

private class ListSelection implements ListSelectionListener
{
    public void valueChanged (ListSelectionEvent e) 
    {
        if (e.getValueIsAdjusting()) return;

        //book.getInfo ( );
        //int index = bookList.getSelectedIndex ( );
        String book = bookList.getSelectedValue();
  }
}

现在您遇到的问题是找到有关该书的信息。您正在创建Book个对象并将它们添加到ArrayList。所以现在你需要创建一个循环来查看ArrayList中的所有条目。类似的东西:

ArrayList<Book> bookInfo = getInfo();

for (Book book: bookInfo)
{
    if (book.equals(book.getTitle())
    {
        authorTextField.setText( book.getAuthor());
        // ... for the rest of the text fields
        return;
    }
}

请注意,更好的设计是在构建类时将书信息读入您的班级。这种方式在每次在JList中进行选择时都不会从文本文件中读取数据。