栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将元素添加到DefaultListModel时JList不更新

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将元素添加到DefaultListModel时JList不更新

有两个问题,
第一个:

不好:

if (workerType.getSelectedItem().toString() == "Salaried"){
。使用equals(…)或equalsIgnoreCase(…)。理解
==
检查是否绝对不是您感兴趣的 引用 相等性。您需要 函数
相等性,这是方法要测试的。

所以:

if ("Salaried".equalsIgnoreCase(workerType.getSelectedItem().toString())){

第二:

AddEmployeeDialog frame = new AddEmployeeDialog(new Employeeframe());

您要传入一个 新的 Employeeframe对象,这意味着您正在为错误的Employeeframe更新列表模型。而是传递 可视化的
Employeeframe。

更改为:

AddEmployeeDialog frame = new AddEmployeeDialog(Employeeframe.this);

我创建的程序可以缩小您的代码,但仍然允许其运行并演示您的问题:

import java.awt.BorderLayout;import java.awt.event.*;import javax.swing.*;import javax.swing.border.TitledBorder;@SuppressWarnings("serial")public class Employeeframe extends Jframe implements ActionListener {    private JButton addButton;    private DefaultListModel<Employee> listModel;    private JList<Employee> employeeList;    public Employeeframe() {        super("Employee Manager");        setLocationByPlatform(true);        setDefaultCloseOperation(EXIT_ON_CLOSE);        listModel = new DefaultListModel<>();        employeeList = new JList<>(listModel);        JScrollPane employeeScroll = new JScrollPane(employeeList);        employeeScroll.setBorder(new TitledBorder("Employee List"));        addButton = new JButton("Add Employee");        addButton.addActionListener(this);        Box topBox = Box.createHorizontalBox();        topBox.add(addButton);        getContentPane().setLayout(new BorderLayout());        getContentPane().add(topBox, BorderLayout.NORTH);        getContentPane().add(employeeScroll, BorderLayout.CENTER);        pack();    }    public DefaultListModel<Employee> getListModel() {        return this.listModel;    }    @Override    public void actionPerformed(ActionEvent event) {        Object source = event.getSource();        if (source == addButton) { // !! AddEmployeeDialog frame = new AddEmployeeDialog(new Employeeframe()); AddEmployeeDialog frame = new AddEmployeeDialog(Employeeframe.this); frame.setVisible(true);        }    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() { public void run() {     new Employeeframe().setVisible(true); }        });    }}@SuppressWarnings("serial")class AddEmployeeDialog extends JDialog implements ActionListener {    private JComboBox<String> workerType;    private JTextField givenNameField;    private JTextField familyNameField;    private JButton okButton;    private JButton cancelButton;    private Employeeframe employeeframe;    public AddEmployeeDialog(final Employeeframe frame) {        super(frame, "Add Employee", true);        setLocationRelativeTo(employeeframe);        this.employeeframe = frame;        workerType = new JComboBox<String>(Employee.getEmployeeTypes());        givenNameField = new JTextField(20);        familyNameField = new JTextField(20);        Box workerBox = Box.createHorizontalBox();        workerBox.add(new JLabel("Worker type"));        workerBox.add(workerType);        workerBox.add(new JLabel("Start date"));        Box givenNameBox = Box.createHorizontalBox();        givenNameBox.add(new JLabel("Given name "));        givenNameBox.add(givenNameField);        Box familyNameBox = Box.createHorizontalBox();        familyNameBox.add(new JLabel("Family name"));        familyNameBox.add(familyNameField);        workerType.setSelectedItem(null);        // Create buttons and add the current class as an ActionListener        okButton = new JButton("OK");        okButton.addActionListener(this);        cancelButton = new JButton("Cancel");        cancelButton.addActionListener(this);        Box bottomBox = Box.createHorizontalBox();        bottomBox.add(Box.createHorizontalGlue());        bottomBox.add(okButton);        bottomBox.add(Box.createHorizontalGlue());        bottomBox.add(cancelButton);        bottomBox.add(Box.createHorizontalGlue());        // Lay out the GUI        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));        getContentPane().add(workerBox);        getContentPane().add(givenNameBox);        getContentPane().add(familyNameBox);        getContentPane().add(Box.createVerticalStrut(10));        getContentPane().add(bottomBox);        pack();    }    @Override    public void actionPerformed(ActionEvent event) {        Object source = event.getSource();        if (source == okButton) { System.out.println("here"); System.out.println(workerType.getSelectedItem()); if ("Salaried".equalsIgnoreCase(workerType.getSelectedItem().toString())) {     Employee employee = new Employee("Salaried", givenNameField.getText(), familyNameField.getText());     employeeframe.getListModel().addElement(employee); } else {     Employee employee = new Employee("Hourly", givenNameField.getText(), familyNameField.getText());     employeeframe.getListModel().addElement(employee); }        }        dispose();    }}class Employee {    private static final String[] EMPLOYEE_TYPES = { "Salaried", "Hourly" };    private String givenName;    private String familyName;    private String type;    public Employee(String type, String givenName, String familyName) {        this.type = type;        this.givenName = givenName;        this.familyName = familyName;    }    public static String[] getEmployeeTypes() {        return EMPLOYEE_TYPES;    }    public String getGivenName() {        return givenName;    }    public String getFamilyName() {        return familyName;    }    public String getType() {        return type;    }    @Override    public String toString() {        return String.format("Employee: %s, %s %s", type, givenName, familyName);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号