Question

You are to write a JAVA GUI that holds a dictionary. The GUI should have a JList which contains all the words contained in the dictionary. When a word is selected, you are to display the corresponding definition (or synonym) in a text field (or text area). When the user clicks the “Redefine” button, you should prompt the user to enter a new definition with a pop-up window. This should not only update the data in the GUI but also update the definition in the dictionary file.

Dictionary

abandon: forsake, desert

abbey: monastery

able: capable

abolish: get rid of

abortion: termination pregnancy

abridge: shorten, cut

absence: failure to be present

absolute: perfect or complete

absorb: take in, suck up

absorption: preoccupation, engrossment, soaking up, social assimilation

abstract: existing only in the mind

abundant: plentiful

abuse: insult, maltreatment, ill-treatment

academy: learned establishment

accent: speech pattern

accept: live with, put up with

acceptance: adoption, espousal, favorable reception

access: entree, accession, admittance, right to obtain or make use of

accident: fortuity, chance event, mishap

accompany: go or come with

account: accounting, account statement, explanation

accountant: comptroller, controller

accumulation: accretion

achievement: accomplishment

acid: water-soluble compound with sour taste

acquaintance: familiar person, familiarity, personal knowledge

acquisition: possession

act: human action, human activity, subdivision of a play or opera or ballet

action: legal action, military action, series of events that form a plot

The dictionary file (dictionary.txt) has one line per dictionary entry. Each entry contains a word, followed by a colon, foll

0 0
Add a comment Improve this question Transcribed image text
Answer #1

GUI_Dictionary.java

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Dictionary extends JFrame implements ListSelectionListener, ActionListener{
    private final HashMap<String, String> dictionary;
    private final JList words;
    private final JTextArea textArea;
    private final JButton redefineButton;
    private final JPanel panel;
    private final DefaultListModel listWords = new DefaultListModel();
    private final String filename;
    private String selectedWord = "";
  
    public Dictionary(String filename) throws IOException{
        this.filename = filename;
        dictionary = new HashMap<>();
        readDictionaryFile();
        words = new JList(listWords);
        words.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        words.setSelectedIndex(0);
        words.addListSelectionListener(this);
        textArea = new JTextArea();
        selectedWord = words.getSelectedValue().toString().trim();
        String sel = dictionary.get(selectedWord);
        textArea.setFont(new Font("Arial", Font.PLAIN, 16));
        textArea.setText("\n     "+sel+" \n \n \n \n");
        textArea.setColumns(10);
        textArea.setBackground(Color.WHITE);
        textArea.setEditable(false);
        redefineButton = new JButton(" Redefine ");
        redefineButton.addActionListener(this);
        panel = new JPanel();
        panel.add(textArea);
        panel.add(redefineButton);
        Container cp = this.getContentPane();
        cp.add(new JScrollPane(words), BorderLayout.WEST);
        cp.add(panel, BorderLayout.CENTER);
    }
  
    private void readDictionaryFile() throws IOException{
        BufferedReader objReader = null;
        try {
            String line;
            String spaces = "                          ";
            objReader = new BufferedReader(new FileReader(filename));
            while ((line = objReader.readLine()) != null) {
                if(line.length() > 0){
                    String[] s = line.split(":");
                    s[0] = s[0].trim();
                    listWords.addElement(" "+s[0]+spaces);
                    dictionary.put(s[0], s[1].trim());
                }
            }

        } catch (IOException e) {
        } finally {
            if (objReader != null) {
                objReader.close();
            }
        }
    }
  
    private void updateDictionaryFile(String word, String value) throws IOException {
        BufferedWriter objWriter = null;
        try {
            String line;
            objWriter = new BufferedWriter(new FileWriter(filename));
            for(Object obj : dictionary.keySet()){
                objWriter.write(obj.toString() + ":" + dictionary.get(obj).toString()+"\n");
            }
        } catch (IOException e) {
        } finally {
            if (objWriter != null) {
                objWriter.close();
            }
        }
    }

    public void showDictionary(){
        //set frame properties
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Dictionary");
        setSize(800, 350);
        setLocation(300, 200);
        setVisible(true);
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        selectedWord = words.getSelectedValue().toString().trim();
        String sel = dictionary.get(selectedWord);
        textArea.setText("\n     " + sel + "       \n \n \n \n");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            String s = JOptionPane.showInputDialog(null,
                    "Enter new definition for word '" + selectedWord + "'",
                    "Redefine", JOptionPane.OK_CANCEL_OPTION);
            s = s.trim();
            if (!s.isEmpty()) {
                dictionary.put(selectedWord, s);
                textArea.setText("\n     " + s + "       \n \n \n \n");
                updateDictionaryFile(selectedWord, s);
            }
        } catch (NullPointerException npe) {
        } catch (IOException ex) {
        }
    }
}

public class GUI_Dictionary {

    public static void main(String[] args) throws IOException {
        Dictionary dict = new Dictionary(args[0]);
        dict.showDictionary();
    }
}

dictionary.txt

abortion:termination pregnancy
abuse:insult, maltreatment, ill-treatment
access:entree, accession, admittance, right to obtain or make use of
accountant:comptroller, controller
abridge:shorten, cut
achievement:accomplishment
abolish:get rid of
absence:failure to be present
absorb:take in, suck up
accident:fortuity, chance event, mishap
acid:water-soluble compound with sour taste
abbey:monastery
act:human action, human activity, subdivision of a play or opera or ballet
action:legal action, military action, series of events that form a plot
acquisition:possession
academy:learned establishment
absorption:preoccupation, engrossment, soaking up, social assimilation
abundant:plentiful
accumulation:accretion
abstract:existing only in the mind
accent:speech pattern
accept:live with, put up with
accompany:go or come with
abandon:forsake, desert
acceptance:adoption, espousal, favorable reception
absolute:perfect or complete
able:capable
acquaintance:familiar person, familiarity, personal knowledge
account:accounting, account statement, explanation

Add a comment
Know the answer?
Add Answer to:
You are to write a JAVA GUI that holds a dictionary. The GUI should have a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT