The project has basically produced a version now. Although it is marginal work, I still want to sort out the things used. After all, I have learned a little bit. The first is the use of TableView. RWT is a subset of SWT. Therefore, RWT may not fully implement all the interfaces of SWT, nor is it as complete as SWT. The architecture of the two is different, reflected in the display form and interface, but the basic controls are still the same. Here we first learn the use of some common controls through SWT.
First, let’s talk about the library files required by SWT. Includes: org.eclipse.swt_3.xxjar org.eclipse.jface_3.xxjar
org.eclipse.core.runtime_3.xxjar org.eclipse.ui.workbench_3.xxjar (org.eclipse.equinox.common_3.xxjar) In addition, this package also contains org.eclipse.core.runtime. I feel that the organization is a bit confusing. This package Also import. In addition, package import errors may occur depending on the version. Please pay attention to the version.
The following code is the creation of Tableview, which is quoted from "Eclipse Getting Started to Mastery"
+ expand sourceview plaincopy to clipboardprint?
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class TableViewer1 {
public static void main(String[] args) {
new TableViewer1().open();
}
public void open() {
final Display display = new Display();
final Shell shell = new Shell();
shell.setSize(500, 150);
//---------------------------------------------
shell.setLayout(new FillLayout());
// Step one: Create a TableViewer object. Style: MULTI allows multiple selections, H_SCROLL has horizontal scroll bars, V_SCROLL has vertical scroll bars, BORDER has borders, and FULL_SELECTION selects the entire row.
TableViewer tv = new TableViewer(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
// Step 2: Set the layout method through the Table object contained in the table
Table table = tv.getTable();
table.setHeaderVisible(true); // Display the table header
table.setLinesVisible(true); // Display table lines
TableLayout layout = new TableLayout(); // Layout dedicated to tables
table.setLayout(layout);
// Step 3: Create table columns using the TableColumn class
layout.addColumnData(new ColumnWeightData(13));//ID column width 13 pixels
new TableColumn(table, SWT.NONE).setText("ID number");
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(table, SWT.NONE).setText("Name");
layout.addColumnData(new ColumnWeightData(20));
new TableColumn(table, SWT.NONE).setText("Gender");
layout.addColumnData(new ColumnWeightData(20));
new TableColumn(table, SWT.NONE).setText("Age");
layout.addColumnData(new ColumnWeightData(60));
new TableColumn(table, SWT.NONE).setText("Record creation time");
// Step 4: Set up the inner container and tagger
tv.setContentProvider(new TableViewerContentProvider());
tv.setLabelProvider(new TableViewerLabelProvider());
// Step 5: Use the setInput method of TableViewer to input data into the table
Object data = PeopleFactory.getPeoples();
tv.setInput(data);
//---------------------------------------------
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class TableViewer1 {
public static void main(String[] args) {
new TableViewer1().open();
}
public void open() {
final Display display = new Display();
final Shell shell = new Shell();
shell.setSize(500, 150);
//------------------------------------------------
shell.setLayout(new FillLayout());
// Step one: Create a TableViewer object. Style: MULTI allows multiple selections, H_SCROLL has horizontal scroll bars, V_SCROLL has vertical scroll bars, BORDER has borders, and FULL_SELECTION selects the entire row.
TableViewer tv = new TableViewer(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
// Step 2: Set the layout method through the Table object contained in the table
Table table = tv.getTable();
table.setHeaderVisible(true); // Display the table header
table.setLinesVisible(true); // Display table lines
TableLayout layout = new TableLayout(); // Layout dedicated to tables
table.setLayout(layout);
// Step 3: Create table columns using the TableColumn class
layout.addColumnData(new ColumnWeightData(13));//ID column width 13 pixels
new TableColumn(table, SWT.NONE).setText("ID number");
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(table, SWT.NONE).setText("Name");
layout.addColumnData(new ColumnWeightData(20));
new TableColumn(table, SWT.NONE).setText("Gender");
layout.addColumnData(new ColumnWeightData(20));
new TableColumn(table, SWT.NONE).setText("Age");
layout.addColumnData(new ColumnWeightData(60));
new TableColumn(table, SWT.NONE).setText("Record creation time");
// Step 4: Set up the inner container and tagger
tv.setContentProvider(new TableViewerContentProvider());
tv.setLabelProvider(new TableViewerLabelProvider());
// Step 5: Use the setInput method of TableViewer to input data into the table
Object data = PeopleFactory.getPeoples();
tv.setInput(data);
//---------------------------------------------
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
The following are the two provided classes. If the code of these two classes is small and the service is one table, they can be written as internal classes:
view plaincopy to clipboardprint?
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
//Inner container. This class filters and transforms data entered into the form.
//This class has three methods to implement the interface, among which getElements is the main method, the other two methods are rarely used, just empty implementation is enough
public class TableViewerContentProvider implements IStructuredContentProvider {
// Filter and transform the data set input to the table.
// All input data sets must be converted into arrays. Each array element is an entity object, which is a record in the table.
public Object[] getElements(Object element) {
//The parameter element is the object input input through setInput(Object input). In this example, the input to setInput is a List collection.
if (element instanceof List)//Add a List type judgment
return ((List) element).toArray(); // Convert the data set List into an array
else
return new Object[0]; //If it is not a List type, return an empty array
}
// This method is triggered when the TableViewer object is closed
public void dispose() {
}
// This method is triggered when TableViewer calls setInput() again
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
}
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
//Inner container. This class filters and transforms data entered into the form.
//This class has three methods to implement the interface, among which getElements is the main method, the other two methods are rarely used, just empty implementation is enough
public class TableViewerContentProvider implements IStructuredContentProvider {
// Filter and transform the data set input to the table.
// All input data sets must be converted into arrays. Each array element is an entity object, which is a record in the table.
public Object[] getElements(Object element) {
//The parameter element is the object input input through setInput(Object input). In this example, the input to setInput is a List collection.
if (element instanceof List)//Add a List type judgment
return ((List) element).toArray(); // Convert the data set List into an array
else
return new Object[0]; //If it is not a List type, return an empty array
}
// This method is triggered when the TableViewer object is closed
public void dispose() {
}
// This method is triggered when TableViewer calls setInput() again
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
}
view plaincopy to clipboardprint?
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;
//Tagger. If the inner container processes the data set input into the form,
//Then the tagger processes and transforms a single entity object in the data set, and the tagger determines which column of the table the fields in the entity object are displayed in.
public class TableViewerLabelProvider implements ITableLabelProvider {
//Create several images
private Image[] images = new Image[] {
new Image(null, "icons/refresh.gif"),
new Image(null, "icons/star.jpg"),
new Image(null, "icons/moon.jpg") };
// This method determines what text is displayed in each column of the table for data records.
//The element parameter is an entity class object. col is the column number of the column currently to be set, 0 is the first column.
public String getColumnText(Object element, int col) {
PeopleEntity o = (PeopleEntity) element; // Type conversion
if (col == 0)//What data should be displayed in the first column?
return o.getId().toString();
if (col == 1)
return o.getName();
if (col == 2)
return o.isSex() ? "Male" : "Female";
if (col == 3)
return String.valueOf(o.getAge()); // Convert int type to String type
if (col == 4)
return o.getCreateDate().toString();
return null; // Method can return null value
}
// The getColumnText method is used to display text, and this method is used to display pictures.
public Image getColumnImage(Object element, int col) {
PeopleEntity o = (PeopleEntity) element;
// Only let the record "Chen Gang" display pictures
if (o.getName().equals("Chen Gang") || o.getName().equals("Zhou Yue")) {
if (col == 0)//The picture to be displayed in the first column
return images[0];
if (col == 2)//Display different icons based on gender
return o.isSex() ? images[1] : images[2];
}
return null; // Method can return null value
}
public void dispose() {
// Don’t forget the principle of SWT components: create it yourself and release it yourself
for (Image image : images) {
image.dispose();
}
}
//-------------The following methods are rarely used, so don't worry about them for now and let them be implemented empty-----------------
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void addListener(ILabelProviderListener listener) {
}
public void removeListener(ILabelProviderListener listener) {
}
}
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;
//Tagger. If the inner container processes the data set input into the form,
//Then the tagger processes and transforms a single entity object in the data set, and the tagger determines which column of the table the fields in the entity object are displayed in.
public class TableViewerLabelProvider implements ITableLabelProvider {
//Create several images
private Image[] images = new Image[] {
new Image(null, "icons/refresh.gif"),
new Image(null, "icons/star.jpg"),
new Image(null, "icons/moon.jpg") };
// This method determines what text is displayed in each column of the table for data records.
//The element parameter is an entity class object. col is the column number of the column currently to be set, 0 is the first column.
public String getColumnText(Object element, int col) {
PeopleEntity o = (PeopleEntity) element; // type conversion
if (col == 0)//What data should be displayed in the first column?
return o.getId().toString();
if (col == 1)
return o.getName();
if (col == 2)
return o.isSex() ? "Male" : "Female";
if (col == 3)
return String.valueOf(o.getAge()); // Convert int type to String type
if (col == 4)
return o.getCreateDate().toString();
return null; // Method can return null value
}
// The getColumnText method is used to display text, and this method is used to display pictures.
public Image getColumnImage(Object element, int col) {
PeopleEntity o = (PeopleEntity) element;
// Only let the record "Chen Gang" display pictures
if (o.getName().equals("Chen Gang") || o.getName().equals("Zhou Yue")) {
if (col == 0)//The picture to be displayed in the first column
return images[0];
if (col == 2)//Display different icons based on gender
return o.isSex() ? images[1] : images[2];
}
return null; // Method can return null value
}
public void dispose() {
// Don’t forget the principle of SWT components: create it yourself and release it yourself
for (Image image : images) {
image.dispose();
}
}
//-------------The following methods are rarely used, so don't worry about them for now and let them be implemented empty-----------------
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void addListener(ILabelProviderListener listener) {
}
public void removeListener(ILabelProviderListener listener) {
}
}
The other is the entity class. Here we simply write an entity. The data here can be read from the database, but then pay attention to the container type used to store the data.
And pay attention to type conversion.
view plaincopy to clipboardprint?
import java.util.Date;
//This class contains five variables of different data types, corresponding to five fields in the database table. The variable is of private type, that is, it can only
//Accessed by the internal code of the class, the outside world can only access them through the corresponding Setter/Geter methods of these variables.
public class PeopleEntity {
private Long id; //Unique identification code, often an automatically incrementing ID column in the database
private String name; //name
private boolean sex; //Gender true male, false female
private int age; //Age
private Date createDate; //The creation date of the record. Date type is java.util.Date, not java.sql.Date
//The following code is the Setter/Geter method of each field. Refer to Section 3.5.2, these methods can be automatically generated in Eclipse.
public Long getId() { return id;}
public void setId(Long long1) {id = long1;}
public String getName() {return name;}
public void setName(String string) {name = string;}
public boolean isSex() { return sex;}
public void setSex(boolean sex) { this.sex = sex; }
public int getAge() {return age;}
public void setAge(int i) {age = i;}
public Date getCreateDate() {return createDate;}
public void setCreateDate(Date date) {createDate = date;}
}
import java.util.Date;
//This class contains five variables of different data types, corresponding to five fields in the database table. The variable is of private type, that is, it can only
//Accessed by the internal code of the class, the outside world can only access them through the corresponding Setter/Geter methods of these variables.
public class PeopleEntity {
private Long id; //Unique identification code, often an automatically incrementing ID column in the database
private String name; //name
private boolean sex; //Gender true male, false female
private int age; //Age
private Date createDate; //The creation date of the record. Date type is java.util.Date, not java.sql.Date
//The following code is the Setter/Geter method of each field. Refer to Section 3.5.2, these methods can be automatically generated in Eclipse.
public Long getId() { return id;}
public void setId(Long long1) {id = long1;}
public String getName() {return name;}
public void setName(String string) {name = string;}
public boolean isSex() { return sex;}
public void setSex(boolean sex) { this.sex = sex; }
public int getAge() {return age;}
public void setAge(int i) {age = i;}
public Date getCreateDate() {return createDate;}
public void setCreateDate(Date date) {createDate = date;}
}
In addition, there is an encapsulated interface for obtaining data:
view plaincopy to clipboardprint?
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//Create a factory for PeopleEntity objects, create three PeopleEntry objects, and load them into the List collection to return
public class PeopleFactory {
public static List<PeopleEntity> getPeoples() { //Static method of factory
List<PeopleEntity> list = new ArrayList<PeopleEntity>();
{ // The first entity class object
PeopleEntity o = new PeopleEntity();
o.setId(new Long(1));//The type of id field is defined as Long, so it needs to be converted
o.setName("Chen Gang");
o.setSex(true);
o.setAge(28);
o.setCreateDate(new Date()); // Current date
list.add(o);
}
{ // The second entity class object
PeopleEntity o = new PeopleEntity();
o.setId(2L); //Use the automatic boxing function of JDK5.0 to save the conversion from long to Long object
o.setName("weekly reading");
o.setSex(false);
o.setAge(18);
o.setCreateDate(new Date());
list.add(o);
}
{ // The third entity class object
PeopleEntity o = new PeopleEntity();
o.setId(3L);
o.setName("Chen Changen");
o.setSex(true);
o.setAge(27);
o.setCreateDate(new Date());
list.add(o);
}
return list;
}
}
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/youxigogo/archive/2009/12/30/5105179.aspx
-