import java.awt.BorderLayout; import java.awt.Color; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Comparator; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumnModel; import javax.swing.table.TableRowSorter; public class SpalteSortieren extends JFrame { public SpalteSortieren() { initGUI(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Tabelle nach Spalten sortieren"); this.setSize(600, 400); this.setLocationRelativeTo(null); this.setVisible(true); } private void initGUI() { DefaultTableModel model = createModel(); JTable table = new JTable(model); table.setShowGrid(true); table.setGridColor(Color.GRAY); JTableHeader header = createTableHeader(table.getColumnModel()); header.setBackground(Color.LIGHT_GRAY); table.setTableHeader(header); table.setAutoCreateRowSorter(true); TableRowSorter newSorter = new TableRowSorter( model); header.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { sortColumns(e, table, newSorter); } }); this.add(new JScrollPane(table), BorderLayout.CENTER); } private void sortColumns(MouseEvent e, JTable table, TableRowSorter newSorter) { int spalte = table.columnAtPoint(e.getPoint()); String spaltenName = table.getColumnName(spalte); table.setRowSorter(newSorter); if (spaltenName.equals("Seriennummer") && e.isAltDown()) { newSorter.setComparator(spalte, new NumberSorter("-")); newSorter.sort(); } else if (spaltenName.equals("Preis")) { newSorter.setComparator(spalte, new DecimalSorter()); newSorter.sort(); } } private JTableHeader createTableHeader(TableColumnModel cm) { return new JTableHeader(cm) { public String getToolTipText(MouseEvent e) { Point p = e.getPoint(); int ci = cm.getColumnIndexAtX(p.x); int mi = cm.getColumn(ci).getModelIndex(); return createToolTips()[mi]; } }; } private String[] createToolTips() { return new String[] { "Click sortiert lexikalisch", "Click sortiert lexikalisch,
Alt-Click sortiert nach der Endnummer", "Click sortiert numerisch" }; } private DefaultTableModel createModel() { String[] header = { "Objekt", "Seriennummer", "Preis" }; String[][] data = { new String[] { "B\u00FCgeleisen", "kg-04-7385", "68,98" }, new String[] { "Staubsauger", "24.17-123456", "417" }, new String[] { "Eierkocher", "eikoch-9413", "19,95" }, new String[] { "Luftpumpe", "0034-lp-1713", "19,95" }, new String[] { "Schreibtisch", "x24-scht-0815", "235,50" } }; return new DefaultTableModel(data, header); } public static void main(String[] args) { new SpalteSortieren(); } } class NumberSorter implements Comparator { final String trenner; public NumberSorter(final String trenner) { this.trenner = trenner; } @Override public int compare(String s1, String s2) { int pos1 = s1.lastIndexOf(trenner) + 1; int pos2 = s2.lastIndexOf(trenner) + 1; if (pos1 > -1) { s1 = ((String) s1).substring(pos1); } if (pos2 > -1) { s2 = ((String) s2).substring(pos2); } if (s1 instanceof String && ((String) s1).length() == 0) { s1 = null; } if (s2 instanceof String && ((String) s2).length() == 0) { s2 = null; } if (s1 == null && s2 == null) { return 0; } else if (s1 == null) { return 1; } else if (s2 == null) { return -1; } else { try { int i1 = new Integer(s1).intValue(); int i2 = new Integer(s2).intValue(); int erg = i1 < i2 ? 1 : i1 > i2 ? -1 : 0; return erg; } catch (NumberFormatException e) { } } return ((String) s1).compareTo((String) s2); } } class DecimalSorter implements Comparator { @Override public int compare(String o1, String o2) { Double d1 = convertToDouble(o1); Double d2 = convertToDouble(o2); if (d1 == null && d2 == null) { return 0; } else if (d1 == null) { return 1; } else if (d2 == null) { return -1; } return d1 < d2 ? 1 : d2 < d1 ? -1 : 0; } private Double convertToDouble(String s) { if (s.indexOf(",") > -1) s = s.replace(",", "."); if (s.matches("-?\\d+([.]{1}\\d+)?")) { try { return new Double(s); } catch (NumberFormatException e) { } } return null; } }