hello im having problems on how to make a column sorter for my JTable.
i want to make a sorter for each column. but i dont know what to do.
any advices will be glady accepted :)

i've seen other tables that their column headers can be clicked to sort the column's items.

here's my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.*;

class CustomerList extends JFrame implements ActionListener, TableModelListener
{

JFrame frame;
JTable table;
Vector rows,columns;
DefaultTableModel tabModel;
JScrollPane scrollPane;
JLabel name,product,date1,time,dot;
JTextField name1,product1;
JButton ADD,DELETE,DISPLAY;
JComboBox month,date,year,hour,minute,day;

public CustomerList()
{
setTitle("Customer List");
setVisible(true);
Container c = getContentPane();
c.setLayout(null);
c.setSize(800,600);
Font font=new Font("Arial",Font.PLAIN,20);
Font font1=new Font("Arial",Font.PLAIN,30);

name=new JLabel("CUSTOMER'S NAME");
product=new JLabel("PRODUCT PURCHASED");
date1=new JLabel("DATE PURCHASED");
time=new JLabel("TIME PURCHASED");
dot=new JLabel(":");
dot.setFont(font1);
name.setFont(font);
product.setFont(font);
date1.setFont(font);
time.setFont(font);


name1=new JTextField();
product1=new JTextField();
name1.setDocument(new JTextFieldLimit(30));
product1.setDocument(new JTextFieldLimit(30));
name1.setFont(font1);
product1.setFont(font1);

	month = new JComboBox();
		month.addItem("");
        month.addItem("January");
        month.addItem("February");
        month.addItem("March");
        month.addItem("April");
        month.addItem("May");
        month.addItem("June");
        month.addItem("July");
        month.addItem("August");
        month.addItem("September");
        month.addItem("October");
        month.addItem("November");
        month.addItem("December");

month.setFont(font);

        date = new JComboBox();
        date.addItem("");
        date.addItem("01");
        date.addItem("02");
        date.addItem("03");
        date.addItem("04");
        date.addItem("05");
        date.addItem("06");
        date.addItem("07");
        date.addItem("08");
        date.addItem("09");
        date.addItem("10");
        date.addItem("11");
        date.addItem("12");
        date.addItem("13");
        date.addItem("14");
        date.addItem("15");
        date.addItem("16");
        date.addItem("17");
        date.addItem("18");
        date.addItem("19");
        date.addItem("20");
        date.addItem("21");
        date.addItem("22");
        date.addItem("23");
        date.addItem("24");
        date.addItem("25");
        date.addItem("26");
        date.addItem("27");
        date.addItem("28");
        date.addItem("29");
        date.addItem("30");
        date.addItem("31");
date.setFont(font);

        year = new JComboBox();
        year.addItem("");
        year.addItem("2000");
        year.addItem("2001");
        year.addItem("2002");
        year.addItem("2003");
        year.addItem("2004");
        year.addItem("2005");
        year.addItem("2006");
        year.addItem("2007");
        year.addItem("2008");
        year.addItem("2009");
        year.addItem("2010");
        year.addItem("2011");
        year.addItem("2012");
        year.addItem("2013");
        year.addItem("2014");
        year.addItem("2015");
        year.addItem("2016");

year.setFont(font);
        hour = new JComboBox();
        hour.addItem("");
        hour.addItem("01");
        hour.addItem("02");
        hour.addItem("03");
        hour.addItem("04");
        hour.addItem("05");
        hour.addItem("06");
        hour.addItem("07");
        hour.addItem("08");
        hour.addItem("09");
        hour.addItem("10");
        hour.addItem("11");
        hour.addItem("12");

hour.setFont(font);
        minute = new JComboBox();
        minute.addItem("");
        minute.addItem("01");
        minute.addItem("02");
        minute.addItem("03");
        minute.addItem("04");
        minute.addItem("05");
        minute.addItem("06");
        minute.addItem("07");
        minute.addItem("08");
        minute.addItem("09");
        minute.addItem("10");
        minute.addItem("11");
        minute.addItem("12");
        minute.addItem("13");
        minute.addItem("14");
        minute.addItem("15");
        minute.addItem("16");
        minute.addItem("17");
        minute.addItem("18");
        minute.addItem("19");
        minute.addItem("20");
        minute.addItem("21");
        minute.addItem("22");
        minute.addItem("23");
        minute.addItem("24");
        minute.addItem("25");
        minute.addItem("26");
        minute.addItem("27");
        minute.addItem("28");
        minute.addItem("29");
        minute.addItem("30");
        minute.addItem("31");
        minute.addItem("32");
        minute.addItem("33");
        minute.addItem("34");
        minute.addItem("35");
        minute.addItem("36");
        minute.addItem("37");
        minute.addItem("38");
        minute.addItem("39");
        minute.addItem("40");
        minute.addItem("41");
        minute.addItem("42");
        minute.addItem("43");
        minute.addItem("44");
        minute.addItem("45");
        minute.addItem("46");
        minute.addItem("47");
        minute.addItem("48");
        minute.addItem("49");
        minute.addItem("50");
        minute.addItem("51");
        minute.addItem("52");
        minute.addItem("53");
        minute.addItem("54");
        minute.addItem("55");
        minute.addItem("56");
        minute.addItem("57");
        minute.addItem("58");
        minute.addItem("59");


minute.setFont(font);
        day = new JComboBox();
        day.addItem("");
        day.addItem("AM");
        day.addItem("PM");
day.setFont(font);
ADD=new JButton("ADD");
DELETE=new JButton("DELETE");
DISPLAY=new JButton("DISPLAY");

/*JLabel name,product,date1,time;
JTextField name1,product1;
JButton ADD,DELETE;
JComboBox month,date,year,hour,minute,day; */

rows=new Vector();
columns= new Vector();
String[] columnNames =
{
"Customer's Name",
"Product Purchased",
"Date",
"Time",
};
addColumns(columnNames);

tabModel=new DefaultTableModel();
tabModel.setDataVector(rows,columns);

    table = new JTable(tabModel){
      public boolean isCellEditable(int rowIndex, int colIndex) {
        return false;   //Disallow the editing of any cell
      }
    };
scrollPane= new JScrollPane(table);//ScrollPane

table.setRowSelectionAllowed(true);

table.getModel().addTableModelListener(this);

ADD.addActionListener(this);
DELETE.addActionListener(this);
DISPLAY.addActionListener(this);
c.add(scrollPane);

c.add(name);
c.add(product); c.add(date1); c.add(time);
c.add(name1); c.add(product1);
c.add(ADD); c.add(DELETE);
c.add(month); c.add(date); c.add(year); c.add(hour); c.add(minute); c.add(day);
c.add(dot); c.add(DISPLAY);

name.setBounds(10,5,200,50);
name1.setBounds(250,5,525,50);
product.setBounds(10,65,225,50);
product1.setBounds(250,65,525,50);
date1.setBounds(10,125,200,50);
month.setBounds(250,125,130,50);
date.setBounds(380,125,100,50);
year.setBounds(480,125,100,50);
time.setBounds(10,185,200,50);
hour.setBounds(250,185,70,50);
dot.setBounds(330,185,70,50);
minute.setBounds(345,185,70,50);
day.setBounds(425,185,70,50);
scrollPane.setBounds(10,245,760,305);
ADD.setBounds(10,560,370,100);
DELETE.setBounds(400,560,370,100);

table.getParent();
DISPLAY.setFont(font1);
ADD.setFont(font1);
DELETE.setFont(font1);
// Disable auto resizing
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// Set the first visible column to 100 pixels wide
int vColIndex = 0,vColIndex2 = 1,vColIndex3 = 2,vColIndex4=3;
TableColumn col = table.getColumnModel().getColumn(vColIndex);
int width = 250;
col.setPreferredWidth(width);

TableColumn col2 = table.getColumnModel().getColumn(vColIndex2);
int width2 = 250;
col2.setPreferredWidth(width2);

TableColumn col3 = table.getColumnModel().getColumn(vColIndex3);
int width3 = 128;
col3.setPreferredWidth(width3);

TableColumn col4 = table.getColumnModel().getColumn(vColIndex4);
int width4 = 128;
col4.setPreferredWidth(width4);
}

