mark
mark

Reputation: 2941

Java - Displaying contents of a HashMap using (Abstract)ListModel

I am designing a basic telephone directory for a project. It has three classes, Main (for the GUI), TelephoneDirectory (an object to store TelephoneRecords objects) and a TelephoneRecords class (where information for each record object is stored).

The requirements state: Extend your application by adding a list displaying the complete current contents of the telephone directory, ordered alphabetically by name. You will need to implement a ListModel. You may want to study class AbstractListModel before starting on your own implementation.

Only problem is, I have absolutely no idea how to extend my application to achieve this. I have searched online all night and haven't found a way to do this. I have tried storing the objects in an AbstractListModel rather than a HashMap but get errors. I don't exactly know what or why it is used and how I could use it. The next requirement (by the way) is to have the JList auto-update with new data when it's entered so I guess it has something to do with that?

Either way, if anyone could help it'd be great. My current working code for the previous that needs to be edited version is:

MAIN

public class Main extends JFrame implements ActionListener {

private static TelephoneDirectory directory = new TelephoneDirectory();
private JTextField nameField;
private JTextField numberField;
private JList contactList;

public Main() {
    setTitle("Telephone Directory");
    setLayout(new GridLayout(0,2));

    JLabel nameLabel = new JLabel("Name of Contact:");
    nameField = new JTextField(20);
    add(nameLabel);
    add(nameField);

    JLabel numberLabel = new JLabel("Number of Contact:");
    numberField = new JTextField(20);
    add(numberLabel);
    add(numberField);

    JButton enterButton = new JButton("Enter");
    JButton cancelButton = new JButton("Cancel");
    enterButton.addActionListener(this);
    cancelButton.addActionListener(this);
    add(enterButton);
    add(cancelButton);

    JLabel contactsLabel = new JLabel("Current Contacts:");
    contactList = new JList();
    add(contactsLabel);
    add(contactList);

    setVisible(true);
    pack();
}

public static void main(String[] args) {

    new Main();

}

@Override
public void actionPerformed(ActionEvent arg0) {
    JButton jb = (JButton) arg0.getSource();
    if (jb.getText().equals("Cancel")) {
        System.exit(0);
    } else {
        directory.addRecord(nameField.getText(), new TelephoneRecords(nameField.getText(), numberField.getText()));
        System.out.println("Added record for " + nameField.getText() + ": number is " + numberField.getText()   + ".");
    }
}

}

TELEPHONEDIRECTORY

public class TelephoneDirectory implements Iterable<TelephoneRecords> {

private HashMap records;

public TelephoneDirectory() {
    records = new HashMap<String, TelephoneRecords>();
}

public void addRecord(String name, TelephoneRecords newRecord) {
    records.put(name, newRecord);
}

public TelephoneRecords getRecord(String name) {
    return (TelephoneRecords) records.get(name);
}

public void getDirectory() {
    System.out.println("Telephone Directory:");
    records.values().iterator();
}

@Override
public Iterator<TelephoneRecords> iterator() {
    return records.values().iterator();
}

}

TELEPHONERECORDS

public class TelephoneRecords {

private String name;
private String number;

public TelephoneRecords(String name, String number) {
    this.name = name;
    this.number = number;
}

public String getName() {
    return name;
}

public String getNumber() {
    return number;
}

@Override
public String toString() { 
    return "The phone number of " + name + " is " + number + ".";
}

}

Upvotes: 0

Views: 898

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You may be trying to do too much with inheritance. Rather than using an AbstractListModel in place of your HashMap, consider creating a class that extends AbstractListModel and that holds the TelephoneDirectory class with its HashMap as the nucleus of the AbstractListModel's data. This is called extending a class by composition rather than by inheritance.

Edit: Also consider using a TreeMap rather than a HashMap so as to be able to retrieve your names and telephone records in name order. You'll also need to give your TelephoneDirectory class a getElementAt(int index) and a getSize() method to allow it to be used within the AbstractListModel class.

Upvotes: 4

Related Questions