Hallo. Ich hab eine JTable welche Daten aus einem Vektor anzeigt. Der Vektor befindet sich in einem eigenen Model:
Wenn ich nun die Tabelle sortiere wird der Vektor nicht aktualisiert, wodurch die Tabelle nicht korrekt funktioniert. Wie kann ich nun den Vektor ebenfalls nach dem selben Schema sortieren? Ich habe dazu etwas gefunden, was Vektoren sortiert:
Doch ich weiss jetzt nicht, in welcher Klasse ich welche Funktion überschreiben muss um beim sortieren zu erzielen, dass eben dieser Vektor mitsortiert wird.
mfg
Code:
/**
* The Class ExplorerFileTableModel.
*/
class ExplorerFileTableModel extends AbstractTableModel {
/** The data. */
private Vector<File> data = new Vector<File>();
/** The column names. */
private String[] columnNames;
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 3751023997792200462L;
/**
* Instantiates a new explorer file table model.
*
* @param columnNames
* the column names
*/
ExplorerFileTableModel(String[] columnNames) {
this.columnNames = columnNames;
}
/*
* (non-Javadoc)
*
* @see javax.swing.this.TableModel#getColumnCount()
*/
@Override
public int getColumnCount() {
return 5;
}
/*
* (non-Javadoc)
*
* @see javax.swing.this.TableModel#getRowCount()
*/
@Override
public int getRowCount() {
return data.size();
}
/*
* (non-Javadoc)
*
* @see javax.swing.table.AbstractTableModel#getColumnName(int)
*/
public String getColumnName(int col) {
return columnNames[col];
}
/*
* (non-Javadoc)
*
* @see javax.swing.table.AbstractTableModel#getColumnClass(int)
*/
@SuppressWarnings("unchecked")
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* (non-Javadoc)
*
* @see javax.swing.this.TableModel#getValueAt(int, int)
*/
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
File tmpFile = data.get(rowIndex);
if (tmpFile == null)
return null;
if (columnIndex == 0) {
if (tmpFile.equals(curDirectory.getParentFile()))
return "...";
return tmpFile.getName();
}
if (columnIndex == 1) {
return getAttrib(tmpFile);
}
if (columnIndex == 2) {
return getFileSize(tmpFile);
}
if (columnIndex == 3) {
return (ldt
.plusSeconds(((Long) (tmpFile.lastModified() / 1000))
.intValue())).toString(dtf);
}
if (columnIndex == 4) {
if (tmpFile.isDirectory())
return "Ordner";
String tmp = tmpFile.getName();
tmp = tmp.replace(".", ":");
Pattern p = Pattern.compile(":");
String[] splitted = p.split(tmp);
if (splitted.length == 0)
return "Kein Dateityp";
return splitted[splitted.length - 1];
}
return null;
}
/**
* Adds the value.
*
* @param file
* the file
*/
public void addValue(File file) {
data.add(file);
}
/**
* Removes the all elements.
*/
public void removeAllElements() {
data.removeAllElements();
}
/**
* Gets the file.
*
* @param index
* the index
*
* @return the file
*/
public File getFile(int index) {
if (index >= data.size())
return null;
return data.get(index);
}
}
Wenn ich nun die Tabelle sortiere wird der Vektor nicht aktualisiert, wodurch die Tabelle nicht korrekt funktioniert. Wie kann ich nun den Vektor ebenfalls nach dem selben Schema sortieren? Ich habe dazu etwas gefunden, was Vektoren sortiert:
Code:
public void sortAllRowsBy(ExplorerFileTableModel model, int colIndex,
boolean ascending) {
Vector data = model.getDataVector();
Collections.sort(data, new ColumnSorter(colIndex, ascending));
model.fireTableStructureChanged();
}
// This comparator is used to sort vectors of data
/**
* The Class ColumnSorter.
*/
public class ColumnSorter implements Comparator<Vector<File>> {
/** The col index. */
private int colIndex;
/** The ascending. */
private boolean ascending;
/**
* Instantiates a new column sorter.
*
* @param colIndex
* the col index
* @param ascending
* the ascending
*/
ColumnSorter(int colIndex, boolean ascending) {
this.colIndex = colIndex;
this.ascending = ascending;
}
/*
* (non-Javadoc)
*
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@SuppressWarnings("unchecked")
public int compare(Vector<File> a, Vector<File> b) {
Object o1 = a.get(colIndex);
Object o2 = b.get(colIndex);
// Treat empty strains like nulls
if (o1 instanceof String && ((String) o1).length() == 0) {
o1 = null;
}
if (o2 instanceof String && ((String) o2).length() == 0) {
o2 = null;
}
// Sort nulls so they appear last, regardless
// of sort order
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return 1;
} else if (o2 == null) {
return -1;
} else if (o1 instanceof Comparable) {
if (ascending) {
return ((Comparable) o1).compareTo(o2);
} else {
return ((Comparable) o2).compareTo(o1);
}
} else {
if (ascending) {
return o1.toString().compareTo(o2.toString());
} else {
return o2.toString().compareTo(o1.toString());
}
}
}
}
Doch ich weiss jetzt nicht, in welcher Klasse ich welche Funktion überschreiben muss um beim sortieren zu erzielen, dass eben dieser Vektor mitsortiert wird.
mfg