Java 不使用CTRL键的JTable多选

Java 不使用CTRL键的JTable多选,java,swing,jtable,Java,Swing,Jtable,我试图更改JTable的选择行为,以便能够在不使用CTRL修改器的情况下向选择中添加和删除行。方法: public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) 似乎正是我想要的,特别是: toggle: true, extend:

我试图更改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);
                }
            }
        }

    });
initComponents();
mainTable.setSelectionMode(ListSelectionModel.MULTIPLE\u INTERVAL\u SELECTION);
mainTable.setRowSelectionAllowed(真);
mainTable.setSelectionModel(新的DefaultListSelectionModel(){
@凌驾
public void addSelectionInterval(int index0,int index1){
如果(index0==index1){
对于(int i=0;i
这似乎毫无作用。谁能告诉我有什么问题吗


谢谢。

您可以创建自定义的
列表选择模型

简单的例子:

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();
            }
        });
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类ToggleListSelectionModel扩展了DefaultListSelectionModel
{
@凌驾
公共无效设置选择间隔(int index0,int index1)
{
//选择多行
如果(index0!=index1)
{
super.addSelectionInterval(index0,index1);
返回;
}
//切换单行的选择
如果(超级isSelectedIndex(index0))
{
super.removeSelectionInterval(index0,index0);
}
其他的
{
super.addSelectionInterval(index0,index0);
}
}
私有静态void createAndShowGUI()
{
字符串[]数字={“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”};
最终JList列表=新JList(编号);
list.setVisibleRowCount(number.length);
list.setSelectionModel(新的ToggleListSelectionModel());
//list.setSelectionMode(ListSelectionModel.MULTIPLE\u INTERVAL\u SELECTION);
JButton clear=新JButton(“clear”);
clear.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
list.clearSelection();
}
});
JFrame=新JFrame(“SSCCE”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
添加(新的JScrollPane(列表),BorderLayout.CENTER);
框架。添加(清晰,边框布局。第页\结尾);
frame.setLocationByPlatform(真);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowGUI();
}
});
}
}

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

您可以创建自定义的
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();
            }
        });
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类ToggleListSelectionModel扩展了DefaultListSelectionModel
{
@凌驾
公共无效设置选择间隔(int index0,int index1)
{
//选择多行
如果(index0!=index1)
{
super.addSelectionInterval(index0,index1);
返回;
}
//切换单行的选择
如果(超级isSelectedIndex(index0))
{
super.removeSelectionInterval(index0,index0);
}
其他的
{
super.addSelectionInterval(index0,index0);
}
}
私有静态void createAndShowGUI()
{
字符串[]数字={“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”};
最终JList列表=新JList(编号);
list.setVisibleRowCount(number.length);
list.setSelectionModel(新的ToggleListSelectionModel());
//list.setSelectionMode(ListSelectionModel.MULTIPLE\u INTERVAL\u SELECTION);
JButton clear=新JButton(“clear”);
clear.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
list.clearSelection();
}
});
JFrame=新JFrame(“SSCCE”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
添加(新的JScrollPane(列表),BorderLayout.CENTER);
框架。添加(清晰,边框布局。第页\结尾);
frame.setLocationByPlatform(真);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowGUI();
}
});
}
}
该示例适用于JList,但JTable也使用ListSelectionModel。

这仍然适用于(Java 8)和
JTable
。这仍然适用于(Java 8)和
JTable