我需要将桌子第四列的单元格内容居中,现在它们从左边开始。
这是表:
Table membersTable = new Table(clubComposite, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
membersTable.setLinesVisible(true);
membersTable.setHeaderVisible(true);
membersTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableColumn tblclmnName = new TableColumn(membersTable, SWT.NONE);
tblclmnName.setWidth(150);
tblclmnName.setText("Nombre");
TableColumn tblclmnCommonPhoneNumber = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonPhoneNumber.setWidth(120);
tblclmnCommonPhoneNumber.setText("Teléfono");
TableColumn tblclmnCommonMoney = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonMoney.setWidth(150);
tblclmnCommonMoney.setText("Participación Habitual");
TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
tblclmnPayed.setWidth(50);
tblclmnPayed.setText("Pagado");
// populate Table
for (int i=0; i<50; i++) {
TableItem tableItem = new TableItem(membersTable, SWT.CENTER);
tableItem.setText(new String[] {"person "+i, "610610620", "100", ""});
tableItem.setImage(3, uncheckedImage);
}
我试过这样做:
TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
但它似乎只是列的标题,而不是它的内容。
有可能在Java SWT上实现我的需求吗?
谢谢
为了使列的内容居中,您还需要为SWT.CENTER
指定TableItem
样式位。
例如:
final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
column1.setWidth(75);
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
column2.setWidth(75);
final TableColumn column3 = new TableColumn(table, SWT.CENTER);
column3.setText("Column 3");
column3.setWidth(75);
for (int i = 0; i < 5; i++) {
new TableItem(table, SWT.CENTER).setText(new String[] { "a", "b", "c" });
}
请注意,这只会影响列的内容,该列也指定SWT.CENTER
,而不是其他列的内容。
关于图像居中的编辑:
我不相信可以通过样式位将图像置于表格行中,因为它们似乎被忽略了。
另一种方法是使用绘图侦听器使用正确的填充绘制图像,使图像在列中居中(请参阅:How to align image to center of table cell (SWT Table))。请注意,使用此方法时,行不会根据图像的大小调整大小,因此除非您的图像很小/与行的高度相同,否则您将需要做一些额外的工作来保持图像形状被切断。