gpt4 book ai didi

java - jface 中最后一列变得过大

转载 作者:行者123 更新时间:2023-12-01 18:33:52 26 4
gpt4 key购买 nike

我对 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();
}
}

我该如何解决这个问题?

最佳答案

您在查看器上的布局:

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 是您为每列选择的数字,也是用于计算列宽度的“权重”。

关于java - jface 中最后一列变得过大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22913668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com