Deselect a Range of Columns in JTable



In this article, we will learn how to deselect a range of columns in a JTable using Java. JTable is a part of the Swing framework in Java, which is used to display and edit tables. Sometimes, we may want to deselect a specific range of columns after selecting columns in a table. This can be done by using the removeColumnSelectionInterval() method.

Problem Statement

Given a JTable with columns selected, write a Java program to deselect a range of columns from the table.

Input

A JTable with columns selected.
Output
The specified range of columns will be deselected.

Steps to deselect a range of columns in JTable

Following are the steps to deselect a range of columns in JTable ?
  • Import the necessary classes from javax.swing and javax.swing.table packages.
  • Create a JTable with a default table model and some rows and columns of data.
  • Use the addColumnSelectionInterval() method to select a range of columns.
  • Use the removeColumnSelectionInterval() method to deselect a specific range of columns.
  • Display the table in a JFrame to observe the result.

The selected range of columns using addColumnSelectionInterval() is shown in the demo screenshot ?

Java program to deselect a range of columns in a JTable

The following is our example of deselecting a range of columns ?

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

Output

Code Explanation

In this program, we create a JTable with a default table model and add columns like "Language/Technology", "Text Tutorial", "Video Tutorial", and "Interview QA". We then populate the table with data. After enabling column selection, we select columns 1 and 2 using addColumnSelectionInterval(). Later, we deselect these columns using removeColumnSelectionInterval(). The table is displayed in a JFrame for visual confirmation.

Updated on: 2024-11-14T17:36:26+05:30

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements