Best Way of JTable Sorting in Java Netbeans with Details

jTable basically used for displaying information in the form of rows and columns. In Java you can easily design your table according to your need, so In this post we learn the Best way of jTable Sorting in Java NetBeans.

Easy Way to Add Coulmn Header and Change Grid Line Color of jTable in Java

Here, SKOTechLearn will explain row sorting of jTable with following way.

  1. Static way to Enable Row Sorting of jTable in Java
  2. Sort Particular Column of jTable in Java dynamically
  3. Disable Particular Column for Sorting of jTable in Java dynamically

Suppose we add some records in table. Now we have to proceed with above given point.


 (1). Static way to Enable Row Sorting of jTable in Java :

If you want to enable row sorting during design time. And sort it during run time then it can be done by two ways:

Enable Row Sorting through Properties in NetBeans

In NetBeans, you can just go to jTable’s "Properties" window, and find “autoCreateRowSorter” option from it.

Now, you have to check this option for enabling sorting like bellow.
AutoCreateRowSorter Setting in JTable in Netbeans
autoCreateRowSorter Setting
When you will check mark on “autoCreateRowSorter” and run application, you have to double click on any Column Header, the column will be sorted according to row’s value in ascending or descending way.
jTable Column Sorting in Java
jTable Column Sorting

Enable Row Sorting through code in Java NetBeans

if you want to set RowSorter through code then you can simply add this  "MyTableView.setAutoCreateRowSorter(true);" code in following way.
 public TableSortFrame() {     
   initComponents();
   //Here MyTableView is jTable   
   MyTableView.setAutoCreateRowSorter(true);
 }

So, this way you can easily create jTable with row sort setting in design time.


 (2). Sort Particular Column of jTable in Java dynamically :

Suppose, you make a project or application where you need to sort only specific column, then you have to follow with further steps.

Suppose, we want to sort Column No. 2 as describe above given table’s data. Now we sort “MyCol2” at runtime.

But first we have to import some classes:
 import javax.swing.table.TableModel;
 import java.util.ArrayList;
 import java.util.List;
 import javax.swing.table.TableRowSorter;
 import javax.swing.RowSorter;
 import javax.swing.SortOrder;

After that, we will drag jButton and write code for it like bellow:
 private void ColSortBtnActionPerformed(java.awt.event.ActionEvent evt) {  
   // MyTableView is jTable        
   TableRowSorter<TableModel> ColSort = new TableRowSorter<>(MyTableView.getModel());
   MyTableView.setRowSorter(ColSort);
   List<RowSorter.SortKey> ColSortingKeys = new ArrayList<>();
   //"SortColNo" is use to set Column No for Sorting
   int SortColNo = 1;
   //"SortKey" is used for sort order for a particular column
   ColSortingKeys.add(new RowSorter.SortKey(SortColNo, SortOrder.ASCENDING));
 
   ColSort.setSortKeys(ColSortingKeys);
   ColSort.sort();
  }       

In above code, We have changed jTable’s Name with “MyTableView”.

When you run jFrame. And click on “ColSortBtn”. This will show the following output:
jTable particular Column Sorting in Java
Friend, in above given way, you can easily sort any specific or particular column of jTable in Java at run time.

 Note: If you Double Click on any other column, then the sorting will also be apply on that column.


 (3). Disable Particular Column for Sorting of jTable in Java dynamically :

As you can see, the above given sorting process will be apply on any other column at run time.

If, you want to fix that only particular column will be sort and other column will not be used as sorting process. Then you have to disable that particular column for sorting.

Disable Column Sorting of jTable in Java

Suppose, you want to apply sorting in Column No. 3, and Disable Sorting in Column 1 and Column 2 then you will simply write following code for this process:
 private void ColSortBtnActionPerformed(java.awt.event.ActionEvent evt) {  
    TableRowSorter<TableModel> ColSort = new TableRowSorter<>(MyTableView.getModel());
    MyTableView.setRowSorter(ColSort);
    List<RowSorter.SortKey> ColSortingKeys = new ArrayList<>();
    //"SortColNo" is use to set Column No for Sorting
    int SortColNo = 2;
    //"SortKey" is used for sort order for a particular column
    ColSortingKeys.add(new RowSorter.SortKey(SortColNo, SortOrder.ASCENDING));
 
    ColSort.setSortKeys(ColSortingKeys);
    ColSort.sort();
    //Disable sorting in column 0 and column 1
    ColSort.setSortable(0, false);
    ColSort.setSortable(1, false);
 }
When you run it, this will work like following image.
Disable Column Sorting in jTable in Java
So Friends, you can try it yourself and find that what will happen. Simply create project in Java and write code for jTable Row sorting in Java Netbeans using SKOTechLearn Tips.

0 comments: