在对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);

最佳答案

Table Cell Listener博客中:

总之,在上述特殊情况下,TableCellListener类可以替代TableModelListener。除了实现TableModelListener之外,您还可以实现一个Action。

由于TableModelListener适用于TableModel,因此行/列值将相对于TableModel。因此,TableCellListener的行/列值也相对于TableModel

如果要使用相对于表的值,则需要使用:

int row = table.convertRowIndexToViw( modelRow );
int column = table.convertColumnIndexToView( modelColumn );

07-27 17:38