Yurttas/PL/OOL/Java/F/09/02/01/00/JawtList.java

From ZCubes Wiki
Revision as of 16:21, 7 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="java" line start="1" enclose="div">import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.util.*; ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
 1import javax.swing.*;
 2import javax.swing.event.*;
 3
 4import java.awt.*;
 5import java.awt.event.*;
 6
 7import java.util.*;
 8
 9//this is a simple adapter class to
10//convert List awt methods to Swing methods
11
12public class JawtList extends JScrollPane 
13                      implements ListSelectionListener,
14                                 AwtList {
15  private JList listWindow;
16  private JListData listContents;
17  private ListSelectionListener listener;
18
19   public JawtList(int rows) {
20     listContents = new JListData();
21     listWindow = new JList(listContents);
22     listWindow.setPrototypeCellValue("Abcdefg Hijkmnop");
23     getViewport().add(listWindow);
24   }
25
26   public int getSelectedIndex() {
27     return listWindow.getSelectedIndex();
28   }
29
30   public void add(String s) {
31     listContents.addElement(s);
32   }
33
34   public void remove(String s) {
35     listContents.removeElement(s);
36   }
37
38   public String[] getSelectedItems() {
39     Object[] obj = listWindow.getSelectedValues();
40     String[] s = new String[obj.length];
41     for(int i=0; i<obj.length; i++) 
42       s[i] = obj[i].toString();
43     return s;
44   }
45
46   public void addListSelectionListener(ListSelectionListener lsl) {
47     listener = lsl;
48     listWindow.addListSelectionListener(this);
49   }
50
51   public void valueChanged(ListSelectionEvent e) {
52     listener.valueChanged(e);
53   }
54
55   public void clear() {
56     listContents.clear();
57   }
58   
59}
60
61class JListData extends AbstractListModel {
62  private Vector data;
63
64  public JListData() {
65    data = new Vector();
66  }
67
68  public void clear() {
69    data = new Vector();
70  }
71
72  public int getSize() {
73    return data.size();
74  }
75
76  public Object getElementAt(int index) {
77    return data.elementAt(index);
78  }
79
80  public void addElement(String s) {
81    data.addElement(s);
82    fireIntervalAdded(this, data.size()-1, data.size());
83  }
84
85  public void removeElement(String s) {
86    data.removeElement(s);
87    fireIntervalRemoved(this, 0, data.size());
88  }
89
90}