【问题标题】:JList using HashMap key (String) as its display?JList 使用 HashMap 键(字符串)作为其显示?
【发布时间】:2017-09-21 23:32:49
【问题描述】:

我已经自学了几个月的 Java 并且遇到了挑战。我正在制作一个联系人列表应用程序。我选择使用 HashMap<String, Contact> 作为我的联系人存储空间。我遇到的挑战是我对 Swing 的不熟悉。我第一次尝试使用 JList。我已经让 JList 可以处理普通的字符串数组,但现在我想使用 HashMap 中的 Key 值来显示 JList。

我在其他地方读到自定义ListModel 可以,但我没有找到任何具体的东西。 oracle 文档How to Use Lists 在其示例中使用了 DefaultListModel。我读过使用 AbstractListModel 或 ListModel 是朝着正确方向迈出的一步。到目前为止,主要有三个类:

班级联系人

public class Contact {

    private String name;
    private String phoneNumber;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

课本

import java.util.HashMap;
import java.util.Map;

public class Book {

    private Map<String, Contact> addressbook = new HashMap<String, Contact>();

    public Map<String, Contact> getAddressbook() {
        return addressbook;
    }

    public void setAddressbook(Map<String, Contact> addressbook) {
        this.addressbook = addressbook;
    }

}

类用户界面 这是我在创建自定义列表模型时遇到困难的地方,该模型从位于类 Book 中的 HashMap 中获取字符串键。

import java.awt.BorderLayout;

public class UserInterface extends JPanel implements ActionListener {

    private static final long serialVersionUID = 2161244209167568887L;

    // Contact list display
    JList contactList;

    // Menu bar and accompanying menu items
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem newContactMenuButton;
    private JMenuItem exitAppMenuButton;

    // Buttons
    private JButton newContactButton;
    private JButton openContactButton; 
    private JButton deleteContactButton; 

    // Panels to place components into
    private JPanel mainPanel;
    private JPanel buttonPanel; 

    // For message dialogs
    private JFrame messageDialog;

    public UserInterface() {

        // Add the JList
        contactList = new JList(new ContactListModel()); // ??

        // Creating the menu bar and its items
        // Adding ActionListeners to the menu buttons
        menuBar = new JMenuBar();
        menu = new JMenu("File");
        newContactMenuButton = new JMenuItem("New Contact");
        exitAppMenuButton= new JMenuItem("Exit");
        newContactMenuButton.addActionListener(this);
        exitAppMenuButton.addActionListener(this);
        menu.add(newContactMenuButton);
        menu.add(exitAppMenuButton);
        menuBar.add(menu);

        // Creating the Buttons
        // Adding ActionListeners to the buttons
        newContactButton = new JButton("New Contact");
        openContactButton = new JButton("Open Contact");
        deleteContactButton = new JButton("Delete Contact");
        newContactButton.addActionListener(this);
        openContactButton.addActionListener(this);
        deleteContactButton.addActionListener(this);

        // Creating the Panels with Grid Layouts
        mainPanel = new JPanel(new GridLayout());
        buttonPanel = new JPanel(new GridLayout(0, 1));

        // Adding components to the Panels
        mainPanel.add(contactList);
        buttonPanel.add(newContactButton);
        buttonPanel.add(openContactButton);
        buttonPanel.add(deleteContactButton);

        // Adding and aligning the Panels
        setBorder(BorderFactory.createEmptyBorder(45, 45, 45, 45));
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.EAST);

    }

    public void CreateAndShowUI() {
        JFrame frame = new JFrame("Addressbook Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new UserInterface());
        frame.setJMenuBar(this.menuBar);
        frame.pack();
        frame.setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == newContactButton) {
            JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
        }

        if (e.getSource() == openContactButton) {
            JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
        }

        if (e.getSource() == deleteContactButton) {

            if(contactList.isSelectionEmpty()) {
                JOptionPane.showMessageDialog(messageDialog, "No contact selected.");

            } else if(!contactList.isSelectionEmpty()) {

                }
            }

        if (e.getSource() == newContactMenuButton) {
            JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
        }

        if (e.getSource() == exitAppMenuButton) {
            System.exit(0);
        }
    }
}

final class ContactListModel extends AbstractListModel {

    Book book = new Book();
    Map<String, Contact> bookList = book.getAddressbook();

    public Object getElementAt(int keys) {
        keys = // ??
        return keys;
    }

    @Override
    public int getSize() {
        return bookList.size();
    }

}

高度赞赏任何正确方向的真正观点。与此同时,我会继续寻找。

编辑:更新和答复

以下是相关的更新代码位。正如用户 carmickr 建议的那样,我使用 DefaultListModel 来处理来自地址簿 HashMap 的数据。

private DefaultListModel<Set<String>> model;
private JList<Set<String>> contactList;

然后在 UserInterface 构造函数内部:

// Create the DefaultListModel object
// Add the JList
model = new DefaultListModel<Set<String>>();
model.addElement(book.AB.keySet());
contactList = new JList<Set<String>>(model);

【问题讨论】:

  • 请将部分答案添加到下面的答案中,而不是问题中。

标签: java swing


【解决方案1】:

我在创建自定义列表模型时遇到困难,该模型从位于类 Book 中的 HashMap 中获取字符串键。

您无需创建自定义模型。您可以将每个Contact 对象添加到DefaultListModel。使用模型的重点是模型包含所有数据,您可以使用模型的方法来访问数据。

那么让JList 工作的最简单方法是在您的Contact 对象中实现toString() 方法以返回您希望在JList 中看到的属性。

编辑:更新和答复

以下是相关的更新代码位。正如用户 carmickr 建议的那样,我使用 DefaultListModel 来处理来自地址簿 HashMap 的数据。

private DefaultListModel<Set<String>> model;
private JList<Set<String>> contactList;

然后在 UserInterface 构造函数内部:

// Create the DefaultListModel object
// Add the JList
model = new DefaultListModel<Set<String>>();
model.addElement(book.AB.keySet());
contactList = new JList<Set<String>>(model);

【讨论】:

  • 谢谢!我从您的回答中汲取了智慧并将其付诸实践。我将在上面发布更新的内容。现在我只需要弄清楚为什么 JList 水平而不是垂直显示每个联系人。我想这是由于 Set 的默认 toString 方法。我会继续工作的。
  • @foamu,默认行为是 JList 在 JList 中垂直显示每个项目,除非您更改了 JList 方向。再次阅读 JLIst 教程并查看示例代码。
  • 感谢您的回复。我发现我没有将元素正确添加到模型中,但解决了它。很高兴回答我的第一个问题:)
猜你喜欢
  • 2016-01-14
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多