简体   繁体   English

如何使用txt文件中的某些单词填充JList

[英]How to populate JList with certain words from txt file

I need help with JList . 我需要有关JList帮助。 Need to add a text file to a list but txt file is named library.txt with: 需要向列表中添加文本文件,但txt文件名为library.txt ,其内容为:

title1 author1 description1 publisher1
title2 author2 description2 publisher2
title3 author3 description3 publisher3
title4 author4 description4 publisher4

What program needs to do is to fill list only with titles from txt and when user select certain title from list the program needs to write the description to a JTextArea . 程序需要做的是仅用 txt中的标题填充列表,并且当用户从列表中选择某些标题时,程序需要将描述写入JTextArea

This is what I got so far. 这就是我到目前为止所得到的。

import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextArea;

public class Library extends JFrame {

    private JList lista;
    private JTextArea tekst;
    DefaultListModel model;

    public Library() {
        super("Biblioteka");
        this.setSize(300, 300);
        setLayout(new FlowLayout());
        model = new DefaultListModel();
        lista = new JList(model);
        add(lista);
        tekst = new JTextArea(20, 20);
        add(tekst);
        File fajl = new File("library.txt");
        BufferedReader ulaz = null;
        try {
            FileReader fr = new FileReader(fajl);
            ulaz = new BufferedReader(fr);
            String linija;
            try {
                while ((linija = ulaz.readLine()) != null) {
                    //lista.add(linija);
                    //System.out.println(linija);
                    String[] reci = linija.split("\t");
                    String naslovi = null;
                    for (int i = 0; i < reci.length; i++) {
                        naslovi = reci[0];
                    }
                    int pos = lista.getModel().getSize();
                    model.addElement(naslovi.toString());
                }
            } catch (IOException ex) {
                Logger.getLogger(Library.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Library.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        Library l = new Library();
        l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        l.setSize(300, 430);
        l.setVisible(true);
    }
}

As Andrew mentioned in his comments use a POJO (plain old java object) like 如安德鲁(Andrew)在其评论中所述,使用POJO(普通的旧Java对象)

class Book {
    String title;
    String author;
    String description:
    /* other fields*/
}

Then rewrite your loop similar to 然后像这样重写循环

while ((linija = ulaz.readLine()) != null) {
    String[] reci = linija.split("\t");

    for (int i = 0; i < reci.length; i++) {
         Book book = new Book();
         book.title = reci[0];
         book.description = reci[2];
         /* other stuff here */
         model.addElement(book);
     }
}

Use then a ListCellRenderer to display only the title in the List and a ListSelectionListener to listen and update the text area accordingly 然后使用ListCellRenderer仅在List显示标题,并使用ListSelectionListener地侦听和更新文本区域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM