我正在尝试扩展DefaultListModel,以便它可以进行排序和迭代。我在这里找到了一些代码http://www.javalobby.org/java/forums/t94074.html。我现在正在尝试参数化此代码,但addAll,containsAll,removeAll,retainAll和toArray方法都会给出名称冲突错误。我有点理解为什么会这样,但我不确定如何解决它。有一个更好的方法吗?代码
package main;
import javax.swing.DefaultListModel;
import javax.swing.*;
import java.util.*;
public class DefaultListModelSort<E extends Object> extends DefaultListModel<E>
implements List<E> {
private Delegate m_delegate = new Delegate();
public DefaultListModelSort() {
super();
}
public DefaultListModelSort(Collection<E> c) {
this();
addAll(c);
}
public boolean add(E o) {
return m_delegate.add(o);
}
public boolean removeE(E o) {
return m_delegate.remove(o);
}
public boolean addAll(int index, Collection<E> c) {
return m_delegate.addAll(index, c);
}
public boolean addAll(Collection<E> c) {
return m_delegate.addAll(c);
}
public boolean containsAll(Collection<E> c) {
return m_delegate.containsAll(c);
}
public boolean removeAll(Collection<E> c) {
return m_delegate.removeAll(c);
}
public boolean retainAll(Collection<E> c) {
return m_delegate.retainAll(c);
}
public Iterator<E> iterator() {
return m_delegate.iterator();
}
public List<E> subList(int fromIndex, int toIndex) {
return m_delegate.subList(fromIndex, toIndex);
}
public ListIterator<E> listIterator() {
return m_delegate.listIterator();
}
public ListIterator<E> listIterator(int index) {
return m_delegate.listIterator(index);
}
public E[] toArray(E a[]) {
return m_delegate.toArray(a);
}
/**
* This class extends AbstractList so we get all the functionality of
* iterators and such for free.
*/
private class Delegate extends AbstractList<E> {
public Delegate() {
super();
}
public E get(int index) {
return DefaultListModelSort.super.get(index);
}
public int size() {
return DefaultListModelSort.super.size();
}
public E set(int index, E element) {
return DefaultListModelSort.super.set(index, element);
}
public void add(int index, E element) {
DefaultListModelSort.super.add(index, element);
}
public E remove(int index) {
return DefaultListModelSort.super.remove(index);
}
}
}
答案 0 :(得分:3)
我试图扩展DefaultListModel,以便它可以排序 可迭代。
使用JTable with one Column,JTableHeader
可以删除
有关RowSorter
阅读Oracle教程的更多信息How to use Tables - Sorting and Filtering