Select a Column in JTable using Java



In this article, we will learn how to select a specific column in a JTable using Java's Swing library. The program creates a simple table displaying a list of products along with their quantities. We use setColumnSelectionInterval() to highlight a single column based on an interval, so in this example, the "Quantity" column (column index 2) will be selected. The program also ensures that only columns, and not rows, can be selected.

Steps to select a column in JTable

Following are the steps to select a column in JTable ?

  • First we will import the classes from the javax.swing and java.awt package.
  • Set up the JFrame window and JPanel to hold the table.
  • Create a 2D array to store table data (product details and quantities) and an array for column headers.
  • Initialize the JTable with data and headers, and customize its appearance:
  • Set horizontal lines for better readability.
  • Enable column selection and disable row selection.
  • Use setColumnSelectionInterval(2,2) to select only the third column (index 2).
  • Add the table to a JScrollPane and then add it to the panel.
  • Display the frame.

Java program to select a column in JTable

The following is an example of selecting the column from a Jtable ?

package my;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.setBorder(BorderFactory.createTitledBorder(
         BorderFactory.createEtchedBorder(), "Stock", TitledBorder.CENTER, TitledBorder.TOP));
      String[][] rec = {
         { "001", "Shirts", "40" },
         { "002", "Trousers", "250" },
         { "003", "Jeans", "25" },
         { "004", "Applicances", "90" },
         { "005", "Mobile Phones", "200" },
         { "006", "Hard Disk", "150" },
      };
      String[] header = { "ID", "Product", "Quantity" };
      JTable table = new JTable(rec, header);
      table.setShowHorizontalLines(true);
      table.setGridColor(Color.blue);
      table.setColumnSelectionAllowed(true);
      table.setRowSelectionAllowed(false);
      table.setColumnSelectionInterval(2,2);
      panel.add(new JScrollPane(table));
      frame.add(panel);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

Output

Above, we have also set the setRowSelectionAllowed() as FALSE and setColumnSelectionAllowed() as TRUE since we need to select column here.

Code explanation

In the above code, we first set up the JFrame and JPanel, creating a titled border around the panel. We define the rec array for table data, with each entry representing a row in the table, and the header array for column names. The JTable is then initialized with rec and header. By setting setColumnSelectionAllowed(true) and setRowSelectionAllowed(false), we enable only column selection. Then, setColumnSelectionInterval(2,2) specifies that only the third column should be selected. Finally, the table is added to a scroll pane, added to the panel, and displayed in the frame.

Updated on: 2024-10-25T23:40:18+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements