禁用多列选择

时间:2015-06-24 21:51:05

标签: java swing jtable

有没有办法禁用Swing JTable的多列选择?我已经在" Tid"中禁用了所有选择。通过覆盖选择模型的选择间隔来列:

myTable.getColumnModel().setSelectionModel(new DefaultListSelectionModel() {
            private boolean isSelectable(int index0, int index1) {
                return index1 != 0;
            }

            @Override
            public void setSelectionInterval(int index0, int index1) {
                if(isSelectable(index0, index1)) {
                    super.setSelectionInterval(index0, index1);
                }
            }

            @Override
            public void addSelectionInterval(int index0, int index1) {
                if(isSelectable(index0, index1)) {
                    super.addSelectionInterval(index0, index1);
                }
            }
        });

我的猜测是,人们也可以通过覆盖选择模型中的方法来禁止选择多个列。但我无法弄清楚如何实现这一目标。

允许选择 Allowed Selection - the selection spans only one column but multiple rows

不允许选择 Disallowed Selection - the selection spans multiple columns and multiple rows

2 个答案:

答案 0 :(得分:4)

首先从TableColumnModel

获取JTable
TableColumnModel columnModel = table.getColumnModel();

接下来,获取LstSeletionModel

TableColumnModel
ListSelectionModel selectionModel = columnModel.getSelectionModel();

有了这个,您可以设置模型将使用的selectionMode,例如

selectionModel.setSelectionModel(ListSelectionModel.SINGLE_SELECTION)

有关详细信息,请参阅ListSelectionModel的JavaDoc和TableColumnModel

Runnable example ....

TableSelection

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
import javax.swing.JScrollPane;
    import javax.swing.JTable;
import javax.swing.ListSelectionModel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

    public class Test {

        public static void main(String[] args) {
            new Test();
        }

        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }

        public class TestPane extends JPanel {

            public TestPane() {
                setLayout(new BorderLayout());

                DefaultTableModel model = new DefaultTableModel(0, 10);
                for (int row = 0; row < 10; row++) {
                    String[] data = new String[10];
                    for (int col = 0; col < 10; col++) {
                        data[col] = row + "x" + col;
                    }
                    model.addRow(data);
                }


                JTable table = new JTable(model);
                table.setColumnSelectionAllowed(true);
                table.setRowSelectionAllowed(true);
                table.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                add(new JScrollPane(table));
            }
        }

    }

答案 1 :(得分:0)

实际上,这对我已经存在的覆盖是一个简单的补充。

@Override
public void setSelectionInterval(int index0, int index1) {
    if (isSelectable(index0, index1)) {
        if (index0==index1) { //The if condition needed.
            super.setSelectionInterval(index0, index1);             
        }
    }
}

我在审核JavaDoc和DefaultListSelectionModel后意识到index0index1正是我所寻找的 - 列跨度。因此,当且仅当两个列索引相等时,才调用超类,因此无法选择多个列。