Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要列表选择侦听器的帮助吗_Java_Textfield_Jlist - Fatal编程技术网

Java 需要列表选择侦听器的帮助吗

Java 需要列表选择侦听器的帮助吗,java,textfield,jlist,Java,Textfield,Jlist,我试图用代码填充我的列表选择侦听器方法,但我不知道如何实现它。基本上,我希望该方法在从JList中选择一个项目时填充多个文本字段 我的JList如下所示: private JList <String> contactList; private DefaultListModel<String> model; //Textfields comboBookType = new JComboBox(BookType.values()); nameText = new JText

我试图用代码填充我的列表选择侦听器方法,但我不知道如何实现它。基本上,我希望该方法在从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 JList联系人列表;
私有模型;
//文本字段
comboBookType=newJComboBox(BookType.values());
nameText=新的JTextField(“,17);
authorText=newjtextfield(“,17);
genreText=新的JTextField(“,17);
comboCategory=newJComboBox(readCategory());
//初始化defaultlistmodel和jlist
model=新的DefaultListModel();
bookList=新JList(型号);
bookList.setVisibleRowCount(10);
bookList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
bookList.setFixedCellHeight(20);
bookList.setFixedCellWidth(130);
bookList.setboorder(BorderFactory.createLineBorder(Color.BLACK,1));
JPanel left=newjpanel(newborderlayout());
left.add(新的JScrollPane(图书列表),BorderLayout.NORTH);
bookList.addListSelectionListener(新建ListSelection());
//填充Jlist
类openFileListener实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
book.getBooks();
for(字符串名称:addressBook.getBooks())
{
型号.附录(名称);
}
}
}
//my Library类中的getBooks()方法
公共ArrayList getBooks()
{
文件;
扫描仪输入;
书籍类型;
ArrayList书籍;
ArrayList name=新的ArrayList();
尝试
{
文件=新文件(“Books.txt”);
输入=新扫描仪(文件);
while(input.hasNext())
{
String line=input.nextLine();
String[]book=line.split(“,”);
类型=BookType.valueOf(信息[0]);
字符串名称=书本[1];
b=新书(类型、书[1]、书[2]、书[3]、书[4]、信息[5]);
表1.2(b);
information.add(名称);
numOfBooks++;
}
input.close();
}
catch(filenotfounde异常)
{
JOptionPane.showMessageDialog(null,“未找到文件”);
}
返回信息;
}
以下是迄今为止我在列表选择侦听器中的内容:

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;
私有类ListSelection实现ListSelectionListener
{
public void值已更改(ListSelectionEvent e)
{
book.getInfo();
int index=bookList.getSelectedIndex();
}
//库类中的getInfo()方法
公共ArrayList getInfo()
{
文件;
扫描仪输入;
书籍类型;
ArrayList书籍;
尝试
{
文件=新文件(“Book.txt”);
输入=新扫描仪(文件);
while(input.hasNext())
{
String line=input.nextLine();
String[]info=line.split(“,”);
类型=BookType.valueOf(信息[0]);
字符串名称=信息[1];
Book b=新联系人(类型、信息[1]、信息[2]、信息[3]、信息[4]、信息[5]);
表1.2(b);
}
input.close();
}
catch(filenotfounde异常)
{
JOptionPane.showMessageDialog(null,“未找到文件”);
}
退货清单;
这不多,但我不知道从这里走到哪里。我知道我必须利用从
getSelectedIndex
获得的
索引,但我不知道如何使用,请帮助,谢谢

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

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

String book = model.getElementAt(index);
或者更简单的方法是使用
getSelectedValue()
方法从
JList
获取元素:

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;
    }
}
ArrayList bookInfo=getInfo();
用于(书籍:bookInfo)
{
if(book.equals(book.getTitle())
{
authorTextField.setText(book.getAuthor());
//…对于其余的文本字段
返回;
}
}

注:更好的设计是在构建类时将图书信息读入类中。这种方法不必每次在JList中进行选择时都从文本文件中读取数据。

不要使用“代码片段”标记。选择代码并使用{}
按钮。@camickr谢谢,从现在开始我会这么做。只是好奇,为什么我要在“代码片段”上使用
{}
?谢谢你给我解释!现在开始设置组合框。