Adding filtering to a JTable

To filter out rows in a JTable, you will need to add a RowFilter (javax.swing.RowFilter). This row filter takes a table model and an object that correspond to an entry in the model.

RowFilter<object,object> startsWithAFilter = new RowFilter<object,object>() {
   public boolean include(Entry entry) {
     for (int i = entry.getValueCount() - 1; i >= 0; i--) {
       if (entry.getStringValue(i).startsWith("a")) {
         // The value starts with "a", include it
         return true;
     // None of the columns start with "a"
     // return false so that this
     // entry is not shown
     return false;

There are a few types of RowFilters available, like a generic “include” filter (so you specify which rows are to be included), a number/date/regex filter (which are specific RowFilter types) and an AND/OR/NOT filter to combine different row filters together.


Interfacing Matlab to Java applications

For existing projects:

  • Open Matlab.
  • Navigate to the directory holding the Matlab project, and open the project file (going by the extension “.prj”).
  • The “Deployment Tool” (which can be opened by typing “deploytool” in the prompt) will be opened.
  • If you are updating files, drag the Matlab files off the “Classes” list and drag the Matlab files onto the “Classes” list.
  • Click on :
    • “Build” icon to make a “Jar” file of the compiled Matlab files or
    • “Package” icon to make a self extracting executable of the Jar file, Javadoc files and a readme file.

For new projects:

  • Enter “deploytool” into the command window
  • Enter a name for the project, a folder and the target should be “Java Package”.
  • Drag Matlab “.m” files that should be accessible to outside classes by dragging them as “Classes”. Private functions can be dragged under “Shared Resources and Helper Files”.
  • And you can click on either Build or Package by the above steps.

Note the Matlab Compiler Runtime is required to run these packages. More information is available here:

Good Java tutorials and technologies….

So I’ll keep this updated over time… but for now:


OpenMap: A Java GIS tool. A friend Martin T. (thanks Martin!!) suggests GeoTools as an alternate Java-based framework for GIS…
GeographicLib a promising looking library for converting between coordinate systems.


The Java Tutorials:

Alternating AWT graphics colour

It is possible to draw 2D colours in such a way that it inverts colours behind it, like in the screenshot below:

line colour

This can be done by enabling “XOR Colour mode” and by using…


…this is because white has an RGB value of 255,255,255, and XOR’ing these values will cause the alternating effect.

The following code example shows this:

graphics.drawLine(x, delegate.getXAxisHeight(), x, delegate.getDiagramRect().height + delegate.getXAxisHeight());

Don’t forget to save the previous colour (using graphics.getColor())and then reset it after drawing this alternative-coloured line.

Changing AWT line thickness

To change the line thickness from thin bands to thicker bands, like below:

line thickness

Set the “stroke” size used on the Graphics2D object:

   Graphics2D graphicsSG2D = ((Graphics2D) graphics);
   if(graphics instanceof Graphics2D) 
      stroke = graphicsSG2D.getStroke();
      graphicsSG2D.setStroke(new BasicStroke(3));

   if(graphics instanceof Graphics2D && stroke != null)

Don’t forget to save the previous stroke type, using


and then reset it after drawing this alternative-thickness line.

Casting integers from objects… (and other primitives too)

In Java, if you are 100% sure you’ve got an Integer in the form of an Object and you want to convert it to an “int” type…

Object obj;
int val = ((Integer) obj).intValue();

And this works with other primitive data types also.

“Readable” Enumerations

It is possible to use an enumeration but to also assign a “Human Readable” version of the enumerated values.

public enum AutofillType {
CATEGORY_1("Category 1"),
CATEGORY_2("Category 2"),
CATEGORY_3("Category 3");

   public final String name;
   private AutofillType (String name)
   { = name;
   public String toString() {
      return name;

Therefore, you can do something like:


And get the following output:

Category 1

Instead of:


I found it was useful for creating a ‘taxonomy’ style, fixed vocabulary and you want to make it easy to put a ‘human-readable’ version… might not be internationalize-able (and there’s probably smarter ways of doing this out there!)…

Adding sorting to a JTable

sorter for JTable

To add buttons to a JTable’s headers so that when you click on the headers the JTable is sorted in those particular fields:

JTable table = new JTable();

This is also helpful as the underlying TableModel is not altered. But to use selected cells one needs to use the following commands:

//for column indices

//for row indices

Note that one can also extend off “DefaultTableModel” and add other helper functions to the table model such as row counts, column counts etc. Although do not include presentation layer concepts like “new entry rows” in the Table Model: these should be done in a separate JTable.