首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java SWT:如何在表中居中显示列的内容?

Java SWT:如何在表中居中显示列的内容?
EN

Stack Overflow用户
提问于 2018-09-17 23:28:54
回答 1查看 871关注 0票数 0

我需要将表格中第四列单元格的内容居中,现在它们从左侧开始。

下表:

代码语言:javascript
复制
    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);
    }

我试过这样做:

代码语言:javascript
复制
TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);

但它似乎只将专栏的标题居中,而不是它的内容。

有可能在Java SWT上实现我的需求吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-18 01:34:31

为了使列的内容居中,还需要为TableItem指定SWT.CENTER样式位。

例如:

代码语言:javascript
复制
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))。注意,使用这种方法不会根据图像的大小调整行的大小,所以除非图像很小/与行的高度相同,否则必须做一些额外的工作来保持图像形式被切断。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52371179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档