在我对JTable进行排序后,我在更改当前行中表的值时遇到了困难...我正在使用TableCellListener类来监听某列中表上的更改值并更改使用新值将值上传到MySQL。问题是,在我解决之后,例如,我按照我的表中的客户ID进行排序,当我更改状态的值时,它会改变"使用的行"在那里。
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);
答案 0 :(得分:1)
来自Table Cell Listener博客:
总之,在上述特殊情况下,TableCellListener类可以用作TableModelListener的替代。您可以实现Action。
,而不是实现TableModelListener由于TableModelListener
适用于TableModel
,因此行/列值将相对于TableModel
。因此TableCellListener
的行/列值也相对于TableModel
。
如果您想要相对于表的值,则需要使用:
int row = table.convertRowIndexToViw( modelRow );
int column = table.convertColumnIndexToView( modelColumn );