gpt4 book ai didi

java - 将字符串数组中的元素添加到defaultlistmodel中

转载 作者:行者123 更新时间:2023-12-01 09:38:35 25 4
gpt4 key购买 nike

我尝试通过删除联系人来更新 JList,但它没有像我希望的那样更新 JList。

最初,JList 看起来像这样。

Broadcast //Not allowed to delete this element
Andy
Bobby
Chris
Brad

假设我已使用按钮将 Andy 从列表中删除。它应该显示

Broadcast //Not allowed to delete this element
Bobby
Chris
Brad

相反,它会在 JList 上显示此内容。

contact0
contact2
contact3
contact4

我不知道出了什么问题,我查阅了很多资料。我希望它返回实际姓名,而不是联系人#。代码如下。

private JList<String> listContacts;
private DefaultListModel<String> model;
private String[] contactList;

model = new DefaultListModel<String>();
contactList = controller.getContacts();
for(String contact : contactList){
model.addElement(contact);
}
listContacts = new JList<String>();
listContacts.setModel(model);

class MyButtonListener5 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(listContacts.getSelectedIndex() == 0){
controller.displayMsg("Can't delete broadcast.\n");
}
else if(listContacts.getSelectedIndex() > 0){
controller.displayMsg("You tried to delete " + listContacts.getSelectedValue() +".\n");
model.remove(listContacts.getSelectedIndex());
listContacts.setModel(model);
}
else{
controller.displayMsg("No contact to delete. Please select one.\n");
}
}
}

这是用于 contactList 的 getContacts。该方法位于 Controller 类中。我希望这能澄清我遇到麻烦的原因。

public String[] getContacts(){
Contact[] contacts = new Contact[5];
String[] names = new String[5];
for(int i = 0; i < 5; i++){
contacts[i] = new Contact("contact" + i);
names[i] = contacts[i].getName();
}
return names;
}

这是在我的联系人类(class)中。

private String name;
private String nickname;

public Contact(){
name = "";
nickname = "";
}
public Contact(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
@Override
public String toString(){
return this.getName();
}

最佳答案

我认为您可以通过重写 Contact 类中的 toString() 方法来解决此问题。

@Override
public String toString() {
return this.getName();
}

然后您可以返回联系人而无需 .getName() 并直接将其添加到列表中。 I recommend you taking a look at this example.

private JList listContacts;
private DefaultListModel model;
private Contact[] contactList;

model = new DefaultListModel();
contactList = controller.getContacts();
for(Contact contact : contactList){
model.addElement(contact);
}
listContacts = new JList();
listContacts.setModel(model);

class MyButtonListener5 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(listContacts.getSelectedIndex() == 0){
controller.displayMsg("Can't delete broadcast.\n");
}
else if(listContacts.getSelectedIndex() > 0){
controller.displayMsg("You tried to delete " + listContacts.getSelectedValue() +".\n");
model.remove(listContacts.getSelectedIndex());
listContacts.setModel(model);
}
else{
controller.displayMsg("No contact to delete. Please select one.\n");
}
}
}

还有你的 getContacts() 方法:

public Contact[] getContacts(){
Contact[] contacts = new Contact[5];
contacts[0] = "Broadcast";
contacts[1] = "Andy";
contacts[2] = "Bobby";
contacts[3] = "Chris";
contacts[4] = "Brad";

return contacts;
}

关于java - 将字符串数组中的元素添加到defaultlistmodel中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38627485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com