before cell select jtable event

后端 未结 3 1250
再見小時候
再見小時候 2021-01-12 22:54

Are there any event that is fired when cell is about to be selected? There is ListSelectionListener, but it has event that is fired only after selection has happened. I need

3条回答
  •  [愿得一人]
    2021-01-12 23:25

    Assuming you have set the selection mode of the ListSelectionModel to the desired value and added a listener, you may find it helpful to examine the predicate getValueIsAdjusting(), which "Returns true if the selection is undergoing a series of changes." In practical terms, it is true when the mouse is down, or when it is being dragged in one of the INTERVAL modes.

    It may also help to know more about the goal of the this effort, as another approach may be helpful. Naturally, an sscce is always in order.

    Addendum: This appears to work, but @kleopatra's approach would prevent losing the previous selection.

    private static final int FORBID = 5;
    class MySelectionListener implements ListSelectionListener {
    
        public void valueChanged(ListSelectionEvent e) {
            int selectedRow = table.getSelectedRow();
            if (selectedRow == FORBID) {
                selectionModel.removeIndexInterval(FORBID, FORBID);
                System.out.println(FORBID + " forbidden.");
            }
        }
    }
    

    user is forbidden to change row if he has not accepted change he made in that row.

    You could use a custom CellEditor that conditions stopCellEditing(), as shown in this example.

提交回复
热议问题