Adding search filter to JFace table viewer

This topic is basically covered in the excellent Vogella JFace Table tutorial.  The upshot of it is that you can add a ViewerFilter to your table viewer.  The implementation of the ViewerFilter determines whether entries in the table are displayed.

Implement your own, and then add to the table viewer via the viewer.addFilter(filter) method.

In the example given in the tutorial, the implementation of the ViewerFilter contains a method – setSearchText(String s) – which allows you to define what the current search string is.  Add a text control to your GUI, and implement a KeyAdapter to listen for keyReleased() messages.  When the user types something in, set the search string on the filter from the contents of the text control.


Combining multiple images in SWT

The task I have been set is to combine multiple images (icons and so on) into one image, along with some text. I found that the best approach to do this within SWT was to make use of the GC object.

Once a GC object has been created, you can draw an image within using gc.drawImage(image, x, y). You can add text using gc.drawText(string, x, y). If you need to find the extent of text within the GC, you can use gc.textExtent(string), which returns a Point.

I created a GC with a new image. Once I had finished drawing to it via the GC, I could just return the image from my helper method.

Adding images to JFace TableViewer

When adding images to a JFace TableViewer, make sure that the column containing the images is wide enough. The TableViewer behaviour seems to be that it squashes the image if it is not in the currently selected row, but hides the image completely if it is in the selected row.

Worth remembering – it took me about half an hour today to work out what was going on! 🙂

Fun with StyledText

So, in order to make a label on a GUI more interesting, I decided to use SWT’s StyledText. To start off with, I had a look at this tutorial:
Getting Your Feet Wet With the StyledText Widget

It certainly is more flexible than a normal label, but I wasn’t sure about the requirement to specify the character ranges within your text to which a particular style range should be applied.

I decided that, instead of creating one large StyledText widget and working out which text ranges within it should have which StyleRange associated with them, I would instead create an overall container composite, and populate it with multiple StyledTexts. Each StyledText would have a particular StyleRange applied to the whole of its text.

In order to help with the creation of multiple StyledTexts, I created a factory class. I had a requirement for a bold, blue header font, and a normal black body font. My factory class looked as follows:

private static StyleRange createHeaderStyleRange(Display display)  {  
StyleRange styleRange = new StyleRange();  
styleRange.foreground = display.getSystemColor(SWT.COLOR_BLUE);  
styleRange.fontStyle = SWT.BOLD;  
return styleRange;

public static StyledText createHeaderText(Display display, Composite parent, String text)  {  
StyledText styledText = new StyledText(parent, SWT.NONE | SWT.WRAP);  
if (text == null)  {    
  text = "";  
StyleRange headerStyleRange = createHeaderStyleRange(display);  
headerStyleRange.start = 0;  
headerStyleRange.length = text.length();  
return styledText;

// Similar for a body text, but without the need to set a style range

So yeah, it worked for me. I’m a newbie to this control though, so if people know of better strategies for using StyleText, please let me know. 🙂

Sorting JFace table viewers by column

As with a lot of RCP topics, the Vogella tutorial on JFace table viewers is highly reccommended. The advanced section is found at

The task which I had to tackle was sorting a table viewer based on the values in one of its columns. I followed the approach detailed in the Vogella tutorial. This entailed two main steps:

1) Define your own ViewerComparator. I adapted the MyViewerComparator class defined in the Vogella tutorial. The viewer comparator has methods on its interface to get the current sort direction, and to get/set the column with which we will carry out the sort.

2) Add a selection adaptor to the table viewer. In the selection adaptor, you get the column on which to sort the table, tell the viewer to carry out the sort, then refresh it.
Sample code from the tutorial to do this is:

SelectionAdapter selectionAdapter = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {  
  int dir = comparator.getDirection();  

Making a JFace dialog resizable

OK, so you’ve extended the JFace Dialog class to customize it with your own behaviour, but the dialog size is fixed to the bounds which you initialise it with.  To fix this, you just need to override the isResizable() method on the Dialog interface as follows:

protected boolean isResizable()  {
  return true;

Nothing to it. 🙂