Tuesday, February 9, 2016

Simple English Dictionary With JList


This program demonstrates the use of JList. Only two Swing components are displayed on window; JList and JTextArea. The content of the dictionary is stored inside a text file named "dict.txt".


Here is the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class EnglishDictionary extends JFrame{
    private JList list=null;
    private DefaultListModel listmod=null;
    private JScrollPane splist=null;
    private JTextArea tarea=null;
    private JScrollPane sptarea=null;
    private ArrayList al1=null;
    private ArrayList al2=null;
    
    public static void main(String[] args){
        EnglishDictionary app=new EnglishDictionary();
    }
    public EnglishDictionary(){
        super("English Dictionary");
        setSize(550,150);
        
        listmod=new DefaultListModel();
        list=new JList(listmod);
        list.addListSelectionListener(new ListHandler());
        splist=new JScrollPane(list);
        splist.setPreferredSize(new Dimension(100,100) );
        tarea=new JTextArea();
        sptarea=new JScrollPane(tarea);
        sptarea.setPreferredSize(new Dimension(400,100) );
        tarea.setEditable(false);
        al1=new ArrayList();
        al2=new ArrayList();
        
        setLayout(new FlowLayout());
        add(splist);
        add(sptarea);
        
        readFile();
        addWords();
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    public void readFile(){
        try{
            Scanner sc=new Scanner(new FileInputStream("dict.txt"));
            int counter=0;
            while(sc.hasNext()){
                StringTokenizer stoken=new StringTokenizer(sc.nextLine());
                al1.add(counter,stoken.nextToken("|"));
                al2.add(counter,stoken.nextToken("|"));
                counter++;
            }
        }catch(Exception e) {
            
        }
    }
    public void addWords(){
        for(int i=0;i<al1.size();i++){
            listmod.add(i,al1.get(i));
        }
    }
    public class ListHandler implements ListSelectionListener{
        public void valueChanged(ListSelectionEvent e){
            int selected=list.getSelectedIndex();
            tarea.setText("");
            tarea.setText((String)al2.get(selected));
        }
    }
}


private JList list=null;
private DefaultListModel listmod=null;
private JScrollPane splist=null;
private JTextArea tarea=null;
private JScrollPane sptarea=null;
private ArrayList al1=null;
private ArrayList al2=null;

Put it simply, JList is a class to present the user with a group of items, in this case "English words". The DefaultListModel class is used to add items to JList. As JList does not provide direct support for scrolling, you need to wrap it in a JScrollPane. The purpose of JTextArea in this program is to display the meaning of the selected word in the list. ArrayList "al1" holds the words retrieved from "dict.txt". ArrayList "al2" holds their meanings.

tarea.setEditable(false);

User is not allowed to edit the content of the text area.

public void readFile(){
     try{
          Scanner sc=new Scanner(new FileInputStream("dict.txt"));
          int counter=0;
          while(sc.hasNext()){
              StringTokenizer stoken=new StringTokenizer(sc.nextLine());
              al1.add(counter,stoken.nextToken("|"));
              al2.add(counter,stoken.nextToken("|"));
              counter++;
          }
     }catch(Exception e) {
            
     }
}

Both the Scanner and StringTokenizer are utilized to retrieve the content of the text file. Notice that "|" is a delimiter that serves to separate the English words and their meanings.

public void addWords(){
     for(int i=0;i<al1.size();i++){
          listmod.add(i,al1.get(i));
     }
}

This is the code to insert all the words into the list. Notice that the instance of DefaultListModel class is used here.

public class ListHandler implements ListSelectionListener{
     public void valueChanged(ListSelectionEvent e){
          int selected=list.getSelectedIndex();
          tarea.setText("");
          tarea.setText((String)al2.get(selected));
     }
}

When an item (word) is selected, its meaning will be displayed on the text area. The getSelectedIndex() method returns the index of the selected item. The value is then used by the get() method.

No comments:

Post a Comment