How to make Scroll pane static?

萝らか妹 提交于 2020-01-02 16:34:53

问题


Observation::

Whenever the user reaches the last cell in the table and press Tab key, the focus is shifted to the first cell i.e the top the table.

Brought the table view back to the last cell, by using the function table.scrollRectToVisible(rect), but due this there is movement and looks like there is a false change in the table values.

The Scenario :: If I have make the make Scroll pane Static at particular position, so that I can control the movement. How can make this possible..

Thank You in Advance...


回答1:


The basic approach is to wrap the table's default navigation action into a custom Action which checks for the current cell: if it's the last, do nothing otherwise execute the wrapped action

code example:

    Object key = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
        .get(KeyStroke.getKeyStroke("ENTER"));
    final Action action = table.getActionMap().get(key);
    Action custom = new AbstractAction("wrap") {

        @Override
        public void actionPerformed(ActionEvent e) {
            // implement your prevention logic
            // here: don't perform if the current cell focus is the very cell of the table
            int row = table.getSelectionModel().getLeadSelectionIndex();
            if (row == table.getRowCount() - 1) {
                int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
                if (column == table.getColumnCount() -1) {
                    // if so, do nothing and return
                    return;
                }
            }
            // if not, call the original action
            action.actionPerformed(e);
        }

    };
    table.getActionMap().put(key, custom);


来源:https://stackoverflow.com/questions/7551502/how-to-make-scroll-pane-static

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!