Java JTable在一行中选择多个连续的单元格
我需要能够仅在Java JTable在一行中选择多个连续的单元格,java,swing,jtable,multipleselection,Java,Swing,Jtable,Multipleselection,我需要能够仅在JTable中选择一行中的多个连续单元格 SINGLE_INTERVAL_SELECTION是最接近我需要的,但我不希望选择多行 单行,多列是我要做的 有没有办法做到这一点?也许您需要单一选择 您将需要: table.setRowSelectionAllowed ( false ); table.setCellSelectionEnabled ( true ); 另一种方式是: table.setColumnSelectionAllowed(true); table.setRow
JTable
中选择一行中的多个连续单元格
SINGLE_INTERVAL_SELECTION
是最接近我需要的,但我不希望选择多行
单行,多列是我要做的
有没有办法做到这一点?也许您需要
单一选择
您将需要:
table.setRowSelectionAllowed ( false );
table.setCellSelectionEnabled ( true );
另一种方式是:
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
无论采用哪种方式,您都可以使用Ctrl
键选择一行中的多个单元格
祝你好运。将行的
列表选择模式设置为单次选择
,将列模式设置为单次间隔选择
并告诉列模式
允许进行列选择
public class Main {
public static void main(String[] args) {
JTable jTable = new JTable();
TableColumnModel columnModel = jTable.getColumnModel();
columnModel.setColumnSelectionAllowed(true);
ListSelectionModel columnSelectionModel = columnModel.getSelectionModel();
columnSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
ListSelectionModel rowSelectionModel = jTable.getSelectionModel();
rowSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable.setModel(createExampleModel());
createFrameAndShow(jTable);
}
private static void createFrameAndShow(JTable jTable) {
JFrame mainFrame = new JFrame("JTable select multiple contiguous cells in a single row");
Container contentPane = mainFrame.getContentPane();
contentPane.add(jTable);
mainFrame.setSize(500, 100);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
}
private static DefaultTableModel createExampleModel() {
DefaultTableModel defaultTableModel = new DefaultTableModel();
defaultTableModel.setColumnCount(4);
defaultTableModel.addRow(new Object[] { "A1", "B1", "C1", "D1" });
defaultTableModel.addRow(new Object[] { "A2", "B2", "C2", "D2" });
defaultTableModel.addRow(new Object[] { "A3", "B3", "C3", "D3" });
return defaultTableModel;
}
}
将导致