Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Help me populating JList from a txt file with a certain words

  1. #1
    labyrinthman is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Help me populating JList from a txt file with a certain words

    Hi, I need help with JList.
    Need to add a text file to a JList but txt file is named "library.txt" with:

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

    What program needs to do is to fill JList ONLY with titles from txt and when user select certain title from JList the program needs to write the description to a JTextArea.
    This is what I got so far

    Java Code:
    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);
        }
    }
    Last edited by Fubarable; 05-16-2012 at 02:02 AM. Reason: moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me populating JList from a txt file with a certain words

    Suggestions:
    • First and formost I would create a Book class, one that has fields for the title, author, description and publisher.
    • I'd create a DefaultListModel<Book> object.
    • I'd read the data from the text file and create a Book object from each row of data read in, and then adding the newly created object into my list model that was created above.
    • I'd create a JList that uses the list model created above.
    • I'd create a ListCellRenderer<Book> for the JList above, that displays only the one field that you want displayed in your JList -- for you, you would want it to display only the title.
    • Then when I detected and retrieved the List item was selected, I'd have a complete Book object with all the information that I need to display it in a JTextField or JTextArea.


    If you have any questions about these recommendations or if you try it and get stuck in a part of your code, please let me know.

    Also note that I added [code] [/code] tags to your post above.
    Tolls likes this.

  3. #3
    labyrinthman is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help me populating JList from a txt file with a certain words

    Quote Originally Posted by Fubarable View Post
    Suggestions:
    • I'd create a DefaultListModel<Book> object.
    • I'd read the data from the text file and create a Book object from each row of data read in, and then adding the newly created object into my list model that was created above.


    If you have any questions about these recommendations or if you try it and get stuck in a part of your code, please let me know.
    I don't know how do you mean to create DefaultListModel<Book>, when I acctually write DefaultListModel<Book> I get an error. Do you mean
    Java Code:
    DefaultListModel Book;
    or
    acctually DefaultListModel<Book> ?

    And how to read each line into Book object ?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me populating JList from a txt file with a certain words

    It is actually DefaultListModel<Book> if you are using Java 7, since Java 7 allows for generics of this type. If not, then you'll have to use just a DefaultListModel object. Create an object of this type.

    As to how to add items to the model, again, please look at the API as it will give you a method to use.

Similar Threads

  1. Populating a combobox from a text file.
    By Mike. in forum New To Java
    Replies: 1
    Last Post: 05-12-2012, 04:35 AM
  2. counting words within a file
    By justOne in forum New To Java
    Replies: 18
    Last Post: 07-25-2011, 02:15 PM
  3. Populating a JList from a text file - Netbeans
    By Ben1 in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 03:30 PM
  4. Finding words (and more) in another file
    By Lund01 in forum Java Applets
    Replies: 5
    Last Post: 09-30-2010, 01:03 PM
  5. populating a JList
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 09-30-2010, 03:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •