Wednesday, January 28, 2015

Word and Dictionary Example

Here is the code for the WordClass

package com.spconger.DictionaryProgram;

public class Word {
 /*************************
  * this class store the content
  * of a word and definition
  * It has a toString() method
  * that concatenates the word and
  * a definition with a dash between
  */
 //private fields
 private String word;
 private String definition;
 
 public String getWord() {
  return word;
 }
 public void setWord(String word) {
  this.word = word;
 }
 public String getDefinition() {
  return definition;
 }
 public void setDefinition(String definition) {
  this.definition = definition;
 }
 
 //this overrides the toString method
 //that comes from object
 public String toString(){
  return getWord() + "--" + getDefinition();
 }

}

Here is the MyDictionary class

package com.spconger.DictionaryProgram;

import java.util.ArrayList;

public class MyDictionary {
 /*****
  * this class stores the Word objects
  * in an Arraylist. It provides methods
  * for adding words, removing them and 
  * returning the whole list of words
  */
     private ArrayList<Word> words;
     
     //initialize the ArrayList in the constructor
     public MyDictionary(){
      words = new ArrayList<Word>();
     }
     
     //add words to the list
     public void addWord(Word w){
      words.add(w);
     }
     
     //remove words from the list
     public void removeWord(Word w){
      for(Word wd : words){
       if(wd.getWord().equals(w.getWord())){
        words.remove(wd);
       }//end if
      }//end for
     }//end removeword
     
     //return the whole list of words
     public ArrayList getWords(){
      return words;
     }
}

Here is the Form Again

package com.spconger.DictionaryProgram;

//import allthe various form elements
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class WordForm {

 /***************
  * this class creates the form to add 
  * words and display the results in a 
  * JList object.  The form contains
  * four panels: A border panel to orient
  * the others, A grid panel that contains
  * labels and textboxes, a scrollpane 
  * with a list in the center and a 
  * flow panel with the buttons at
  * the bottom
  */
 private JFrame frame;
 private JPanel borderPanel;
 private JPanel newWordPanel;
 private JPanel buttonPanel;
 private JScrollPane scrollPane;
 private JList wordList;
 private JLabel wordPrompt;
 private JTextField wordText;
 private JLabel defPrompt;
 private JTextField defText;
 private JButton addButton;
 private JButton getWordsButton;
 private JButton exitButton;

 //instantiate the dictionary class
 private MyDictionary tech;

 public WordForm() {
  createFrame();
  tech = new MyDictionary();
 }

 private void createFrame() {
  frame = new JFrame();
  frame.setBounds(100, 100, 300, 300);
  frame.add(createBorderPanel());
  frame.setVisible(true);
 }

 private JPanel createBorderPanel() {
  borderPanel = new JPanel();
  borderPanel.setLayout(new BorderLayout());
  borderPanel.add(createNewWordPanel(), BorderLayout.NORTH);
  borderPanel.add(createScrollPane(), BorderLayout.CENTER);
  borderPanel.add(createButtonPanel(), BorderLayout.SOUTH);
  return borderPanel;
 }

 private JPanel createNewWordPanel() {
  newWordPanel = new JPanel();
  newWordPanel.setLayout(new GridLayout(2, 2));
  wordPrompt = new JLabel("Enter Word");
  wordText = new JTextField();
  defPrompt = new JLabel("Enter Definition");
  defText = new JTextField();
  newWordPanel.add(wordPrompt);
  newWordPanel.add(wordText);
  newWordPanel.add(defPrompt);
  newWordPanel.add(defText);
  return newWordPanel;
 }

 private JScrollPane createScrollPane() {
  wordList = new JList();
  // add the selection listener to the list
  // wordlist.addListSelectionListener(new SelectionListener());
  scrollPane = new JScrollPane(wordList);
  scrollPane.setBounds(20, 20, 100, 200);

  return scrollPane;
 }

 private JPanel createButtonPanel() {
  buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout());
  addButton = new JButton("Add Word");
  addButton.addActionListener(new AddButtonListener());
  getWordsButton = new JButton("Get Words");
  getWordsButton.addActionListener(new GetWordsListener());
  exitButton = new JButton("Exit");
  exitButton.addActionListener(new ExitListener());

  buttonPanel.add(addButton);
  buttonPanel.add(getWordsButton);
  buttonPanel.add(exitButton);

  return buttonPanel;
 }

 private class AddButtonListener implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent e) {
   //in this method we add a word object
   //to the dictionary class
   Word w = new Word();
   w.setWord(wordText.getText());
   w.setDefinition(defText.getText());
   tech.addWord(w);

   wordText.setText("");
   defText.setText("");
  }

 }

 private class GetWordsListener implements ActionListener {

  @Override
  //here we write to the JList
  public void actionPerformed(ActionEvent e) {
   ArrayList<Word> words = tech.getWords();
   DefaultListModel model = new DefaultListModel();

   for (Word w : words) {
    model.addElement(w.toString());
   }
   wordList.setModel(model);
  }

 }

 private class ExitListener implements ActionListener {

  @Override
  public void actionPerformed(ActionEvent e) {
   System.exit(0);

  }

 }
}

Here is the Program class

package com.spconger.DictionaryProgram;

public class Program {

 public static void main(String[] args) {
  WordForm wf = new WordForm();

 }

}

Here is a picture of the program running

No comments:

Post a Comment