Flashcards in Java

Written By: James Williams

- 10 Apr 2006 -

Description: By using Java and XML, this tutorial walks you through creating flashcards for learning French.

  1. Introduction
  2. The User Interface
  3. ActionListener/MouseListeners
  4. Loading the Word List

Loading the Word List

As in JDOM primer, we can load the actual file in only a couple lines of code to load the file but we need a file chooser to select the location on the hard drive. After loading, we load the elements into an array and alter the title of the window.

public void load_list() {
                try {
                        //Create a file chooser to select the file to open
                        JFileChooser chooser = new JFileChooser();
                        int result = chooser.showOpenDialog(this);
                
                        if(result == JFileChooser.APPROVE_OPTION) {
                                File file = chooser.getSelectedFile();
                                SAXBuilder builder = new SAXBuilder();
                                Document document = builder.build(file);
                                
                                Element root = document.getRootElement();
                                Element title = root.getChild("title");
                                
                                //Change name of window to
                                //Flash Cards - WordListName
                                setTitle("Flash Cards - " + title.getAttribute("name").getValue());
                                
                                //get elements and store them in an ArrayList
                                wordList = root.getChildren("entry");
                        }
                }
                catch (JDOMException j) {
                        JOptionPane.showMessageDialog(null, j.getMessage());
                }
                catch (IOException io) {
                        JOptionPane.showMessageDialog(null, io.getMessage());
                }
        }

We're done. That's not to say there are things that might still be useful to add. For example, possibly an auto-advance setting, the ability to load more than one list at a time, etc. But I will leave that for you to figure. Next time, we'll code the helper application to create the word list files.

<< Previous