使控件可调整大小
Make controls resizeable
我想在 Eclipse 中创建一个页面,其中有两个彼此相邻的树组件和树下方的 table。
我编写的代码就是这样做的,但我还希望 table 和树可以调整大小。当我调整大小时 table 变小树应该变大。
我尝试了一些在互联网上找到的不同方法,但 none 确实奏效了。
这是我写的代码:
@PostConstruct
public void postConstruct(Composite parent, MApplication app) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL, true, true, 1,1));
composite.setLayout(new GridLayout(2, true));
treeLeft = new TreeViewer(composite);
treeLeft.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
treeRight = new TreeViewer(composite);
treeRight.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
Composite tableComposite = new Composite(parent, SWT.NONE);
tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
tableComposite.setLayout(new GridLayout());
table = new TableViewer(tableComposite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
GridData grid = new GridData(GridData.FILL_BOTH);
grid.horizontalSpan = 4;
table.setContentProvider(ArrayContentProvider.getInstance());
createTableColumns();
table.getControl().setLayoutData(grid);
table.getTable().setHeaderVisible(true);
table.getTable().setLinesVisible(true);
见以下代码:
@PostConstruct
public void postConstruct(Composite parent, MApplication app) {
parent.setLayout(new GridLayout(1, false));
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
composite.setLayout(new GridLayout(2, false));
TreeViewer treeViewerLeft = new TreeViewer(composite, SWT.BORDER);
Tree treeLeft = treeViewerLeft.getTree();
treeLeft.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TreeViewer treeViewerRight = new TreeViewer(composite, SWT.BORDER);
Tree treeRight = treeViewerRight.getTree();
treeRight.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
table.setContentProvider(ArrayContentProvider.getInstance());
createTableColumns();
}
使用 SashForm
可以调整组件的大小。这是一个例子:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Whosebug");
shell.setLayout(new GridLayout(1, false));
SashForm form = new SashForm(shell, SWT.VERTICAL);
form.setLayout(new GridLayout(1, false));
form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite top = new Composite(form, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginHeight = layout.marginWidth = 0;
top.setLayout(layout);
top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer first = createTableViewer(top);
first.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer second = createTableViewer(top);
second.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer third = createTableViewer(form);
third.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.pack();
shell.setSize(shell.getSize().x, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static TableViewer createTableViewer(Composite parent)
{
TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(viewer);
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
List<String> data = new ArrayList<>();
for (int i = 0; i < 20; i++)
data.add("Item " + i);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(data);
return viewer;
}
private static void createColumns(TableViewer viewer)
{
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("Column");
column.setLabelProvider(new ColumnLabelProvider()
{
@Override
public void update(ViewerCell cell)
{
Object element = cell.getElement();
if (element instanceof String)
{
String obj = (String) element;
cell.setText(obj);
}
}
});
}
看起来像这样:
调整大小后:
我想在 Eclipse 中创建一个页面,其中有两个彼此相邻的树组件和树下方的 table。
我编写的代码就是这样做的,但我还希望 table 和树可以调整大小。当我调整大小时 table 变小树应该变大。
我尝试了一些在互联网上找到的不同方法,但 none 确实奏效了。
这是我写的代码:
@PostConstruct
public void postConstruct(Composite parent, MApplication app) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL, true, true, 1,1));
composite.setLayout(new GridLayout(2, true));
treeLeft = new TreeViewer(composite);
treeLeft.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
treeRight = new TreeViewer(composite);
treeRight.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
Composite tableComposite = new Composite(parent, SWT.NONE);
tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
tableComposite.setLayout(new GridLayout());
table = new TableViewer(tableComposite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
GridData grid = new GridData(GridData.FILL_BOTH);
grid.horizontalSpan = 4;
table.setContentProvider(ArrayContentProvider.getInstance());
createTableColumns();
table.getControl().setLayoutData(grid);
table.getTable().setHeaderVisible(true);
table.getTable().setLinesVisible(true);
见以下代码:
@PostConstruct
public void postConstruct(Composite parent, MApplication app) {
parent.setLayout(new GridLayout(1, false));
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
composite.setLayout(new GridLayout(2, false));
TreeViewer treeViewerLeft = new TreeViewer(composite, SWT.BORDER);
Tree treeLeft = treeViewerLeft.getTree();
treeLeft.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TreeViewer treeViewerRight = new TreeViewer(composite, SWT.BORDER);
Tree treeRight = treeViewerRight.getTree();
treeRight.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
table.setContentProvider(ArrayContentProvider.getInstance());
createTableColumns();
}
使用 SashForm
可以调整组件的大小。这是一个例子:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Whosebug");
shell.setLayout(new GridLayout(1, false));
SashForm form = new SashForm(shell, SWT.VERTICAL);
form.setLayout(new GridLayout(1, false));
form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite top = new Composite(form, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginHeight = layout.marginWidth = 0;
top.setLayout(layout);
top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer first = createTableViewer(top);
first.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer second = createTableViewer(top);
second.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewer third = createTableViewer(form);
third.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.pack();
shell.setSize(shell.getSize().x, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static TableViewer createTableViewer(Composite parent)
{
TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(viewer);
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
List<String> data = new ArrayList<>();
for (int i = 0; i < 20; i++)
data.add("Item " + i);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(data);
return viewer;
}
private static void createColumns(TableViewer viewer)
{
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("Column");
column.setLabelProvider(new ColumnLabelProvider()
{
@Override
public void update(ViewerCell cell)
{
Object element = cell.getElement();
if (element instanceof String)
{
String obj = (String) element;
cell.setText(obj);
}
}
});
}
看起来像这样:
调整大小后: