2

大家!

在我的应用程序中,我需要创建一个被动 JTable。被动是指行的选择不是由 JTable 直接完成的,而是在另一个组件请求时完成的。因此,当用户转到新行时,表格不会立即做出反应,而是首先要求数据集根据所需的新行更新其内部状态,然后数据集回调表格以进行实际选择。所以我只是想在表格中选择新行之前执行一个操作。

我为您创建了一个小原型,让您了解我想要什么。在原型下方,您会找到我的问题。

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout(new BorderLayout());

        final JLabel selectedRow = new JLabel();

        final Table table = new Table();
        table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener()
            {
                @Override
                public void valueChanged(ListSelectionEvent e)
                {
                    if (!e.getValueIsAdjusting())
                    {
                        selectedRow.setText(
                            "Selected row: " + table.getSelectedRow());
                    }
                }
            }
        );

        new DataSet(table);

        add(new JScrollPane(table), BorderLayout.CENTER);
        add(selectedRow, BorderLayout.PAGE_END);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Table Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(
            new Runnable()
            {
                @Override
                public void run()
                {
                    createAndShowGUI();
                }
            }
        );
    }
}

class DataSet
{
    private final Table _table;
    private int _currentIndex;

    DataSet(Table table)
    {
        _table = table;
        _table.setDataSet(this);
    }

    int getCurrentIndex()
    {
        return _currentIndex;
    }

    void moveTo(int index) throws MovementException
    {
        if (index < 0 || index > 4)
        {
            throw new IndexOutOfBoundsException();
        }
        // Let's suppose there was a problem moving to the 2nd index. Maybe
        // the data set was in edit mode and couldn't persist the changes
        // because of a validation error.
        if (index == 2)
        {
            throw new MovementException();
        }
        _currentIndex = index;
        // Notifies the table that the data was moved so that the table can
        // update its selection model based on the current index of the
        // data set.
        _table.dataMoved();
    }
}

class MovementException extends RuntimeException
{
}

class Table extends JTable
{
    private DataSet _dataSet;
    // When true signals that the data was moved in the data set, so selection
    // is allowed.
    private boolean _dataMoved;
    // Previous selected column.
    private int _oldSelectedColumn;

    Table()
    {
        super(new Model());

        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setCellSelectionEnabled(true);
        getTableHeader().setReorderingAllowed(false);
        setPreferredScrollableViewportSize(new Dimension(500, 170));

        getColumnModel().setSelectionModel(new ColumnSelectionModel());
    }

    void setDataSet(DataSet dataSet)
    {
        _dataSet = dataSet;
    }

    // Called by DataSet#moveTo.
    void dataMoved()
    {
        _dataMoved = true;
        try
        {
            int rowIndex = _dataSet.getCurrentIndex();
            // Select the new row.
            setRowSelectionInterval(rowIndex, rowIndex);
        }
        finally
        {
            _dataMoved = false;
        }
    }

    @Override
    protected ListSelectionModel createDefaultSelectionModel()
    {
        return new RowSelectionModel();
    }

    private class ColumnSelectionModel extends DefaultListSelectionModel
    {
        @Override
        public void setSelectionInterval(int index0, int index1)
        {
            // Save the old selected column to be restored in
            // RowSelectionModel#setSelectionInterval in case of an error.
            _oldSelectedColumn = getSelectedColumn();
            super.setSelectionInterval(index0, index1);
        }
    }

    private class RowSelectionModel extends DefaultListSelectionModel
    {
        @Override
        public void setSelectionInterval(int index0, int index1)
        {
            if (_dataMoved || index1 == _dataSet.getCurrentIndex())
            {
                super.setSelectionInterval(index0, index1);
            }
            else
            {
                try
                {
                    _dataSet.moveTo(index1);
                }
                catch (MovementException ex)
                {
                    // There was a problem in the data set. Restore the old
                    // selected column.
                    setColumnSelectionInterval(
                    _oldSelectedColumn, _oldSelectedColumn);
                    throw ex;
                }
            }
        }
    }

    private static class Model extends AbstractTableModel
    {
        private String[] columnNames =
            {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
        private Object[][] data = {
            {"Kathy", "Smith", "Snowboarding", 5, false},
            {"John", "Doe", "Rowing", 3, true},
            {"Sue", "Black", "Knitting", 2, false},
            {"Jane", "White", "Speed reading", 20, true},
            {"Joe", "Brown", "Pool", 10, false}
        };

        public int getColumnCount()
        {
            return columnNames.length;
        }

        public int getRowCount()
        {
            return data.length;
        }

        public String getColumnName(int col)
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col)
        {
            return data[row][col];
        }

