简体   繁体   English

Java SWT:如何将表中列的内容居中?

[英]Java SWT: How to center content of a column in a Table?

I need to center the content of the cells of the fourth column of my table, now they start on left.我需要将表格第四列的单元格内容居中,现在它们从左侧开始。

This is the table:这是表:

    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);
    }

I tried doing this:我尝试这样做:

TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);

But it seems that it only centers the title of the column, not it's content.但它似乎只是以列的标题为中心,而不是它的内容。

It is possible to achieve my needs on Java SWT?有可能在 Java SWT 上实现我的需求吗?

In order to center the content of a column, you need to specify the SWT.CENTER style bit for the TableItem as well.为了使列的内容居中,您还需要为TableItem指定SWT.CENTER样式位。

For example:例如:

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" });
}

Note that this will only affect the contents for the column which also specifies SWT.CENTER , not the content for the other columns.请注意,这只会影响也指定SWT.CENTER的列的内容,而不影响其他列的内容。


Edit in regards to centering an image:编辑关于居中图像:

I don't believe it is possible to center an image within the table row via the style bits, since they seem to be ignored.我不相信可以通过样式位将图像放在表格行中,因为它们似乎被忽略了。

One alternative would be to use a paint listener to draw the image with the correct padding to center the image in the column (See: How to align image to center of table cell (SWT Table) ).一种替代方法是使用绘画侦听器绘制具有正确填充的图像,以使图像在列中居中(请参阅: 如何将图像与表格单元格的中心对齐(SWT 表格) )。 Note that with this method the row is not resized based on the size of the image, so unless your image is tiny/the same height as the row, you'll have to do some additional work to keep the image form being cut off.请注意,使用此方法不会根据图像的大小调整行的大小,因此除非您的图像很小/与行的高度相同,否则您必须做一些额外的工作以保持图像形式被切断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM