如何在 Java 中取消选择表中的一系列行?
假设我们已使用演示截图中所示的 addRowSelectionInterval() 对一系列行进行了选择 −
现在,我们将使用 removeRowSelectionInterval() 取消对上方所显示已选择行的选择。此处的范围应设置为间隔,即第 3 至 6 行(索引 2 至 5)将取消选择 −
table.removeRowSelectionInterval(2, 5);
以下是取消选择一系列行的示例 −
示例
package my; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel); tableModel.addColumn("Language/ Technology"); tableModel.addColumn("Text Tutorial"); tableModel.addColumn("Video Tutorial"); tableModel.addColumn("Interview QA"); tableModel.addRow(new Object[] { "ASP.NET", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "F#", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "SharePoint", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "AWS", "No", "No", "Yes"}); tableModel.addRow(new Object[] { "Blockchain", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "C#", "Yes", "Yes", "Yes"}); tableModel.addRow(new Object[] { "Java", "Yes", "No", "Yes"}); tableModel.addRow(new Object[] { "NodeJS", "No", "Yes", "Yes"}); tableModel.addRow(new Object[] { "MVC", "Yes", "No", "Yes"}); // selected rows table.setColumnSelectionAllowed(false); table.setRowSelectionAllowed(true); table.addRowSelectionInterval(2, 5); // deselecting rows table.removeRowSelectionInterval(2,5); JFrame f = new JFrame(); f.setSize(600, 400); f.add(new JScrollPane(table)); f.setVisible(true); } }
以下为输出。现在可以看到没有任何行被选中 −
广告