我在排序JTable后改变当前行中表的值时遇到困难...我使用TableCellListener类来侦听表中某个特定值列并更改值并上传到具有新值的MySQL中。问题是,在我分类之后,例如,当我改变状态的值时,我按照客户ID排序,当它改变状态的值时,它就会从“used”那一行开始改变。Sorter从原始序列中获取值
TableCellListener.java --->这个类是用来听的新值输入
public class TableCellListener implements PropertyChangeListener, Runnable
{
private JTable table;
private Action action;
private int row;
private int column;
private Object oldValue;
private Object newValue;
/**
* Create a TableCellListener.
*
* @param table the table to be monitored for data changes
* @param action the Action to invoke when cell data is changed
*/
public TableCellListener(JTable table, Action action)
{
this.table = table;
this.action = action;
this.table.addPropertyChangeListener(this);
}
/**
* Create a TableCellListener with a copy of all the data relevant to
* the change of data for a given cell.
*
* @param row the row of the changed cell
* @param column the column of the changed cell
* @param oldValue the old data of the changed cell
* @param newValue the new data of the changed cell
*/
private TableCellListener(JTable table, int row, int column, Object oldValue, Object newValue)
{
this.table = table;
this.row = row;
this.column = column;
this.oldValue = oldValue;
this.newValue = newValue;
}
/**
* Get the column that was last edited
*
* @return the column that was edited
*/
public int getColumn()
{
return column;
}
/**
* Get the new value in the cell
*
* @return the new value in the cell
*/
public Object getNewValue()
{
return newValue;
}
/**
* Get the old value of the cell
*
* @return the old value of the cell
*/
public Object getOldValue()
{
return oldValue;
}
/**
* Get the row that was last edited
*
* @return the row that was edited
*/
public int getRow()
{
return row;
}
/**
* Get the table of the cell that was changed
*
* @return the table of the cell that was changed
*/
public JTable getTable()
{
return table;
}
@Override
public void propertyChange(PropertyChangeEvent e)
{
// A cell has started/stopped editing
if ("tableCellEditor".equals(e.getPropertyName()))
{
if (table.isEditing())
processEditingStarted();
else
processEditingStopped();
}
}
/*
* Save information of the cell about to be edited
*/
private void processEditingStarted()
{
// The invokeLater is necessary because the editing row and editing
// column of the table have not been set when the "tableCellEditor"
// PropertyChangeEvent is fired.
// This results in the "run" method being invoked
SwingUtilities.invokeLater(this);
}
/*
* See above.
*/
@Override
public void run()
{
row = table.convertRowIndexToModel(table.getEditingRow());
column = table.convertColumnIndexToModel(table.getEditingColumn());
oldValue = table.getModel().getValueAt(row, column);
newValue = null;
}
/*
* Update the Cell history when necessary
*/
private void processEditingStopped()
{
newValue = table.getModel().getValueAt(row, column);
// The data has changed, invoke the supplied Action
if (! newValue.equals(oldValue))
{
// Make a copy of the data in case another cell starts editing
// while processing this change
TableCellListener tcl = new TableCellListener(
getTable(), getRow(), getColumn(), getOldValue(), getNewValue());
ActionEvent event = new ActionEvent(
tcl,
ActionEvent.ACTION_PERFORMED,
"");
action.actionPerformed(event);
}
}
}
这是我如何使用它在表上...
Action action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
TableCellListener tcl = (TableCellListener)e.getSource();
if (tcl.getColumn() == 9) {
System.out.println(tcl.getNewValue() + " " + (int) t_op.getValueAt(tcl.getRow(), 7));
Object[] options = {"Confirmar",
"Voltar"};
int n = JOptionPane.showOptionDialog(null, "Deseja inserir esse ID do Pre-Boleto?", "Confirma\u00E7\u00E3o",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
if (n == 0) {
OpPreBolIDWorker sqlw = new OpPreBolIDWorker(t_op, (int) tcl.getNewValue(), (int) t_op.getValueAt(tcl.getRow(), 7));
sqlw.execute();
LiqPreBolIDWorker lfiw = new LiqPreBolIDWorker(t_op, (int) tcl.getNewValue(), (int) t_op.getValueAt(tcl.getRow(), 7));
lfiw.execute();
} else if (n == 1) {
t_op.setValueAt(tcl.getOldValue(), tcl.getRow(), tcl.getColumn());
}
}
if (tcl.getColumn() == 13) {
CobrancaWorker cw = new CobrancaWorker(t_op, tcl.getNewValue().toString(), (int) t_op.getValueAt(tcl.getRow(), 7));
cw.execute();
}
}
};
TableCellListener tcl = new TableCellListener(t_op, action);
控制台打印出排序后“使用”在该位置的行的值...
这就是我如何整理我的表格。
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(t_op.getModel()) {
@Override
public void toggleSortOrder(int column) {
if (column >= 0 && column < getModelWrapper().getColumnCount() && isSortable(column)) {
List<SortKey> keys = new ArrayList<SortKey>(getSortKeys());
if (!keys.isEmpty()) {
SortKey sortKey = keys.get(0);
if (sortKey.getColumn() == column && sortKey.getSortOrder() == SortOrder.DESCENDING) {
setSortKeys(null);
return;
}
}
}
super.toggleSortOrder(column);
}
};
t_op.setRowSorter(sorter);
非常感谢您!这工作就像一个魅力!我试了几个小时试图让这个问题得到解决,我只好回到他们的博客。谢谢! – SunnyH