How to deselect a range of rows from a table in Java?

Java 8Object Oriented ProgrammingProgramming

Let’s say, we have selected a range of rows using addRowSelectionInterval() as shown in the demo screenshot −

Now, we will deselect the above shown selected rows using removeRowSelectionInterval(). The range is to be set here for interval i.e rows 3 to 6 (index 2 to 5) will get deselected −

table.removeRowSelectionInterval(2, 5);

The following is our example to deselect a range of rows −

Example

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

Following is the output. Now you can see none of the row is selected −

raja
Updated on 30-Jul-2019 22:30:26

Advertisements