 public void addColumns(String[] colName)//Table Columns
{
for(int i=0;i<colName.length;i++)
columns.addElement((String) colName[i]);
}




void deleteRow(int index)
   {
     if(index!=-1)//At least one Row in Table
      {
        rows.removeElementAt(index);
        table.addNotify();
       }

   }//Delete Row


	public void actionPerformed(ActionEvent e)
	{
		String a,b,mon,dat,ye,ho,min,da,total,wa="",we="";

	try
	{
	if(e.getSource()==ADD)
	{
		if(name1.getText().equals(we)||product1.getText().equals(wa)||month.getSelectedItem()==""&&date.getSelectedItem()==""&&year.getSelectedItem()==""&&hour.getSelectedItem()==""&&minute.getSelectedItem()==""&&day.getSelectedItem()=="")
		{
			JOptionPane.showMessageDialog(null,"PLEASE FILL UP EVERYTHING");
		date.setSelectedItem("");
		month.setSelectedItem("");
		year.setSelectedItem("");
		hour.setSelectedItem("");
		minute.setSelectedItem("");
		day.setSelectedItem("");

		name1.setText("");
		product1.setText("");
		}

		else
		{



				a=name1.getText();
				b=product1.getText();
				mon=month.getSelectedItem().toString();
				dat=date.getSelectedItem().toString();
				ye=year.getSelectedItem().toString();
				ho=hour.getSelectedItem().toString();
				min=minute.getSelectedItem().toString();
				da=day.getSelectedItem().toString();
				total=a+" "+b+" "+mon+"-"+dat+"-"+ye+" "+ho+":"+min+""+da;
				Vector t = new Vector();
				t.addElement((String) ""+a);
				t.addElement((String) ""+b);
				t.addElement(month.getSelectedItem()+"-"+date.getSelectedItem()+"-"+year.getSelectedItem());
				t.addElement(hour.getSelectedItem()+":"+minute.getSelectedItem()+" "+day.getSelectedItem());
				rows.addElement(t);
				table.addNotify();


				
				System.out.println(" "+total+" ");
				date.setSelectedItem("");
				month.setSelectedItem("");
				year.setSelectedItem("");
				hour.setSelectedItem("");
				minute.setSelectedItem("");
				day.setSelectedItem("");

				name1.setText("");
				product1.setText("");

		}
	}

	else if(e.getSource()==DELETE)
	{
		Object obj1 = GetData(table, table.getSelectedRow(), 1);
		System.out.println(" "+obj1+"'s data has been deleted");
		deleteRow(table.getSelectedRow());

	}


	}

		catch(Exception z)
		{
			JOptionPane.showMessageDialog(null,"INVALID ACTION!");
		}
	}


