⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e964. listening for selection events in a jtable component.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
To listen for selection changes, you need to add a listener to both the JTable's table model and to the table column model. 
A selection change event has a first and last property that specifies the range of affected rows (or columns). For example, if the 5th row is selected and the user selects the 7th row, the first property is 4 and the last is property 6. There is not enough information in the event to determine exactly which cells have changed. See e942 Getting the Selected Cells in a JTable Component for an example of how to determine the selected cells. 

Changing the selection programmatically (see e941 Programmatically Making Selections in a JTable Component) causes selection change events to be fired. 

    SelectionListener listener = new SelectionListener(table);
    table.getSelectionModel().addListSelectionListener(listener);
    table.getColumnModel().getSelectionModel()
        .addListSelectionListener(listener);
    
    public class SelectionListener implements ListSelectionListener {
        JTable table;
    
        // It is necessary to keep the table since it is not possible
        // to determine the table from the event's source
        SelectionListener(JTable table) {
            this.table = table;
        }
        public void valueChanged(ListSelectionEvent e) {
            // If cell selection is enabled, both row and column change events are fired
            if (e.getSource() == table.getSelectionModel()
                  && table.getRowSelectionAllowed()) {
                // Column selection changed
                int first = e.getFirstIndex();
                int last = e.getLastIndex();
            } else if (e.getSource() == table.getColumnModel().getSelectionModel()
                   && table.getColumnSelectionAllowed() ){
                // Row selection changed
                int first = e.getFirstIndex();
                int last = e.getLastIndex();
            }
    
            if (e.getValueIsAdjusting()) {
                // The mouse button has not yet been released
            }
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -