java - JTable 不使用 CTRL 进行多选

标签 java swing jtable

我正在尝试更改 JTable 的选择行为,以便能够在不使用 CTRL 修饰符的情况下向选择添加和删除行。方法:

public void changeSelection(int rowIndex,
                        int columnIndex,
                        boolean toggle,
                        boolean extend)

似乎正是我正在寻找的,特别是这种情况:

toggle: true, extend: false. If the specified cell is selected, deselect it. If it is not selected, select it. 

这就是我的意思。问题是我无法让它发挥作用。也许我缺少一些有关内部 JTable 工作原理的信息,但这是我的代码:

initComponents();

    mainTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    mainTable.setRowSelectionAllowed(true);

    mainTable.setSelectionModel(new DefaultListSelectionModel() {
        @Override
        public void addSelectionInterval(int index0, int index1) {
            if (index0 == index1) {
                for (int i = 0; i < mainTable.getColumnModel().getColumnCount(); i++) {
                    mainTable.changeSelection(index0, i, true, false);
                }
            }
        }

    });

这似乎什么也没做。谁能告诉我出了什么问题吗?

谢谢。

最佳答案

您可以创建自定义ListSelectionModel

简单的例子:

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

public class ToggleListSelectionModel extends DefaultListSelectionModel
{
    @Override
    public void setSelectionInterval(int index0, int index1)
    {
        //  Select multiple lines

        if (index0 != index1)
        {
            super.addSelectionInterval(index0, index1);
            return;
        }

        //  Toggle selection of a single line

        if  (super.isSelectedIndex(index0))
        {
            super.removeSelectionInterval(index0, index0);
        }
        else
        {
            super.addSelectionInterval(index0, index0);
        }
    }

    private static void createAndShowGUI()
    {
        String[] numbers = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
        final JList<String> list = new JList<String>( numbers );
        list.setVisibleRowCount( numbers.length );
        list.setSelectionModel(new ToggleListSelectionModel());
//      list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        JButton clear = new JButton("Clear");
        clear.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                list.clearSelection();
            }
        });

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(list), BorderLayout.CENTER);
        frame.add(clear, BorderLayout.PAGE_END);
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

该示例适用于 JList,但 JTable 也使用 ListSelectionModel。

关于java - JTable 不使用 CTRL 进行多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545591/

相关文章:

java - ORMLite id 始终为 null(没有 id 字段)

java - 听起来是 Java 语言。错误

Java - 为应用程序构建 GUI

java - JScrollPane 内的 JPanel : Vertical bar is not appearing

java - jTable 中的时间表

java - 如何将 JTable 放入 JScrollPane 中?

java - JScrollPane (for JTable) 在 MigLayout 中填充和增长

java - JAX-RS:如何扩展应用程序类来扫描包?

java - Windows 中的 Hadoop 错误

java - JTree 更新节点而不折叠