	 public void tableChanged(javax.swing.event.TableModelEvent source)     {
	                 String msg="";
	                 TableModel tabMod = (TableModel)source.getSource();
	          switch (source.getType())
	                   {
	                       case TableModelEvent.UPDATE:
	                       msg="Table Value Updated for  cell "+table.getSelectedRow()+","+table.getSelectedColumn()+"\nWhich is "+table.getValueAt(table.getSelectedRow(),table.getSelectedColumn()).toString();
	              JOptionPane.showMessageDialog(null,msg,"Table Example",JOptionPane.INFORMATION_MESSAGE);
	                break;

	                   }

    }


public static void main(String[]args)
{
CustomerList o=new CustomerList();
o.setSize(800,700);
o.setResizable(false);
}

public Object GetData(JTable table, int row_index, int col_index){
    return table.getModel().getValueAt(row_index,col_index);
  }

}

before anything about DefaultTableSorter

if you are able to implement this one instead of your JCombobox

http://www.java2s.com/Code/Java/Swing-Components/AutocompleteComboBox.htm

two separeted classes (you needed both)

private ArrayList<String> listCustomer = new ArrayList<String>();
private final Java2sAutoComboBox customerCombo = new Java2sAutoComboBox(listCustomer);
listCustomere.add("-");
listCustomere.add
listCustomere.add
listCustomere.add
customerCombo.setDataList(listMmFullname);

same for AutoCompleteJtextField (that s very interesting),

how many free JCalendars, JDatePicker, there are exist

don't use bounds learn how to place JComponets by using Insets

Insets myInsets = myPanel.getInsets();

(just in case if you don't want to use any of regular and proper LayoutManagers)

here is your sorter

final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(myTableModel);
myTable.setRowSorter(sorter);

defaultRowSorter knows String, Date, NumberInstance and null, then you have to correctly set Class for each columns separatelly

nothing else to this code

I think that your tableListeners doesn't works, isn't it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.