Java Program to deselect a range of columns in a JTable

Java 8Object Oriented ProgrammingProgramming

At first, let’s say, we have selected a range of columns using addColumnSelectionInterval() as shown in the demo screenshot −

Now, we will deselect the above shown selected columns using removeColumnSelectionInterval(). The range is to be set here for interval i.e column 1 to 2 will get deselected −

table.removeColumnSelectionInterval(1, 2);

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

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"});
      table.setColumnSelectionAllowed(true);
      table.addColumnSelectionInterval(1, 2);
      table.setRowSelectionAllowed(false);
      table.removeColumnSelectionInterval(1, 2);
      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 column is selected −

raja
Published on 06-Jun-2019 13:18:22
Advertisements