PFGrid toolkit and widgets for SWT Eclipse

...Empower your UI with our fast and flexible controls!

Creating a JAVA-project using PFGrid and Eclipse

The following tutorial shows how to create a Java-application with Eclipse 3.4 in which hierarchical data is displayed with PFGrid. Just follow these steps:

  1. Create JAVA project

Img 1: New JAVA Project

Img 2: Define a name and press "Next"

Img 3: Add External JARs (PFGrid)

 

Now press "Finish" and your JAVA-project is created.

 


2. Create a dialog containing the PFGrid

 

Img 4: First create a class with a main-method

 

In this class open a shell and add the PFGrid (TreeListView) with some columns:

 

package tree.demo;

 

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

 

import com.pfcomponents.grid.TreeListColumn;

import com.pfcomponents.grid.TreeListView;

import com.pfcomponents.grid.enums.Align;

import com.pfcomponents.grid.enums.SelectionType;

import com.pfcomponents.grid.render.VistaTreeListRenderer;

 

public class DemoTreeSample {

 

private static TreeListView treeList;

private static Image cellImageSheet;

private static Image cellImageUser;

/**

* @param args

*/

public static void main(String[] args) {

Display display = new Display();

Shell shell = new Shell(display);

shell.setLayout(new FillLayout());

shell.setText("Demo Tree with Vista renderer");

shell.setSize(700, 600);

shell.setLocation(350, 300);

 

treeList = new TreeListView(shell, SWT.V_SCROLL | SWT.H_SCROLL);

treeList.setShowGroupbox(false);

treeList.setShowCellSelectionBorder(true);

treeList.setAllowDragCopy(true);

 

treeList.setAlternateBackcolor(true);

treeList.setSelectionType(SelectionType.Row);

treeList.setRenderer(new VistaTreeListRenderer());

 

TreeListColumn colFirstname = new TreeListColumn(treeList);

colFirstname.setText("Firstname");

colFirstname.setWidth(140);

 

TreeListColumn colLastname = new TreeListColumn(treeList);

colLastname.setText("Lastname");

colLastname.setWidth(140);

colLastname.setAllowEdit(false);

 

TreeListColumn colStreet = new TreeListColumn(treeList);

colStreet.setText("Street");

colStreet.setWidth(120);

 

TreeListColumn colZip = new TreeListColumn(treeList);

colZip.setText("Zip");

colZip.setWidth(100);

 

TreeListColumn colSkill = new TreeListColumn(treeList);

colSkill.setText("Skill");

colSkill.setWidth(80);

 

TreeListColumn colActive = new TreeListColumn(treeList);

colActive.setText("Active");

colActive.setWidth(60);

colActive.setAlign(Align.Center);

 

shell.open();

while (!shell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

 

display.dispose();

}

}

Now the UI looks like this:

3. Adding data

Add the following code after the last TreeListColumn-definition in the source-code to insert data to the tree. Put icons users.ico and sheet.ico inside the src-folder of your project:

 

// define images

InputStream ins1 = DemoTreeSample.class.getResourceAsStream("users.ico");

cellImageUser = new Image(display, ins1);

 

InputStream ins2 = DemoTreeSample.class.getResourceAsStream("sheet.ico");

cellImageSheet = new Image(display, ins2);

 

 

// add the root-nodes

for (int i = 0; i < 100; i++) {

TreeListRow row = new TreeListRow(treeList);

 

TreeListCell cellFirst = new TreeListCell(row, "Firstname " + i);

cellFirst.setCellImage(cellImageUser);

 

new TreeListCell(row, "Lastname " + i);

new TreeListCell(row, "Street No. " + i);

new TreeListCell(row, i * 33);

createProgressBarCell(row);

createCheckBoxCell(row);

 

// add childrows

addChildRows(treeList, cellImageSheet, row);

 

}

 

 

Now add these methods to your file to create special cells for ProgressBar and CheckBox:

 

public static void createProgressBarCell(TreeListRow row) {

Random r = new Random();

int value = (int) (r.nextDouble() * 100);

new ProgressBarCell(row, value);

}

 

public static void createCheckBoxrCell(TreeListRow row) {

Random r = new Random();

boolean value = r.nextBoolean();

new CheckBoxCell(row, value);

}

 

This method you have to define to add child rows to the root-nodes:

 

private static void addChildRows(TreeListView treeList,

Image cellImageSheet, TreeListRow row) {

for (int j = 0; j < 5; j++) {

TreeListRow childRow = new TreeListRow(row);

childRow.setChildrenLazy(true);

 

TreeListCell cellInnerFirst = new TreeListCell(childRow,

"Child-Firstname " + j);

cellInnerFirst.setCellImage(cellImageSheet);

new TreeListCell(childRow, "Child-Lastname " + j);

new TreeListCell(childRow, "Child-Street " + j);

new TreeListCell(childRow, "Child " + j * 17);

createProgressBarCell(childRow);

createCheckBoxrCell(childRow);

}

}

 

Add all required imports and run the application:


Now the data is shown in the PFGrid like that: