Sorting data in JTable

Discussion in 'Java' started by Lukasz, Aug 16, 2006.

  1. Lukasz

    Lukasz Guest

    Hello,

    From
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
    page, section "Sorting and Otherwise Manipulating Data" I downloaded
    the TableSorter.java and implemented it into my test applet:

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;

    public class Sort extends Applet implements ActionListener {

    JButton push;
    JTable tabela;
    String[] data = {"First Name", "Last Name"};
    String[][] values = new String[145][2];
    JFrame ram;
    JScrollPane scrollPane;

    public void init() {

    setLayout(null);
    setBackground(new Color(204, 206, 209));

    push = new JButton("Push");
    push.addActionListener(this);
    push.setBounds(10,10, 100, 25);
    add(push);
    }

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == push) {
    removeAll();

    setLayout(null);
    TableSorter sorter = new TableSorter(new Model());
    tabela = new JTable(sorter);
    sorter.setTableHeader(tabela.getTableHeader());
    tabela.setPreferredScrollableViewportSize(new Dimension(450, 250));
    scrollPane = new JScrollPane(tabela);
    scrollPane.setSize(700,250);
    scrollPane.setLocation(200,100);
    add(scrollPane);
    validate();
    repaint();
    }
    }

    class Model extends AbstractTableModel {

    String[] data = {"First Name", "Last Name"};
    Object[][] values = {
    {"mary", "Campione"},
    {"alison", "Huml"},
    {"Kathy", "Walrath"},
    {"Sharon", "Zakhour"},
    {"Philip", "Milne"}
    };

    public int getColumnCount() {
    return data.length;
    }

    public int getRowCount() {
    return values.length;
    }

    public String getColumnName(int col) {
    return data[col];
    }

    public Object getValueAt(int row, int col) {
    return values[row][col];
    }

    public Class getColumnClass(int c) {
    return getValueAt(0, c).getClass();
    }
    }
    }


    HTML:
    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <APPLET CODE="Sort.class" WIDTH=1000 HEIGHT=1000></APPLET>
    </BODY>
    </HTML>

    When every name or surname from values array starts with big letter,
    sorting works fine. When it starts with small letter, this value is not
    taken to sorting. I tried in LEXICAL_COMPARATOR in TableSorter.java to
    put ignoreCase() and toLowerCase(), but the result was still the same.

    Anyone has an idea, what to change in TableSorter.java to have a
    properly working sorting?
     
    Lukasz, Aug 16, 2006
    #1
    1. Advertising

  2. Lukasz

    John Guest

    Lukasz wrote:
    >
    > When every name or surname from values array starts with big letter,
    > sorting works fine. When it starts with small letter, this value is not
    > taken to sorting. I tried in LEXICAL_COMPARATOR in TableSorter.java to
    > put ignoreCase() and toLowerCase(), but the result was still the same.
    >
    > Anyone has an idea, what to change in TableSorter.java to have a
    > properly working sorting?
    >



    The String class cannot understand natural language sorting. You need a
    Collator for that. See either of the following:
    http://weblogs.java.net/blog/joconner/archive/2006/06/strings_equals.html
    http://www.joconner.com/2006/06/28/when-stringequal-just-isnt-enough/

    The blog portion that should interest you is the discussion of String's
    compareTo vs Collator. I haven't looked at your TableSorter.java class,
    but it is most likely comparing with String...not a good thing for
    linguistic sorts as shown in the blog entries.

    Regards,
    John O'Conner
     
    John, Aug 16, 2006
    #2
    1. Advertising

  3. Lukasz wrote:
    > When every name or surname from values array starts with big letter,
    > sorting works fine. When it starts with small letter, this value is not
    > taken to sorting. I tried in LEXICAL_COMPARATOR in TableSorter.java to
    > put ignoreCase() and toLowerCase(), but the result was still the same.
    >
    > Anyone has an idea, what to change in TableSorter.java to have a
    > properly working sorting?


    Alter the comparator:

    public void setColumnComparator(Class type, Comparator comparator)

    Something like this should do it (although I've not tested it):

    setColumnComparator(String.class, new Comparator() {
    public int compare(Object o1, Object o2) {
    return
    o1.toString().toLowerCase().compareTo(o2.toString().toLowerCase());
    }
    }

    Richard
     
    Richard Wheeldon, Aug 28, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian J. Sayatovic

    Sorting a JTable with a delegating TableModel

    Brian J. Sayatovic, Jul 10, 2003, in forum: Java
    Replies:
    2
    Views:
    1,550
    Kleopatra
    Jul 15, 2003
  2. Tivo Escobar
    Replies:
    1
    Views:
    7,405
    manusa
    Apr 12, 2007
  3. RollNick

    JTable and sorting

    RollNick, Jan 23, 2004, in forum: Java
    Replies:
    5
    Views:
    4,321
    Tim Ward
    Jan 27, 2004
  4. Tintin92
    Replies:
    1
    Views:
    1,748
    Andrew Thompson
    Feb 14, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    349
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page