Table programming in SWT (translation)

xiaoxiao2021-03-06  24

// Originally taken from "The Definitive Guide to Swt and JFACE"

Tables

SWT uses Table, TableColumn, and TableItem these independent classes to build Table.

These three classes do not need to be inherited.

***** CREANG TABLES

Table class only provides a constructor:

Public Table (Composite Parent, Int Style)

The following table shows the role of the style, allowing the logical and operators to represent STYLE in the constructor.

STYLE DESCRIPTION

Swt.single can only choose one line, the default parameter.

Swt.multi can choose more lines at a time, generally using the Ctrl mouse implementation to select multiple lines.

SWT.check will place a Checkbox at the beginning of each line. Note Checkbox

The selection status is independent of whether the table is in the selected state.

Swt.full_selection When the table is selected, it is highlighted instead of the default only the first column is displayed.

SWT.HIDE_SELECTIIN When the window is located in the table is not the current window, remove the highlights selected.

Display. The default is whether or not the window is located for the current window.

Selecting rows at highlights.

***** Add COLMNS

TableColumn class represents a column in the form, you create a list using a Parent Table, A Style

, And an optional index. If you don't set index, then this column will be set by default to 0.

TableColumn constructor:

Public TableColumn (Table Parent, Int Style)

Public TableColumn (Table Parent, Int Style, INDEX)

The alignment parameters of all the contents of the table column are:

SWT.LEFT left alignment

SWT.center

SWT.right right alignment

Only one of the parameters can only be set, if more than one parameter is set, the consequences will be uncertain. You can change it later in the constructor

Align: Through the setAlignment () method. The default alignment is left to stand aligned and affects all the rows in this column.

Table columns can display titles. Each title can only display a line, which will display characters as ASC2 code.

Parent Table can control whether the title is displayed by the setHeadersVisible () method.

***** Added Rows

Construction method:

TableItem (Table Parent, Int Style)

TableItem (Table Parent, Int Style, INT INDEX)

Use the second parameter to insert a row in the table, will be moved backwards, if the incoming parameters are more than the scope

For example, if there is no line, if there is no line, if you write, if you write this:

New TableItem (Table, SWT.NONE, 1);

The results are as follows:

Java.lang.illegalargumentException: Index Out of Bounds

Without STYLE is provided to TableItem, you must always pass the SWT.NONE parameter to ignore other values.

You can change the foreground color and background color of TableItem, with a wide line or a cell width. Single

Items can be displayed in the text, the picture, or both. If you are set, the picture will appear on the left of the text.

square.

***** Sample Program:

Import org.eclipse.swt. *;

Import org.eclipse.swt.graphics.font;

Import org.eclipse.swt.graphics.color;

Import org.eclipse.swt.layout. *;

Import org.eclipse.swt.widgets. *;

/ **

* Displays ASCII CODES

* /

