gpt4 book ai didi

java - 在 JList 中显示 HashSet 并实时更新项目

转载 作者:行者123 更新时间:2023-11-30 11:43:02 25 4
gpt4 key购买 nike

我想显示 HashSet 的内容在JList .我通过以下声明实现了这一点:

JList list = new JList(hashset.toArray());

现在,我想要list 来反射(reflect)hashset 中的变化。当 hashset 中的元素发生变化时,我可以通过调用 setListData() 来完成此操作,如下所示:

list.setListData(hashset.toArray());

这似乎不是一个正确的方法。据我了解 tutorial , 我需要扩展/实现 ListModel .如果适用于我的意图,请提供重载 ListModel 的适当示例。否则,请提出更好的方法。

提前致谢。

最佳答案

根据我们在聊天中的讨论,这里有一个可能的解决方案。我没有对此进行调试或测试,所以请注意:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import javax.swing.*;

public class TreeSetInJListTest extends JPanel {
private static final long serialVersionUID = 1L;
private static final String[] DATA_ARRAY = { "one", "two", "three", "two",
"three", "this is a longer string" };
TreeSetListModel<String> stringListModel = new TreeSetListModel<String>(
String.CASE_INSENSITIVE_ORDER);
JList<String> jList = new JList<String>(stringListModel);

public TreeSetInJListTest() {
for (int i = 0; i < DATA_ARRAY.length; i++) {
stringListModel.add(DATA_ARRAY[i]);
}

add(new JScrollPane(jList));
add(new JButton(new AddToListAction("Add To List", KeyEvent.VK_A)));
}

@SuppressWarnings("serial")
private class AddToListAction extends AbstractAction {
public AddToListAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}

@Override
public void actionPerformed(ActionEvent arg0) {
String element = JOptionPane.showInputDialog(TreeSetInJListTest.this,
"Enter String:", "Insert Text Into JList",
JOptionPane.OK_CANCEL_OPTION);
stringListModel.add(element);
}
}

private static void createAndShowGui() {
TreeSetInJListTest mainPanel = new TreeSetInJListTest();

JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

class TreeSetListModel<T extends Comparable<T>> extends AbstractListModel<T> {
private static final long serialVersionUID = 1L;
private TreeSet<T> treeSet;

public TreeSetListModel() {
treeSet = new TreeSet<T>();
}

public TreeSetListModel(Comparator<? super T> comparator) {
treeSet = new TreeSet<T>(comparator);
}

@Override
public T getElementAt(int index) {
if (index < 0 || index >= getSize()) {
String s = "index, " + index + ", is out of bounds for getSize() = "
+ getSize();
throw new IllegalArgumentException(s);
}
Iterator<T> iterator = treeSet.iterator();
int count = 0;
while (iterator.hasNext()) {
T t = (T) iterator.next();
if (index == count) {
return t;
}
count++;
}
// out of index. return null. will probably never reach this
return null;
}

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

public int getIndexOf(T t) {
int index = 0;
for (T treeItem : treeSet) {
if (treeItem.equals(treeItem)) {
return index;
}
index++;
}
return -1;
}

public boolean add(T t) {
boolean result = treeSet.add(t);
if (result) {
int index = getIndexOf(t);
fireIntervalAdded(this, index, index + 1);
}
return result;
}

public boolean remove(T t) {
int index = getIndexOf(t);
if (index < 0) {
return false;
}
boolean result = treeSet.remove(t);
fireIntervalRemoved(this, index, index + 1);
return result;
}

}

关于java - 在 JList 中显示 HashSet 并实时更新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437304/

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