jxTable初始排序顺序

时间:2014-05-27 15:00:44

标签: java swing swingx

我想先对表格进行排序。我尝试了以下代码:

tableModel = createTableModel(model);
rowSorter = new TableRowSorter<>(tableModel);
rowSorter.setSortKeys(Arrays.asList(getDefaultSort()));
rowSorter.sort();


final JXTable table = new JXTable();
table.setAutoCreateRowSorter(false);
table.setRowSorter(createSorter());

当我单击默认排序列时,它工作正常,因此实际排序似乎没有问题。但是UI最初没有使用上面的代码排序,UI没有显示排序指示符(列标题中的小三角形)。我错过了什么?

3 个答案:

答案 0 :(得分:3)

问题可能是在设置TableModel之前设置了RowSorter。我曾经遇到过这个问题。

答案 1 :(得分:0)

DefaultRowSorter.toggleSortOrder( column );

答案 2 :(得分:-1)

尝试这样做:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class SortTable extends JFrame{
  protected JTable table = new JTable();

  protected TabModel tabmodel;

   public SortTable() {
    super();
    setSize(500, 200);

    tabmodel = new TabModel();

     table.setModel(tabmodel);

    JTableHeader header = table.getTableHeader();
    header.setUpdateTableInRealTime(true);
    header.addMouseListener(tabmodel.new ColumnListener(table));
    header.setReorderingAllowed(true);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);
    setVisible(true);
  }

  public static void main(String argv[]) {
    new SortTable();
  }
}
class TabModel extends AbstractTableModel {
  protected int sortCol = 0;

  protected boolean isSortAsc = true;

  protected int m_result = 0;
  protected int columnsCount = 1;
  Vector vector = new Vector();
  public TabModel() {
    vector.removeAllElements();
    vector.addElement(new Integer(3));
    vector.addElement(new Integer(504));
    vector.addElement(new Integer(1));
    vector.addElement(new Integer(4));
    vector.addElement(new Integer(4));
    vector.addElement(new Integer(7));
    vector.addElement(new Integer(9));
    vector.addElement(new Integer(78));
    vector.addElement(new Integer(654));
    vector.addElement(new Integer(91));
    vector.addElement(new Integer(718));
    vector.addElement(new Integer(62254));
  }

  public int getRowCount() {
Collections.sort(vector,new TabComparator(isSortAsc)); 
        return vector == null ? 0 : vector.size();
  }

  public int getColumnCount() {
    return columnsCount;
  }

  public String getColumnName(int column) {
    String str = "data";
    if (column == sortCol)
      str += isSortAsc ? " >>" : " <<";
    return str;
  }

  public boolean isCellEditable(int nRow, int nCol) {
    return false;
  }

  public Object getValueAt(int nRow, int nCol) {
    if (nRow < 0 || nRow >= getRowCount())
      return "";
    if(nCol>1){
      return "";
    }
    return vector.elementAt(nRow);
  }

  public String getTitle() {
    return "data ";
  }

  class ColumnListener extends MouseAdapter {
    protected JTable table;

    public ColumnListener(JTable t) {
      table = t;
    }

    public void mouseClicked(MouseEvent e) {
      TableColumnModel colModel = table.getColumnModel();
      int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
      int modelIndex = colModel.getColumn(columnModelIndex)
          .getModelIndex();

      if (modelIndex < 0)
        return;
      if (sortCol == modelIndex)
        isSortAsc = !isSortAsc;
      else
        sortCol = modelIndex;

      for (int i = 0; i < columnsCount; i++) { 
        TableColumn column = colModel.getColumn(i);
        column.setHeaderValue(getColumnName(column.getModelIndex()));
      }
      table.getTableHeader().repaint();

      Collections.sort(vector,new TabComparator(isSortAsc));
      table.tableChanged(new TableModelEvent(TabModel.this));
      table.repaint();
    }
  }
}

class TabComparator implements Comparator {
  protected boolean isSortAsc;

  public TabComparator( boolean sortAsc) {
    isSortAsc = sortAsc;
  }

  public int compare(Object o1, Object o2) {
    if (!(o1 instanceof Integer) || !(o2 instanceof Integer))
      return 0;
    Integer s1 = (Integer) o1;
    Integer s2 = (Integer) o2;
    int result = 0;
    result = s1.compareTo(s2);
    if (!isSortAsc)
      result = -result;
    return result;
  }

  public boolean equals(Object obj) {
    if (obj instanceof TabComparator) {
        TabComparator compObj = (TabComparator) obj;
      return compObj.isSortAsc == isSortAsc;
    }
    return false;
  }
}