我必须在添加新行后对<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rentals_reservation" data-id="3407">1.Booking id: 3407</div>
<div class="rentals_reservation" data-id="3407">2.Booking id: 3407</div>
<div class="rentals_reservation" data-id="3407">3.Booking id: 3407</div>
<div class="rentals_reservation" data-id="3227">1.Booking id: 3227</div>
<div class="rentals_reservation" data-id="3227">2.Booking id: 3227</div>
<div class="rentals_reservation" data-id="3227">3.Booking id: 3227</div>
进行排序。
这是代码(没有工作)
import itertools as it
import matplotlib.pyplot as plt
import time
def printImage(M, k):
fig = plt.imshow(M, interpolation='nearest')
fig.set_cmap('hot')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.axis('off')
plt.tight_layout()
plt.savefig(str(k) + '.png', bbox_inches='tight', pad_inches=0, dpi=300)
plt.ioff()
n = 1000
t = 1000
with open('data', 'r') as f:
for i in range(t):
t0 = time.clock()
try:
items = [list(map(float, i.split())) for i in it.islice(f, n)]
except:
raise
else:
printImage(items,i)
t1 = time.clock()
print(str(i) + '/' + str(t) + ' ' + str(t1-t0))
输出结果:表(A,C,B),预期(A,B,C)
model - DefaultTableModel
deviceTable - Jtable
答案 0 :(得分:1)
model.fireTableDataChanged();
不要直接调用fireXXX()方法。
TableModel负责调用相应的方法(在本例中为fireTableRowsInserted)。
编辑:
删除创建分拣机和比较器的代码。
您只需让表格创建分拣机。但是,一旦你这样做,你需要告诉分拣机哪一列要排序:
table.setAutoCreateRowSorter(true);
((DefaultRowSorter)table.getRowSorter()).toggleSortOrder(0);
答案 1 :(得分:1)
根据您的示例,您需要定义一个排序键,否则该表实际上是未排序的......
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);
table.setRowSorter(sorter);
以下示例(基于您的)对第一列进行排序。当您点击该按钮时,它会添加行,然后对其进行排序
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
JTable table = new JTable();
table.setModel(new DefaultTableModel(null, new String[]{"col1"}));
TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
sorter.setComparator(0, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
sorter.setSortsOnUpdates(true);
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
sorter.setSortKeys(sortKeys);
table.setRowSorter(sorter);
JButton btn = new JButton("...");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new String[]{"A"});
model.addRow(new String[]{"C"});
model.addRow(new String[]{"B"});
}
});
frame.add(new JScrollPane(table));
frame.add(btn, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}