Home Eclipse. Share on Facebook Share on Twitter. You can see the SWT instruction at:. Thus, the knowledge of SWT is basic and vital. JFace helps: Simplifying the data model and visual display. For example, it simplifies data and display in Table, ComboBox, and Tree.
|Published (Last):||5 October 2006|
|PDF File Size:||1.56 Mb|
|ePub File Size:||16.41 Mb|
|Price:||Free* [*Free Regsitration Required]|
You can use the TableViewer class to create tables using the JFace framework. This class wraps the Table widget and makes it simpler to use with a custom data model. The table widget can still be accessed to set its properties. As with other JFace viewers a content provider supplies the data which should be displayed in the TableViewer. Eclipse provides an implementation of this interface via the ArrayContentProvider class.
You can implement your own content provider for a table by implementing the interface IStructuredContentProvider from the org. The getElements method of the content provider is used to translate the input of the viewer into an array of elements.
Once the setInput method on the viewer is called, it uses the content provider to convert it. This is the reason why the content provider must be set before the setInput method is called.
Each object in the Array returned by the content provider is displayed as an individual element by the viewer. In case of the table viewer each object is displayed as an individual row.
Each TableViewerColumn object needs to have a label provider assigned to it via the setLabelProvider method. The label provider defines what data is displayed in the column. The label provider for a table viewer column is called per row and gets the corresponding object as its input.
It uses this input to determine which data is displayed in the column for this row. A default implementation of this class is provided by the ColumnLabelProvider class. Its usage is demonstrated in the following code snippet. The above code uses two fields which contain Image instances. These fields could for example be initialized via the following code.
Using the classes in this code requires a dependency on the org. To reflect data changes in the data model that is displayed by the viewer, you can call the viewer.
This method updates the viewer based on the data that is assigned to it. Via the addSelectionChangedListener method you can add a listener to a viewer.
This listener is an implementation of the ISelectionChangedListener interface. The following code shows an example that gets the selected element of the viewer. In most of the cases it is desired to select certain objects and therefore using the JFace API is more common. Each person is displayed in one individual row. This tutorial the basic setup of a JFace Table. Create a new RCP Project de. Create a package "de. The Person class represents the data model for this example.
It has also PropertyChange support, which is not necessary for this example but is nice if you would later extend this example with Eclipse Data Binding support.
Create the ModelProvider class which is a in-memory representation of your data. This class is defined as a Singleton. The createColumns method creates the table columns, headers, sets the size of the columns and makes the columns re-sizable.
The EditingSupport implementation defines how the content can be changed. The getCellEditor method returns an object of type CellEditor. This object creates the controls to change the data. The canEdit method defines if the cell can be edited. The getValue method receives the current object and returns the value which should be edited.
The method setValue in EditingSupport receives the changed value based on the user input. In this method you assign the value to your data object. The TableColumn class provides the setEditingSupport method to set the editor for the table column. From an application design perspective, editing within a table can be cumbersome for the user.
If the end user has to edit a lot of data, you should also offer a dialog, wizard or part to edit the data.
A JFace viewer supports filtering of data via the setFilters or addFilter methods. These methods expect ViewerFilter objects as arguments. For each registered ViewerFilter object the select method is called. The method returns true if the data should be shown and false if it should be filtered. All filters are checked whenever the input of the viewer changes, or whenever its refresh method is called. If more than one filter is defined for a viewer, all filters must return true to display the data.
JFace supports sorting of the viewer content via the setComparator method on the viewer object. This method expects a ViewerComparator object. By default, it will sort based on the toString method of the objects in the viewer. With the TableColumnLayout class you can define the width of the columns in the table. This can be done based on a fixed or percentage value. Using TableColumnLayout requires a Composite which only contains the table widget.
This Composite gets the TableColumnLayout assigned. The TableColumnLayout requires that you define a fixed or relative size for all columns. A StyledCellLabelProvider supports flexible styling of content in a column. The following example shows how to use a StyledCellLabelProvider. In this example a portion of a pre-defined text is highlighted. You can add a menu to your table. This menu can get a menu entry for each column.
This allow you to add arbitrary popup actions to your columns. You can use it, for example, to hide and show columns based on the width setting as demonstrated in the following code snippet.
You can use tooltips for the cells of the viewer. To achieve this, you have to activate the tooltips for a viewer. In your CellLabelProvider you specify the related methods for displaying the tooltip. If you have a huge number of lines which you want to display in the table, you can use a LazyContentProvider.
This provider allows you to fetch the data when they are needed instead of loading everything into memory. As a result of the lazy loading you gain a better memory footprint and improve the performance for a large set of data. The following code demonstrates its usage. It is possible to use other table implementations. Most notable is the NatTable implementation. It provides a flexible and powerful framework for creating tables, grids and trees that show great performance for a huge number of rows and columns.
A ViewerComparator allows to sort the table. The following example will allow to sort the table based on different columns. This method will add a listener to the columns. If one of the columns is selected, the sorting column in ViewerComparator will be set.
If you run the example and click on a column header, the table should be sorted according to the content of this column. There will also appear a sort-direction in the top of the column. Create the following new classes in the new de. Replace the existing method createColumns with the following one and adjust your imports. The following adds a filter to the table based on the Search Text widget. The user can enter search pattern in this text field and data which fits to the entered pattern will be displayed.
Add to your search text field a keyListener which updates the filter and the viewer. You need also to define a new private PersonFilter filter; field and change the createPartControl method. First create the following helper class which will determine which occurrence of the search string is in the column. Change the class View to the following. Most of the code changes are in method createColumns plus a view variable definitions.
If you are not familiar with Eclipse Commands, please have a look at the following tutorial: Eclipse Commands Tutorial. You have to make the selection of the Viewer available and also provide public method to refresh the data of to the Viewer so that other components can trigger the refresh process.
Create a Dialog to enter the data for the additional person. Create the de. Create a new View called ChangeView to your application and add it to your perspective. This View contains a Button which allows to start the Dialog , adds the new Person to the data and refreshes the Viewer.
When you finished this successfully, you are able to add new persons to your TableViewer via clicking on the Button. Create the command de.
Your first JFace application : JFace Introduction « SWT « Java Tutorial
You can use the TableViewer class to create tables using the JFace framework. This class wraps the Table widget and makes it simpler to use with a custom data model. The table widget can still be accessed to set its properties. As with other JFace viewers a content provider supplies the data which should be displayed in the TableViewer. Eclipse provides an implementation of this interface via the ArrayContentProvider class. You can implement your own content provider for a table by implementing the interface IStructuredContentProvider from the org. The getElements method of the content provider is used to translate the input of the viewer into an array of elements.
Eclipse JFace Tutorial
JFace includes the usual UI toolkit components of image and font registries, text, dialog, preference and wizard frameworks, and progress reporting for long running operations. Two of its more interesting features are actions and viewers. The action mechanism allows user commands to be defined independently from their exact whereabouts in the UI. Viewers are model based adapters for certain SWT widgets, simplifying the presentation of application data structured as lists, tables or trees. The JFace data binding framework was introduced in Eclipse 3. Data binding allows linking UI elements and models so that users can edit or view the data in the model. The framework makes it easy to connect data sources to widgets such as text fields, combos, tables and trees, for viewing and editing.
03 - Working With JFace Viewers
Eclipse allows you to use dialogs to prompt the user for additional information or provide the user with feedback. The default SWT dialogs are listed below. The following code demonstrates the usage of the MessageBox class to open a message dialog. JFace contains several frequently used dialogs which are not based on the native dialogs as well as a framework for building custom dialogs. The MessageDialog class provides static methods to open commonly used dialogs, for example an information or a warning dialog.
SWT and JFace, Part 1: A gentle introduction
JFace provides classes and frameworks which simplify common SWT use cases. JFace provides the viewers framework, which simplifies the mapping of a data model to a visual representation. For example, you find viewers for ComboBoxes, Tables and Trees. JFace also provides helper classes to effectively manage your system resources, like colors, images and fonts. In addition JFace provides support for handling preferences, preference pages, wizards and dialogs. It also contains functionality to support icon decorations and user-input help for SWT controls.