繁体   English   中英

具有单元格选择和多个时间间隔选择的JTable显示错误的选择

[英]JTable with cell selection and multiple interval selection shows incorrect selection

我想要一张桌子,人们可以自由选择单元格。

为此,我使用了ListSelectionModel.MULTIPLE_INTERVAL_SELECTION和table.setCellSelectionEnabled(true)

我有2个问题:

  • 方法table.addRowSelectionInterval不选择任何单元格
  • 如果选择2个块,则也会选择具有相同行和列的单元格。 例如,选择B2-D4,然后选择F7。 这还将选择B7-D7和F2-F4。

这是重现这些问题的程序:

import java.awt.BorderLayout;
import javax.swing.*;

public class JTableCellSelection {
    public static void showDemo(JComponent demo, String title) {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setTitle(title);
        JPanel contentPanel = new JPanel(new BorderLayout());

        contentPanel.add(demo);

        mainFrame.add(contentPanel);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        JTable table = new JTable(10, 10);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        table.setCellSelectionEnabled(true);
        table.addRowSelectionInterval(6, 7); // Select 2 lines
        showDemo(new JScrollPane(table), "Select a block and some rows");
    }
}

感觉我将不得不自己检查选择,而不必依赖表格单元格渲染器的isSelected。

因此,我已经在JTable中实现了它。

请注意,此解决方案有几个缺点:

  • 您不能使用table.getListSelectionModel来选择行,需要调用table.addRowSelectionInterval
  • 尝试选择所选块旁边的另一列将取消选择行
  • 我还没有测试过列选择,但是我猜这是行不通的
  • 选择块时更改方向并非总是有效

但对于其余部分,它几乎可以满足我的要求

/*
 * Copyright 2013 Japplis.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 * The JTable used to display data.
 * This class is only to fix bugs or improve existing functionalities.
 *
 * @author Anthony Goubard - Japplis
 */
public class SheetTable extends JTable {

    private Map<Integer, Set<Integer>> selectedCells = new HashMap<>();
    private Point firstExtendCell;

    public SheetTable(TableModel tableModel) {
        super(tableModel);
    }

    @Override
    public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
        if (toggle && isCellSelected(rowIndex, columnIndex) && !extend) {
            selectedCells.get(rowIndex).remove(columnIndex);
        } else {
            if (!toggle && !extend) {
                selectedCells.clear();
            }
            Set<Integer> selectedColumns = selectedCells.get(rowIndex);
            if (selectedColumns == null) {
                selectedColumns = new TreeSet<>();
                selectedCells.put(rowIndex, selectedColumns);
            }
            selectedColumns.add(columnIndex);
            if (!extend) {
                firstExtendCell = new Point(rowIndex, columnIndex);
            } else {
                for (int i = Math.min(firstExtendCell.x, rowIndex); i <= Math.max(firstExtendCell.x, rowIndex); i++) {
                    for (int j = Math.min(firstExtendCell.y, columnIndex); j <= Math.max(firstExtendCell.y, columnIndex); j++) {
                        selectedCells.get(i).add(j);
                    }
                }
            }
        }
        super.changeSelection(rowIndex, columnIndex, toggle, extend);
    }

    @Override
    public void addRowSelectionInterval(int index0, int index1) {
        for (int i = index0; i < index1; i++) {
            selectedCells.remove(i);
        }
        super.addRowSelectionInterval(index0, index1);
    }

    @Override
    public void removeRowSelectionInterval(int index0, int index1) {
        for (int i = index0; i < index1; i++) {
            selectedCells.remove(i);
        }
        super.removeRowSelectionInterval(index0, index1);
    }

    @Override
    public void selectAll() {
        selectedCells.clear();
        super.selectAll();
    }

    @Override
    public void clearSelection() {
        if (selectedCells != null) {
            selectedCells.clear();
        }
        super.clearSelection();
    }

    @Override
    public boolean isCellSelected(int row, int column) {
        if (!getSelectionModel().isSelectedIndex(row)) {
            return false;
        }
        if (getSelectionModel().isSelectedIndex(row) && selectedCells.get(row) == null) {
            return true;
        }
        return selectedCells.get(row).contains(column);
    }
}

最新版本位于bitbucket.org上的Joeffice Mercurial存储库中

我做了一些实验,并在这里阅读了文档。 它说了setCellSelectionEnabled()方法:

设置此表是否允许同时存在列选择和行选择。 设置后,表格会将行和列选择模型的交集视为所选单元格。

我在showDemo()之前showDemo()添加了两行代码用于调试目的,以查看在添加行间隔之后所选择的行和列是什么:

public static void main(String[] args) {
    JTable table = new JTable(10, 10);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    table.setCellSelectionEnabled(true);
    table.addRowSelectionInterval(6, 7); // Select 2 lines

    // Let's see what rows and columns have been selected 

    System.out.println("Rows: " + Arrays.toString(table.getSelectedRows()));
    System.out.print("Columns: " + Arrays.toString(table.getSelectedColumns()));

    showDemo(new JScrollPane(table), "Select a block and some rows");
}

我得到以下输出:

Rows: [6, 7]
Columns: []

这就解释了为什么addRowSelectionInterval无法按预期工作。 添加缺少的部分很容易:

    ...
    table.addRowSelectionInterval(6, 7); // Select 2 lines
    table.addColumnSelectionInterval(0, 9); // and Select ** ALL** the columns
    ...

对于其他观察结果,可以得出类似的理由。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM