Tables
SWT uses three independent classes: Table, tablecolumn, and tableitem to construct a table.
These three classes do not need to be inherited.
***** Creating tables
The table class only provides one constructor:
Public table (composite parent, int style)
The following table shows the functions of styles. In the constructor, the logic and operators are allowed to represent styles.
Style description
SWT. Single can only select one row at a time, the default parameter.
SWT. multi can select multiple rows at a time, generally using Ctrl + mouse to select multiple rows.
SWT. Check will place a checkbox at the beginning of each line. Note that checkbox
The selection status is irrelevant to whether the table row is in the selected status.
SWT. full_selection when the table row is selected, it is highlighted, instead of displaying only the first column by default.
SWT. hide_selectin when the window in which the table is located is not the current window, remove the highlighted row
Display. By default, the table is selected regardless of whether the window in which the table is located is the current window.
The selected row is highlighted.
***** Adding columns
The tablecolumn class indicates a column in the table. You can create a column using a parent table, a style
And an optional index. If you do not set the index, this column will be set to 0 by default.
Tablecolumn constructor:
Public tablecolumn (Table parent, int style)
Public tablecolumn (Table parent, int style, int index)
The following alignment parameters are used to set the content of all table columns:
SWT. Left left alignment
SWT. Center
SWT. Right
Only one of the parameters can be set. If more than one parameter is set, the result will be uncertain. You can change it after constructing the method.
Variable alignment (): the default alignment is left alignment and affects all rows in this column.
The table column can display titles. Each title can only display one row. It will display characters according to the asc2 code.
The parent table can use the setheadersvisible () method to control whether the title is displayed.
* ***** Adding rows
Constructor:
Tableitem (Table parent, int style)
Tableitem (Table parent, int style, int index)
Insert rows in the table using the 2nd parameters to move the existing rows backward. If the input parameters exceed the range
Output illegalargumentexception. For example, if no row exists in the table, write as follows:
New tableitem (table, SWT. None, 1 );
The result is as follows:
Java. Lang. illegalargumentexception: Index out of bounds
No style is provided to tableitem, so you must always pass the SWT. None parameter to ignore other values.
You can change the foreground color and background color of tableitem, either in the whole row width or in the same cell width.
The cell can display text, images, or both. If both are set, the image is displayed on the left of the text.
Side.
* ***** 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 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 characters
Private Static final string [] char_names = {"NUL", "Soh", "STX", "etx", "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 displaying characters
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 ();
Shell shell = new shell (Display );
Shell. settext ("ASCII codes ");
Createcontents (Shell );
Shell. Pack ();
Shell. open ();
While (! Shell. isdisposed ()){
If (! Display. readanddispatch ()){
Display. Sleep ();
}
}
// Call dispose to dispose any resources
// We have created
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 them
For (INT I = 0, n = colors. length; I <n; I ++ ){
If (colors [I]! = NULL ){
Colors [I]. Dispose ();
}
}
}
/**
* Creates the font
*/
Private void createfont (){
// Create a font that will display the range
// Of characters. "terminal" 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 ){
Tablecolumn [] Columns = new tablecolumn [column_names.length];
For (INT I = 0, n = columns. length; I <n; I ++ ){
// Create the tablecolumn with right alignment
Columns [I] = new tablecolumn (table, SWT. Right );
// This text will appear in the column header
Columns [I]. settext (column_names [I]);
}
Return columns;
}
/**
* Creates the window's contents (the table)
*
* @ Param composite the parent Composite
*/
Private void createcontents (composite ){
Composite. setlayout (New filllayout ());
// The system font will not display the lower 32
// Characters, so create one that will
Createfont ();
// Create a table with visible Headers
// And lines, and set the font that we
// Created
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 [] Columns = createcolumns (table );
For (INT I = 0; I <max_chars; I ++ ){
// Create a background color for this row
Colors [I] = new 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 <char_names.length? Char_names [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
// Contents
For (INT I = 0, n = columns. length; I <n; I ++ ){
Columns [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
You want to click the table title to sort each row by column. SWT tables are not automatically sorted in ascending or descending order.
You can do this by programming and sorting by following the steps below:
1. Add a listener to the column title bar to detect the title bar being clicked by the mouse.
2. obtain the information to be sorted. (which column is sorted and in which direction is sorted-ascending or descending)
3. sort and re-display the data.
* ***** Summary
If you are a swing develloper, you will make fun of the SWT table sorting method: getting data, sorting,
Then fill in again. Using SWT programming means to use data (model), view, and
View and controller are inseparable. Data and view are separated like swing tables, and MVC
Use controller to make table sorting more direct.
But remember that although SWT is at the widget level, jface provides an MVC level on SWT.
To make programming in jface as simple as swing.