JavaSWT:如何将列的内容集中在表中?
我需要将表格第四列的单元格内容居中,现在它们从左边开始 这是表格:JavaSWT:如何将列的内容集中在表中?,java,eclipse,swt,windowbuilder,Java,Eclipse,Swt,Windowbuilder,我需要将表格第四列的单元格内容居中,现在它们从左边开始 这是表格: Table membersTable = new Table(clubComposite, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION); membersTable.setLinesVisible(true); membersTable.setHeaderVisible(true); membersTable.setLayoutData(new Grid
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);
}
但它似乎只是以专栏标题为中心,而不是以内容为中心
可以满足我对Java SWT的需求吗?为了使列的内容居中,您还需要为
TableItem
指定SWT.center
样式位
例如:
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" });
}
final Table Table=新表(shell,SWT.BORDER | SWT.V| u滚动| SWT.FULL|u选择);
setLayoutData(新的GridData(SWT.FILL,SWT.FILL,true,true));
表.setheadervible(true);
表.setLinesVisible(真);
最终TableColumn column1=新TableColumn(表,SWT.NONE);
第1列。setText(“第1列”);
列1.设置宽度(75);
最终表列2=新表列(表,SWT.NONE);
第2栏。setText(“第2栏”);
列2.设置宽度(75);
最终的TableColumn column3=新的TableColumn(表格,SWT.CENTER);
第3栏。setText(“第3栏”);
第3列:设置宽度(75);
对于(int i=0;i<5;i++){
新的TableItem(table,SWT.CENTER).setText(新字符串[]{“a”,“b”,“c”});
}
请注意,这只会影响同时指定SWT.CENTER
的列的内容,而不会影响其他列的内容
编辑关于图像居中: 我认为不可能通过样式位在表格行中居中显示图像,因为它们似乎被忽略了
另一种方法是使用绘制侦听器绘制具有正确填充的图像,以使图像在列中居中(请参见:)。请注意,使用此方法时,不会根据图像的大小调整行的大小,因此,除非您的图像很小/与行的高度相同,否则您必须做一些额外的工作以保持图像窗体被截断。不起作用。也许是因为内容是图像?我编辑了我的帖子,在其中添加了代码image@NullPointerException我不确定是否可以使用样式位在行中居中显示图像-我添加了一个编辑选项,该选项可能适用于您的情况。
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" });
}