Thomas Darimont
Erfahrenes Mitglied
Hallo!
Schau mal hier:
Gruß Tom
Schau mal hier:
Java:
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import com.sun.java.swing.plaf.motif.MotifGraphicsUtils;
/**
* @author Administrator
*
* TODO Explain me
*/
public class JTableExample extends JFrame {
private JTable table;
private final ImageIcon UP_ICON = new ImageIcon(MotifGraphicsUtils.class
.getResource("icons/ScrollUpArrow.gif"));
private ImageIcon DOWN_ICON = new ImageIcon(MotifGraphicsUtils.class
.getResource("icons/ScrollDownArrow.gif"));
public JTableExample() {
super("JTableExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
table = new JTable(new SortableTableModel(
createRowData(),
createColumnHeaderNames(),
createColumnComparators()));
table.getTableHeader().setDefaultRenderer(createTableHeaderRenderer());
table.getTableHeader().addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
int columnIndex = table.columnAtPoint(evt.getPoint());
((SortableTableModel) table.getModel()).sortByColumn(table
.getColumnName(columnIndex));
}
});
Container c = getContentPane();
c.add(new JScrollPane(table), BorderLayout.CENTER);
pack();
setVisible(true);
}
private TableCellRenderer createTableHeaderRenderer() {
return new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
JTableHeader header = table.getTableHeader();
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
setText(value == null ? "" : value.toString());
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(SwingConstants.CENTER);
setHorizontalTextPosition(SwingConstants.LEFT);
SortableTableModel sortableTableModel = (SortableTableModel) table
.getModel();
if (sortableTableModel.sortColumnDesc[sortableTableModel
.getOriginalIndexFor(table.getColumnName(column))]) {
setIcon(UP_ICON);
} else {
setIcon(DOWN_ICON);
}
return this;
}
};
}
private Comparator[] createColumnComparators() {
return new Comparator[] { SortableTableModel.STRING_COMPARATOR,
SortableTableModel.STRING_COMPARATOR,
SortableTableModel.NUMBER_COMPARATOR };
}
private String[] createColumnHeaderNames() {
return new String[] { "Header1", "Header2", "Header3" };
}
private Object[][] createRowData() {
return new Object[][] { { "a", "c", 17 }, { "b", "b", 4 },
{ "f", "r", -393.2 }, { "q", "a", 44 },
{ "w", "z", 222222222 }, { "c", "a", 111 } };
}
public static void main(String[] args) {
new JTableExample();
}
static class SortableTableModel extends DefaultTableModel {
private boolean[] sortColumnDesc;
private Map<String, Comparator<?>> columnNameToComparatorMap = new HashMap<String, Comparator<?>>();
private Map<String, Integer> originalColumnNameToIndexMap = new HashMap<String, Integer>();
public static final Comparator<String> STRING_COMPARATOR = String.CASE_INSENSITIVE_ORDER;
public static final Comparator<Number> NUMBER_COMPARATOR = new Comparator<Number>() {
public int compare(Number o1, Number o2) {
return Double.valueOf(o1.toString()).compareTo(
Double.valueOf(o2.toString()));
}
};
private final RowComparator rowComparator = new RowComparator();
public SortableTableModel(Object[][] rowData, String[] headers,
Comparator<?>[] comparators) {
super(rowData, headers);
sortColumnDesc = new boolean[headers.length];
for (int i = 0; i < headers.length; i++) {
columnNameToComparatorMap.put(headers[i], comparators[i]);
originalColumnNameToIndexMap
.put(headers[i], Integer.valueOf(i));
}
}
public int getOriginalIndexFor(String columnName) {
return originalColumnNameToIndexMap.get(columnName);
}
@SuppressWarnings("unchecked")
public void sortByColumn(final String columnName) {
rowComparator.setDelegatingComparator(columnNameToComparatorMap
.get(columnName));
int columnIndex = originalColumnNameToIndexMap.get(columnName);
rowComparator.setCurrentColumn(columnIndex);
rowComparator.setSortDescending(sortColumnDesc[columnIndex]);
Collections.sort(this.dataVector, rowComparator);
sortColumnDesc[columnIndex] ^= true;
}
}
static class RowComparator implements Comparator<Object> {
int currentColumn;
boolean sortDescending;
Comparator<?> delegatingComparator;
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
Vector v1 = (Vector) o1;
Vector v2 = (Vector) o2;
int cmp = getDelegatingComparator().compare(
v1.get(getCurrentColumn()),
v2.get(getCurrentColumn()));
if (isSortDescending()) {
cmp *= -1;
}
return cmp;
}
/**
* @return the currentColumn
*/
public int getCurrentColumn() {
return currentColumn;
}
/**
* @param currentColumn
* the currentColumn to set
*/
public void setCurrentColumn(int currentColumn) {
this.currentColumn = currentColumn;
}
/**
* @return the sortDescending
*/
public boolean isSortDescending() {
return sortDescending;
}
/**
* @param sortDescending
* the sortDescending to set
*/
public void setSortDescending(boolean sortDescending) {
this.sortDescending = sortDescending;
}
/**
* @return the comparator
*/
public Comparator getDelegatingComparator() {
return delegatingComparator;
}
/**
* @param comparator
* the comparator to set
*/
public void setDelegatingComparator(Comparator comparator) {
this.delegatingComparator = comparator;
}
}
}
Gruß Tom