        public Class<?> getColumnClass(int c)
        {
            return getValueAt(0, c).getClass();
        }

        public void setValueAt(Object value, int row, int col)
        {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
    }
}
  • 你觉得这个设计有什么缺陷吗?
  • 我是否需要重写ColumnSelectionModelRowSelectionModel类中的更多方法来强制合同,还是只需要setSelectionInterval方法就足够了?到目前为止,我还没有发现这方面的任何缺陷。
  • 拥有ColumnSelectionModel类真的让我很恼火。它的目的只是在选择新列之前捕获旧的选定列,以便在出现问题时可以在RowSelectionModel#setSelectionInterval中恢复它。我不能只使用RowSelectionModel类来做到这一点。还有其他方法吗?

还有另一种不使用选择模型的方法。你可以这样做:

注释getColumnModel().setSelectionModel(new ColumnSelectionModel());表构造函数中的行。

注释方法Table#createDefaultSelectionModel方法。

Table#dataMoved方法替换为:

void dataMoved()
{
    _dataMoved = true;
    try
    {
        int rowIndex = _dataSet.getCurrentIndex();
        changeSelection(rowIndex, getSelectedColumn(), false, false);
    }
    finally
    {
        _dataMoved = false;
    }
}

覆盖Table#changeSelection方法:

@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
{
    if (_dataMoved)
    {
        super.changeSelection(rowIndex, columnIndex, toggle, extend);
    }
    else
    {
        if (rowIndex != _dataSet.getCurrentIndex())
        {
            _dataSet.moveTo(rowIndex);
        }
        super.changeSelection(_dataSet.getCurrentIndex(), columnIndex, toggle, extend);
    }
}

但是我没有使用这种方法,尽管它比选择模型简单得多,因为changeSelection方法的文档说:

UI 接收到的键盘或鼠标事件导致的选择的大多数更改都是通过此方法引导的,因此该行为可以被子类覆盖。

所以我解释了

大多数变化

由于并非所有更改,这意味着可能会有一些选择更改未通过此方法。我是对的还是我可以相信changeSelection方法?

先感谢您。

马科斯

4

1 回答 1

2

您的方法的主要缺陷是您的(导航)模型(又名:DataSet)和视图之间的硬连线双向耦合。出路类似于 VetoableSelectionModel:然后您可以将 DataSet 注册为 vetoablePropertyChangeListener 到选择模型,这是松散耦合,可以在不对表进行子类化的情况下进行配置。

整体布线的一些代码片段:

final JTable table = new JTable(new Model());
VetoableListSelectionModel selectionModel = new VetoableListSelectionModel();
table.setSelectionModel(selectionModel);
VetoableChangeListener veto = new VetoableChangeListener() {

    @Override
    public void vetoableChange(PropertyChangeEvent evt)
            throws PropertyVetoException {
        if (2 == (Integer) evt.getNewValue()) throw new PropertyVetoException("", evt);
    }
};
selectionModel.addVetoableChangeListener(veto);
table.getSelectionModel().addListSelectionListener(
    new ListSelectionListener()
    {
        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            if (!e.getValueIsAdjusting())
            {
                selectedRow.setText(
                    "Selected row: " + table.getSelectedRow());
            }
        }
    }
);

骨架选择模型(完整代码在swingx 孵化器中- 注意:未维护!)

/**
 * Quick impl of a list selection model which respects a veto before
 * changing selection state. The veto is effect in SINGLE_SELECTION mode
 * only.
 */
public class VetoableListSelectionModel extends DefaultListSelectionModel {
    private VetoableChangeSupport vetoableChangeSupport;

    /**
     * Defaults to SINGLE_SELECTION mode.
     *
     */
    public VetoableListSelectionModel() {
        super();
        setSelectionMode(SINGLE_SELECTION);
    }

    @Override
    public void setSelectionInterval(int index0, int index1) {
        if (isVetoable()) {
            try {
                fireVetoableChange(getMinSelectionIndex(), index0);
            } catch (PropertyVetoException e) {
                // vetoed - do nothing
                return;
            }
        }
        super.setSelectionInterval(index0, index1);
    }

    // similar for all methods that change the selection
    ...

    // methods to add/remove listeners and fire the event
    ...
}
于 2013-04-06T13:04:09.840 回答