在 Java 中设置表格模型中的单元格是否可选或取消选择?
我们可以使用 setCellSelectionEnabled() 设置或禁止表格中单元格的选择。以下是一个示例。−
如果要允许选择单元格,则将该方法设置为 TRUE −
table.setCellSelectionEnabled(true);
如果要禁止选择单元格,则将该方法设置为 FALSE −
table.setCellSelectionEnabled(false);
在这里,我们禁止选择单元格 −
举例
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"}); table.setCellSelectionEnabled(false); JFrame f = new JFrame(); f.setSize(600, 400); f.add(new JScrollPane(table)); f.setVisible(true); } }
输出
广告