Jtable with sorting and filtering functions

Source: Internet
Author: User
Tags java se
In addition to updating java. AWT in Java SE 6, javax. Swing has also made great improvements. In the C/S program, we often use "tables ". For example
The query results can be displayed in the table after the database is queried. In Java, tables are displayed using the jtable class. In previous versions, jtable can only display data in simple ways, and there is nothing
Additional processing functions, while the jtable in Java SE 6 adds the sorting and filtering functions. You can click the column header to sort data or filter data in the table based on a column.

To enable jtable to perform data operations, you must associate the rowsorter class with the jtable. Rowsorter is an abstract class, which is responsible
Data is mapped to sortable data. In actual use, we will directly use the tablerowsorter subclass of rowsorter. The following code shows how
The tablerowsorter class is associated with jtable.

Tablemodel model = new defaulttablemodel (rows, columns );
Jtable table = new jtable (model );
Rowsorter sorter = new tablerowsorter (model );
Table. setrowsorter (sorter );

The code above first creates a tablemodel, and then passes the tablemodel instance to both jtable and rowsorter. The following is a simple example of using jtable sorting.

Import javax. Swing .*;
Import javax. Swing. Table .*;
Import java. AWT .*;

Public class testsortedtable
{
Public static void main (string ARGs [])
{
Jframe frame = new jframe ("jtable sorting test ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
// Data displayed in the table
Object rows [] [] =
{
{"Wang Ming", "China", 44 },
{"Yao Ming", "China", 25 },
{"Zhao Zilong", "xishu", 1234 },
{"Cao", "bei wei", 2112 },
{"Bill Gates", "USA", 45 },
{"Mike", "UK", 33 }};
String columns [] =
{"Name", "nationality", "Age "};
Tablemodel model = new defaulttablemodel (rows, columns );
Jtable table = new jtable (model );
Rowsorter <tablemodel> sorter = new tablerowsorter <tablemodel> (model );
Table. setrowsorter (sorter );
Jscrollpane pane = new jscrollpane (table );
Frame. Add (PANE, borderlayout. center );
Frame. setsize (300,150 );
Frame. setvisible (true );
}
}

Figures 1 and 2 are displayed in ascending and descending order by "name.


Figure 1 display in ascending order of "name"

Figure 2 display by name in descending order

Figure 3 shows a descending order by age. However, we found a strange problem, that is, the "Age" field is not sorted by numerical type, but by character type.


Figure 3 show by age in descending order

This occurs because the defaulttablemodal column is of the object type by default. To sort jtable by numeric values, you must overwrite the defaulttablemodal getcolumnclass method.

Tablemodel model = new defaulttablemodel (rows, columns)
{
Public class getcolumnclass (INT column)
{
Class returnvalue;
If (column> = 0) & (column <getcolumncount ()))
{
Returnvalue = getvalueat (0, column). getclass ();
}
Else
{
Returnvalue = object. Class;
}
Return returnvalue;
}
};

Figure 4 shows the page for sorting by age to see if it is sorted by numerical value.


Figure 4 sorting by Value Type

Let's see how jtable is used for filtering. You can use the convertrowindextomodel method to filter data. The following code adds the Event code to a button to call the jtable filter function.

Button. addactionlistener (New actionlistener ()
{
Public void actionreceivmed (actionevent E)
{
String text = filtertext. gettext ();
If (text. Length () = 0)
{
Sorter. setrowfilter (null );
}
Else
{
Sorter. setrowfilter (rowfilter. regexfilter (text ));
}
}
});

The above Code does not call the convertrowindextomodel () method. If you call it, you can perform corresponding operations in the table.

In jtable, use the abstract class rowfilter to filter rows. Unlike sorting, you can use the six static methods of this abstract class without creating their subclasses.

· Andfilter
· Datefilter (rowfilter. comparisontype type, date, Int... indices)
· Notfilter (rowfilter <m, I> filter)
· Numberfilter (rowfilter. comparisontype type, number, Int... indices)
· Orfilter
· Regexfilter (string RegEx, Int... indices)

The andfilter (), orfilter (), and notfilter () methods are used to combine the current filtering conditions with other filtering conditions. For example, you need to combine date filtering and value filtering when comparing dates and values at the same time. These combinations are very simple.

Rowfilter type comparison allows you to compare four types of relationships, which are equal to, not equal to, greater than, or less. You can specify a column for filtering or filter all columns. This
The most interesting part is the regular expression filtering (Regular Expression Filter, or RegEx filter for short ). Use this filter
More advanced filtering of table data. The following code implements a simple filter.

Import javax. Swing .*;
Import javax. Swing. Table .*;
Import java. AWT .*;
Import java. AWT. event .*;

Public class testfilter
{
Public static void main (string ARGs [])
{
Jframe frame = new jframe ("jtable filtering test ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Object rows [] [] =
{
{"Wang Ming", "China", 44 },
{"Yao Ming", "China", 25 },
{"Zhao Zilong", "xishu", 1234 },
{"Cao", "bei wei", 2112 },
{"Bill Gates", "USA", 45 },
{"Mike", "UK", 33 }};
String columns [] =
{"Name", "nationality", "Age "};
Tablemodel model = new defaulttablemodel (rows, columns)
{
Public class getcolumnclass (INT column)
{
Class returnvalue;
If (column> = 0) & (column <getcolumncount ()))
{
Returnvalue = getvalueat (0, column). getclass ();
}
Else
{
Returnvalue = object. Class;
}
Return returnvalue;
}
};
Final jtable table = new jtable (model );
Final tablerowsorter <tablemodel> sorter = new tablerowsorter <tablemodel> (model );
Table. setrowsorter (sorter );
Jscrollpane pane = new jscrollpane (table );
Frame. Add (PANE, borderlayout. center );
Jpanel Panel = new jpanel (New borderlayout ());
Jlabel label = new jlabel ("filter ");
Panel. Add (Label, borderlayout. West );
Final jtextfield filtertext = new jtextfield ("");
Panel. Add (filtertext, borderlayout. center );
Frame. Add (panel, borderlayout. North );
Jbutton button = new jbutton ("filter ");
Button. addactionlistener (New actionlistener ()
{
Public void actionreceivmed (actionevent E)
{
String text = filtertext. gettext ();
If (text. Length () = 0)
{
Sorter. setrowfilter (null );
}
Else
{
Sorter. setrowfilter (rowfilter. regexfilter (text ));
}
}
});
Frame. Add (button, borderlayout. South );
Frame. setsize (300,250 );
Frame. setvisible (true );
}
}

Figure 5 shows the running interface of the above program.

From: http://hi.baidu.com/pidk/blog/item/875b44232b53eb529922edc0.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.