好的,所以我有一个 JList 并且内容是由一个数组提供的。我知道如何将元素添加到数组中,但我想知道如何刷新 JList ......或者甚至有可能吗?我试过谷歌。:\
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class bs extends JApplet implements MouseListener {
public static String newline;
public static JList list;
public void init() {
String[] data = {"one", "two", "three", "four"};
list = new JList(data);
this.getContentPane().add(list);
list.addMouseListener(this);
String newline = "\n";
list.setVisible(true);
}
public void refresh(){
Address found;
this.listModel.clear();
int numItems = this.getAddressBookSize();
String[] a = new String[numItems];
for (int i=0;i<numItems;i++){
found = (Address)Addresses.get(i);
a[i] = found.getName();
}
/* attempt to sort the array */
Arrays.sort(a, String.CASE_INSENSITIVE_ORDER);
for (int i=0;i<numItems;i++) {
this.listModel.addElement(a[i]);
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
Object index = list.getSelectedValue();
System.out.println("You clicked on: " + index);
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void paint(Graphics g) {
}
}
有任何想法吗?
谢谢你。