简体   繁体   English

JFace:将图像添加到表查看器中会产生问题

[英]JFace : Adding images to a table viewer is creating an issue

I have a table viewer with 5 columns. 我有一个5列的表格查看器。 In the 1st column I check some conditions and add an image and the rest of the columns will have text, But when I try to add the image and then add the rest of the columns with text then the 2nd column's text sits right beside the image in the 1st column. 在第一列中,我检查了一些条件并添加了图像,其余列将包含文本,但是当我尝试添加图像,然后在其余列中添加文本时,第二列的文本就位于图像旁边在第一列。 Here's my sample code snippet : 这是我的示例代码片段:

TableItem item = new TableItem(table, SWT.NONE);
if(condition) {
item.setImage(image);
}
else {
item.setImage(image);
}
item.setText(new String[]{"street", "city","state","Zip Code"});

Problem is that the string "street" sits right beside the image in the 1st column itself but I want the string "street" in the second column and the rest of the strings in the subsequent columns. 问题是字符串“ street”位于第一列本身的图像旁边,但我希望第二列中的字符串“ street”以及后续列中的其余字符串。 What am I doing wrong here? 我在这里做错了什么? How do I add table items from the 2nd column? 如何从第二列添加表格项?

Each column in the table can have both an image and text. 表格中的每一列都可以包含图像和文本。

Calling item.setImage(image) sets the image for the first column. 调用item.setImage(image)设置第一列的图像。

Calling item.setText(String []) sets the text for each column starting at the first column. 调用item.setText(String [])设置从第一列开始的每一列的文本。 So your "street" goes next to the image. 因此,您的“街道”位于图片旁边。

If you don't want text in the first column then set the text for that column to blank: 如果您不想在第一栏中输入文字,则将该栏的文字设置为空白:

item.setText(new String[]{"", "street", "city", "state", "Zip Code"});

Update: 更新:

You can also set the text for individual columns with 您还可以使用

item.setText(column, text);

If you are using TableViewer you should not be using TableItem , instead use the label provider and content provider. 如果使用TableViewer ,则不应使用TableItem ,而应使用标签提供者和内容提供者。

Not as clear. 不太清楚。 If you want just an image in the first colum then then just provide an empty string: 如果只想在第一个列中添加图片,则只需提供一个空字符串即可:

item.setText(new String[]{"", "street", "city","state","Zip Code"});

But it is not worth working with jface without using the benefits of ContentProvider and LabelProvider. 但是,如果不使用ContentProvider和LabelProvider的好处,则不值得使用jface。 So you should read some jface tutorials before doing stuff like that. 因此,在进行此类操作之前,您应该阅读一些jface教程。

Example: 例:

  TableViewer tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
      tableViewer.setContentProvider(ArrayContentProvider.getInstance());

      TableViewerColumn tableViewerColumnImg = new TableViewerColumn(tableViewer, SWT.NONE);
      tableViewerColumnImg.setLabelProvider(new ColumnLabelProvider()
      {
         @Override
         public Image getImage(Object element)
         {
            Person p = (Person) element;
            return p.getImage();
         }
      });
      TableColumn tblclmnImg = tableViewerColumnImg.getColumn();
      tblclmnImg.setWidth(100);
      tblclmnImg.setText("Img");

      TableViewerColumn tableViewerColumnStreet = new TableViewerColumn(tableViewer, SWT.NONE);
      tableViewerColumnStreet.setLabelProvider(new ColumnLabelProvider()
      {
         @Override
         public String getText(Object element)
         {
            Person p = (Person) element;
            return p.getStreet();
         }
      });
      TableColumn tblclmnStreet = tableViewerColumnStreet.getColumn();
      tblclmnStreet.setWidth(100);
      tblclmnStreet.setText("Street");


      tableViewer.setInput(//set an array of persons here

);

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

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