Deselect All Cells in a JTable in Java



In this article, we will learn to deselect all cells in a JTable in Java. The JTable component is a powerful tool for displaying tabular data when working with Java Swing applications. Sometimes, you may want to deselect all selected cells in a JTable programmatically.

Why Deselecting Cells in JTable is Important

In certain scenarios, such as resetting form fields, implementing "Clear Selection" buttons, or preparing the table for a fresh data display, you need to clear the selection in a JTable. This improves user experience and ensures the application behaves as expected.

Deselecting All Cells in JTable

Following are the steps to deselect all cells in a JTable in Java ?

  • Initialize Table Model: The DefaultTableModel class is used to manage the data and structure of the table.
  • Add Columns and Rows: Columns and rows of data are added to represent a tutorial tracker.
  • Select a Row: The addRowSelectionInterval method selects a specific row.
  • Deselect All Cells: The clearSelection() method clears any selected rows or cells in the table.
  • Display the Table: A JFrame and JScrollPane are used to render the table in a GUI window

Initialization of table model and JTable ?

DefaultTableModel tableModel = new DefaultTableModel();
JTable table = new JTable(tableModel);

Selecting a specific row in Jtable ?

table.addRowSelectionInterval(2, 2);

Clear all selections ?

table.clearSelection();

At first, let's say, we have selected a row using addRowSelectionInterval() as shown in the demo screenshot ?

Now, we will deselect all these cells using clearSelection() as shown in the following example. This method clears the selected cells from the table ?

Example

Below is an example of deselecting all cells in a JTable in Java ?

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 a row
      table.addRowSelectionInterval(2, 2);
      // deselecting
      table.clearSelection();
      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 cells is selected ?

Output

Conclusion

Using the clearSelection method is a simple and effective way to deselect all cells in a JTable. The above example provides a clear understanding of how to implement this functionality in your Java Swing applications. Incorporate this feature to enhance user experience and streamline your application's behavior.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-31T22:02:44+05:30

557 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements