Wednesday, January 28, 2015

Code for WordForm

Here is the code for the word form. I thought I would give it to you to save a little time. I have not given you the word class or the TechnicalDictionary class. We will do them together.

package com.spconger.TechDictionary;

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 {
 
 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;
 
 private TechnicalDictionary tech;
 
 public WordForm(){
  createFrame();
  tech = new TechnicalDictionary();
 }
 
 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) {
   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
  public void actionPerformed(ActionEvent e) {
   ArrayList 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);
   
  }
  
 }
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey Steve,

    Could you comment your code more? It helps me in a step-by-step manner.
    -MM

    ReplyDelete