禁用多列选择

Jer*_*emi 2 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);
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

我的猜测是,还可以通过覆盖选择模型中的方法来禁止选择多列。但我真的不知道如何做到这一点。

允许选择 允许的选择 - 选择仅跨越一列但跨越多行

不允许的选择 不允许的选择 - 选择跨越多列和多行

Mad*_*mer 5

首先TableColumnModelJTable

TableColumnModel columnModel = table.getColumnModel();
Run Code Online (Sandbox Code Playgroud)

接下来,拿到LstSeletionModelTableColumnModel

ListSelectionModel selectionModel = columnModel.getSelectionModel();
Run Code Online (Sandbox Code Playgroud)

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

selectionModel.setSelectionModel(ListSelectionModel.SINGLE_SELECTION)
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅ListSelectionModelTableColumnModel的 JavaDocs

可运行示例....

表选择

    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));
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)