gpt4 book ai didi

java - 调整 swt-table 的大小而不强制滚动条

转载 作者:行者123 更新时间:2023-12-01 11:31:44 24 4
gpt4 key购买 nike

以下代码为我提供了一个类似于下面图片链接的构造。例如,我有一个主要组,里面有 3 个组。每个组都有一个包含 3 列且最小宽度为 200 的表格。如果调整组合的大小,表格和组的大小也会调整。但如果大小小于200,则每个表格下方都会出现滚动条。事实上,我的目的是在表格下方看到一个滚动条。我也尝试过在主组和较小组之间使用 ScrolledComposite-“层”,但它不起作用。有人可以给我一个例子,如何创建一个滚动条而不是 3 个?

谢谢!

图片:http://www.bilder-upload.eu/show.php?file=d94e50-1432047327.jpg

public class GroupTable
{
private static Composite composite;
private static ArrayList<Table> tablesList = new ArrayList<Table>();
private static int minWidth = 100;

public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

composite = new Composite(shell, SWT.BORDER);
GridLayout layout = new GridLayout(1, false);
layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
composite.setLayout(layout);

// MainGroup
Group gr = new Group(composite, SWT.NONE);
GridLayout gLayout = new GridLayout(3, false);
gr.setLayout(gLayout);
GridData griddata = new GridData(SWT.FILL, SWT.FILL, true, false);
gr.setLayoutData(griddata);
gr.setText("Main Group");

for (int i = 0; i < 3; i++)
{
createGroups(gr, layout, i);
}

composite.addControlListener(new ControlAdapter()
{
@Override
public void controlResized(ControlEvent e)
{
for (Table table : tablesList)
{
Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
ScrollBar vBar = table.getVerticalBar();
Composite group = table.getParent();
Rectangle area = group.getClientArea();
Rectangle t1 = area;
Rectangle t2 = table.computeTrim(0, 0, 0, 0);
Point t3 = vBar.getSize();

int width = t1.width - t2.width - t3.x;
if (size.y > area.height + table.getHeaderHeight())
{
// Subtract the scrollbar width from the total column
// width if a vertical scrollbar will be required
Point vBarSize = vBar.getSize();
width -= vBarSize.x;
}

TableColumn[] columnse = table.getColumns();
int colCount = columnse.length;
Point oldSize = table.getSize();
if (oldSize.x > area.width)
{
// table is getting smaller so make the columns
// smaller first and then resize the table to
// match the client area width
for (TableColumn column : columnse)
{
if (width / colCount < minWidth)
{
width = minWidth * colCount;
}
column.setWidth(width / 3);
}
table.setSize(area.width, area.height);
}
else
{
// table is getting bigger so make the table
// bigger first and then make the columns wider
// to match the client area width
table.setSize(area.width, area.height);
for (TableColumn column : columnse)
{
if (width / colCount < minWidth)
{
width = minWidth * colCount;
}
column.setWidth(width / 3);
}
}
}
}
});

shell.setSize(900, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

private static void createGroups(Composite parentGroup, GridLayout layout, int i)
{
Group group = new Group(parentGroup, SWT.NONE);
group.setLayout(layout);
GridData griddata1 = new GridData(SWT.FILL, SWT.FILL, true, true);
group.setLayoutData(griddata1);
group.setText("group " + i);
createTables(group, i);
}

private static void createTables(Group parent, int tn)
{
Table table = new Table(parent, SWT.FULL_SELECTION);
tablesList.add(table);
TableViewer localTableViewer = new TableViewer(table);
localTableViewer.getControl().setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, true));

for (int j = 0; j < 3; j++)
{
TableViewerColumn column = new TableViewerColumn(localTableViewer,
SWT.NONE);
column.getColumn().setText("column " + j);
column.getColumn().pack();
}

for (int i = 0; i < 10; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[]{"i ", "am", "table " + tn});
}
table.setHeaderVisible(true);
}
}

最佳答案

这是一个使用 ScrolledComposite 的示例:

public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
scrolledComposite.setLayout(new FillLayout());

Composite content = new Composite(scrolledComposite, SWT.NONE);
content.setLayout(new GridLayout(3, true));

for(int i = 0; i < 3; i++)
{
Table table = new Table(content, SWT.BORDER);
table.setHeaderVisible(true);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

for(int col = 0; col < 3; col++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText("Col " + col);
}

for(int row = 0; row < 10; row++)
{
TableItem item = new TableItem(table, SWT.NONE);

for(int col = 0; col < table.getColumnCount(); col++)
{
item.setText(col, "Item " + row + " " + col);
}
}

for(int col = 0; col < table.getColumnCount(); col++)
{
table.getColumn(col).pack();
}


}

scrolledComposite.setContent(content);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT));

shell.pack();
shell.setSize(400,300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

看起来像这样:

不需要滚动条:

enter image description here

一个滚动条:

enter image description here

关于java - 调整 swt-table 的大小而不强制滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30328955/

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