最后一列在jface中变得过大

时间:2014-04-07 13:18:56

标签: java eclipse jface

我对Jface很新,现在我遇到了表中最后一列的问题。看一下下一个截图:

enter image description here

正如您所看到的,当应用程序启动时,最后一列变得过大,无论如何我都无法解决这个问题!您可以在下面看到查​​看代码:

package de.vogella.jface.tableviewer;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import de.vogella.jface.tableviewer.model.CenterImageLabelProvider;
import de.vogella.jface.tableviewer.model.ModelProvider;
import de.vogella.jface.tableviewer.model.Person;

public class View extends ViewPart {

  public static final String ID = "de.vogella.jface.tableviewer.view";

  private TableViewer viewer;
  // static fields to hold the images
  private static final Image CHECKED = Activator.getImageDescriptor(
          "icons/checked.gif").createImage();
  private static final Image UNCHECKED = Activator.getImageDescriptor(
          "icons/unchecked.gif").createImage();

  public void createPartControl(Composite parent) {

    GridLayout layout = new GridLayout(2, false);
    parent.setLayout(layout);
    Label searchLabel = new Label(parent, SWT.NONE);
    searchLabel.setText("Search: ");
    final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH);
    searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
        | GridData.HORIZONTAL_ALIGN_FILL));
    createViewer(parent);
  }

  private void createViewer(Composite parent) {

    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
        | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
    createColumns(parent, viewer);
    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    viewer.setContentProvider(new ArrayContentProvider());
    // get the content for the viewer, setInput will call getElements in the
    // contentProvider
    viewer.setInput(ModelProvider.INSTANCE.getPersons());
    // make the selection available to other views
    getSite().setSelectionProvider(viewer);
    // set the sorter for the table

    // define layout for the viewer
    GridData gridData = new GridData();
    gridData.verticalAlignment = GridData.FILL;
    gridData.horizontalSpan = 2;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.horizontalAlignment = GridData.FILL;
    viewer.getControl().setLayoutData(gridData);
  }

  public TableViewer getViewer() {

    return viewer;
  }

  // create the columns for the table
  private void createColumns(final Composite parent, final TableViewer viewer) {

    String[] titles = { "First name", "Last name", "Gender", "Married", "Age" };
    int[] bounds = { 100, 100, 100, 100, 100 };

    // first column is for the first name
    TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

        Person p = (Person) element;

        return p.getFirstName();
      }
    });

    // second column is for the last name
    col = createTableViewerColumn(titles[1], bounds[1], 1);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

        Person p = (Person) element;

        return p.getLastName();
      }
    });

    // now the gender
    col = createTableViewerColumn(titles[2], bounds[2], 2);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

        Person p = (Person) element;

        return p.getGender();
      }
    });

    // now the status married
    col = createTableViewerColumn(titles[3], bounds[3], 3);
    col.setLabelProvider(new CenterImageLabelProvider() {

      @Override
      public Image getImage(Object element) {

        if (((Person) element).isMarried()) {

          return CHECKED;
        }
        else {

          return UNCHECKED;
        }
      }
    });

    // now the age
    col = createTableViewerColumn(titles[4], bounds[4], 4);
    col.setLabelProvider(new ColumnLabelProvider() {

      @Override
      public String getText(Object element) {

          Person p = (Person) element;

          return p.getAge().toString();
      }
    });

  }

  private TableViewerColumn createTableViewerColumn(String title, 
          int bound, final int colNumber) {

    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
        SWT.CENTER);
    final TableColumn column = viewerColumn.getColumn();
    column.setText(title);
    column.setWidth(bound);
    column.setResizable(true);
    column.setMoveable(true);

    return viewerColumn;
  }

  public void setFocus() {

    viewer.getControl().setFocus();
  }
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您在观看者上的布局:

GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);

表示您希望查看器使用所有可用的水平空间,因此查看者必须调整最后一列的大小才能实现此目的。

使用

gridData.grabExcessHorizontalSpace = false;
gridData.horizontalAlignment = GridData.BEGINNING;

不要占用额外的空间。

另外,要填充可用空间,请单独使用GridData并使用TableLayoutColumnWeightData,例如:

TableLayout tableLayout = new TableLayout();
table.setLayout(tableLayout);

...
... don't call column.setWidth replace with:
tableLayout.addColumnData(new ColumnWeightData(nnn));

nnn是您为每列选择的数字,是' weight'用于计算列的宽度。