Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Monday, January 8, 2018

Sort a JTable with fixed / frozen columns

We would assume that the main table and the frozen table use the same table model. In order to have the frozen table sorted as well when the main table is sorted, you can share the table sorter of the main table with the frozen table.

//Make the main table sortable by clicking the column header
mainTable.setAutoCreateRowSorter(true);

frozenTable.setRowSorter(mainTable.getRowSorter());

//After sorting, the same number of row in the table is selected
frozenTable.setUpdateSelectionOnSort(false);

//After sorting, the same number of row in the model is selected
mainTable.setUpdateSelectionOnSort(true);

--------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Friday, December 13, 2013

JTable sort by clicking column header

Sample code for sorting a column by clicking the column header.

import java.awt.Dimension;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class TestClass extends javax.swing.JFrame {
    private DefaultTableModel tableModel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
 
    public TestClass() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1.setViewportView(jTable1);
        getContentPane().add(jScrollPane1);
     
        setPreferredSize(new Dimension(500,300));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
     
        Object[] headers = {"Item", "Fruit"};
        tableModel = new DefaultTableModel();
        tableModel.setColumnIdentifiers(headers);
        Vector row1 = new Vector();
        row1.add("Item1");
        row1.add("Papaya");
        Vector row2 = new Vector();
        row2.add("Item2");
        row2.add("Orange");
        Vector row3 = new Vector();
        row3.add("Item3");
        row3.add("Apple");
        tableModel.addRow(row1);
        tableModel.addRow(row2);
        tableModel.addRow(row3);
     
        jTable1.setModel(tableModel);
     
        jTable1.setAutoCreateRowSorter(true);
        TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(tableModel);
        sorter.setComparator(1, new SorterComparator<String>());
        jTable1.setRowSorter(sorter);
    }
 
    private class SorterComparator<E> implements Comparator {
        public int compare(Object s1, Object s2){
            return ((String)s1).compareTo((String)s2);
        }
    }
 
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestClass().setVisible(true);
            }
        });
    }
}

Further Readings:
1. JTable sort the content of a column

-------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

JTable sort the content of a column

Here is the sample code

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

public class NewJFrame extends javax.swing.JFrame {
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private MyTableModel tableModel;
 
    public NewJFrame() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1.setViewportView(jTable1);
        getContentPane().add(jScrollPane1);
     
        JButton button = new JButton("Sort Fruit");
        JPanel panel = new JPanel();
        panel.add(button, null);
        this.add(panel, BorderLayout.SOUTH);

        setPreferredSize(new Dimension(500,300));  
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        pack();
     
        Object[] headers = {"Item", "Fruit"};
        tableModel = new MyTableModel();
        tableModel.setColumnIdentifiers(headers);
        SortableVector row1 = new SortableVector();
        row1.add("Item1");
        row1.add("Papaya");
        SortableVector row2 = new SortableVector();
        row2.add("Item2");
        row2.add("Orange");
        SortableVector row3 = new SortableVector();
        row3.add("Item3");
        row3.add("Apple");
        tableModel.addRow(row1);
        tableModel.addRow(row2);
        tableModel.addRow(row3);
     
        jTable1.setModel(tableModel);
     
        button.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               tableModel.sort();
               tableModel.fireTableDataChanged();
           }
        });
    }
 
    public class MyTableModel extends DefaultTableModel {
        public void sort() {
            Vector<SortableVector> data = getDataVector();
            Collections.sort(data);
        }
    }
 
    public class SortableVector<E> extends Vector implements Comparable {

        public SortableVector() {
        }

        @Override
        public int compareTo(Object o) { //sort on column 1
            Vector row2 = (SortableVector) o;
            String value1 = (String)get(1);
            String value2 = (String)row2.get(1);
         
            return value1.compareTo(value2);
        }
    }

Further Readings:
1. JTable sort by clicking column header

-------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.