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.

Advertisements

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: http://www.mathworks.com.au/products/compiler/mcr/index.html.

Good Java tutorials and technologies….

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

Technologies

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.

Tutorials

The Java Tutorials: http://docs.oracle.com/javase/tutorial/reallybigindex.html

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…

Color.WHITE

…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.setXORMode(Color.WHITE);
graphics.drawLine(x, delegate.getXAxisHeight(), x, delegate.getDiagramRect().height + delegate.getXAxisHeight());
graphics.setColor(Color.BLACK);

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)
   {
      graphicsSG2D.setStroke(stroke);
   }

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

graphics2D.getStroke()

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 {
NONE("None"),
CATEGORY_1("Category 1"),
CATEGORY_2("Category 2"),
CATEGORY_3("Category 3");

   public final String name;
   private AutofillType (String name)
   {
      this.name = name;
   }
   @Override
   public String toString() {
      return name;
   }
}

Therefore, you can do something like:

System.out.println(AutofillType.CATEGORY_1);

And get the following output:

Category 1

Instead of:

CATEGORY_1

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();
table.setAutoCreateRowSorter(true);

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
table.convertColumnIndexToModel(viewColumnIndex);
table.convertColumnIndexToView(modelColumnIndex);

//for row indices
table.convertRowIndexToModel(viewRowIndex);
table.convertRowIndexToView(modelRowIndex);

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.