Public class asciitable {// the number of character of characters to show.

PRIVATE STATIC FINAL INT MAX_CHARS = 128;

// Names for Each of the columns

Private static final string [] column_names = {"char", "dec", "hex", "oct",

"Bin", "name"};

// the names of the first 32 character 32 character

Private static final string [] char_names = {"nul", "soh", "stx", "etc", "eot",

"ENQ", "Ack", "Bel", "BS", "Tab", "LF", "VT", "FF", "Cr", "SO", "Si",

"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "Sub",

"ESC", "FS", "GS", "RS", "US", "Space"}

// the font to use for displaming character

PRIVATE FONT FONT;

// the background colors to use for the rows

Private color [] colors = new color [max_chars];

/ **

* Runs the application

* /

Public void run () {

Display display = new display () and DISPLAY ();

Shell shell = new shell (display);

Shell.Settext ("ASCII Codes");

CreateContents (shell);

Shell.pack ();

shell.open ();

While (! shell.isdisposed ()) {

IF (! display.readddispatch ()) {

Display.sleep ();

}

}

// Call Dispose to Dispose Any Resources

// we have create

Dispose ();

Display.dispose ();

}

/ **

* DISPOSES The Resources CREATED

* /

Private void dispose () {

// We created this font; We Must Dispose IT

IF (font! = NULL) {

Font.dispose ();

}

// We created the colors; we must dispose

For (int i = 0, n = colors.Length; i

IF (Colors [i]! = null) {

Colors [i] .dispose ();

}

}

}

/ **

* Creates the font

* /

Private void createfont () {

// Create a Font That Will Display The Range

// of character "." Works Well in

// windows

Font = New font (Display.getCurrent (), "Terminal", 10, SWT.NORMAL);

}

/ **

* Creates the columns for the Table

*

* @Param Table The Table

* @Return TableColumn []

* /

Private TableColumn [] CreateColumns (Table Table) {

TableColumn [] columns = new TableColumn [colorn_names.length];

For (int i = 0, n = columns.length; i

// Create The TableColumn with right alignment

Column [I] = New TableColumn (Table, SWT.Right);

// this TEXT WILL APPEAR in The Column Header

Columnx [i] .Settext (Column_names [i]);

}

Return columns;

}

/ **

* Creates The window's contents (the table)

*

* @Param Composite The Parent Composite

* /

Private void createContents (Composite Composite) {

Compositive.setLayout (New FillLayout ());

// the system font will not display the limited 32

// Characters, So Create One That Will

CreateFont ();

// Create a Table with visible headers

// and line, and set the font this

// created

Table Table = New Table (Composite, SWT.SINGLE | SWT.FULL_SELECTION);

Table.setHeadervisible (TRUE);

Table.SetLinesVisible (TRUE);

Table.SetredRaw (false);

Table.SetFont (font);

// Create the columns

TableColumn [] Column = CreateColumns (Table);

For (int i = 0; i

// Create A Background Color for this Row

Color (Table.getdisPlay (), 255 - i, 127 i, i);

// Create the row in the table by CREATING

// a TableItem and setting text for Each

// column

INT C = 0;

TableItem item = new TableItem (Table, SWT.NONE); Item.Settext (C , String.Valueof ((char) i));

Item.SetText (C , String.Valueof (i));

Item.Settext (C , Integer.toHexString (i) .touppercase ());

Item.Settext (C , Integer.ToOctalString (i));

Item.SetText (C , Integer.TobinaryString (i));

Item.Settext (C , I

Item.SetBackground; Colors [i]);

}

// Now That We've Set The Text Into the columns,

// we call pack () on each one to size it to the

// contents

For (int i = 0, n = columns.length; i

Column [i] .pack ();

}

// set Redraw Back to True So That The Table

// Will Paint Appropriately

Table.SetredRaw (TRUE);

}

/ **

* The Application Entry Point

*

* @Param Args the Command Line Arguments

* /

Public static void main (String [] args) {

New asciitable (). Run ();

}

}

***** Sorting Tables

Users want to make each line according to column, in ascending or descending. The SWT form will not be automatically sorted by clicking on the table title.

Do these things, but can be implemented by programming, to achieve sorting, you can follow the steps below:

1. Add a listner for the column header bar, which can be reconnaissance bar by the mouse click.

2. Available now to sort. (Which column is sorted, sequentially, or descended in order)

3. Sort the data and redisplay.

*****summary

If you are a Swing Developer, you will definitely make this sorting method: obtain data, sort,

Then re-filled. Programming with SWT means on Widget Level, use Data (Model), View, and

Smell the View and Controller. Separate Data and View in Swing, and use MVC

The pattern uses Controller to make the form sorted more direct.

But remember that although SWT is on Widget's Level, JFACE provides a MVC hierarchy on SWT

Cheng, so that the programming in the JFACE becomes simple as Swing.

转载请注明原文地址:https://www.9cbs.com/read-40523.html

New Post(0)