before cell select jtable event

后端 未结 3 1227
再見小時候
再見小時候 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.

    0 讨论(0)
  • 2021-01-12 23:36

    The only way to do this that i can think of is handle the MouseEvent and using MouseAdapters, get the coordinates and somehow to check whether the mouse pointer is hovering over a cell or not, if it is, do what you want to do. you probably have to do addMouseListener to get the effect.

    0 讨论(0)
  • 2021-01-12 23:50

    whatever the goal is that you want to achieve: thinking "mouseEvent" is not enough, selection might change for other reasons (f.i. keyboard input, programmatic trigger, ..). Reverting an unwated change in a listener is not an option: as you already noted that would require to keep a duplicate of the selection and might confuse other listeners.

    The only way (that I see, could be others, of course ;-) is not to let it happen in the first place: implement a List SelectionModel which doesn't change the selection if certain conditions are met. My favourite (biased me :-) is a VetoableListSelectionModel It's a subclass of DefaultListSelectionModel which in SingleSelectionMode waits for vetoes from interested parties before actually changing.

    Here's a (raw) code snippet using it:

        VetoableListSelectionModel vetoableSelection = new VetoableListSelectionModel();
        VetoableChangeListener navigationController = new VetoableChangeListener() {
    
            public void vetoableChange(PropertyChangeEvent evt)
                    throws PropertyVetoException {
                // custom method that implements your condition 
                if (!canSelect((int) evt.getOldValue(), (int) evt.getNewValue()))
                    throw new PropertyVetoException("uncommitted changes",
                            evt);
            }
    
        };
        vetoableSelection.addVetoableChangeListener(navigationController);
        myTable.setSelectionModel(vetoableSelection);
    
    0 讨论(0)
提交回复
热议问题