Friday, December 13, 2013

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.

No comments:

Post a Comment