SWT:表格大小调整问题 [英] SWT: Table Resizing Issue

查看:56
本文介绍了SWT:表格大小调整问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个作业项目,其中向用户显示产品目录,然后用户从目录中选择产品进行购买.

I am working on a homework project wherein the user is displayed a product catalog and then the user selects a product from the catalog to make a purchase.

我决定使用 SWT 构建一个基本的 UI.更不用说我刚刚开始学习 SWT.

I decide to build a basic UI using SWT. And not to mention I have just started learning SWT.

这就是我到目前为止所做的.UI 的第一个组件是一个 Table,用于显示产品目录.代码片段:

So this is what I have done so far. The first component of UI is a Table which displays the product catalog. Code snippet for this:

private void displayProductCatalog(List<Product> productList) {
        Group group = new Group(shell, SWT.NULL);
        group.setLayout(new GridLayout());
        Label label = new Label(group, SWT.NULL);
        label.setAlignment(SWT.CENTER);
        label.setText("Plese select a product by clicking on the desired row.");

        Table table = new Table(group, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        table.setLayoutData(data);
        String[] titles = { "Product ID", "Product Description", "Cost" };
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.BOLD | SWT.CENTER);
            column.setText(titles[i]);
            column.setWidth(300);
        }

        String currency = " " + CurrencyHelper.fetchCurrency();

        for (Product product : productList) {
            ProductDescription productDescription = product.getProductDescription();
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(0, productDescription.getProductId());
            item.setText(1, productDescription.getDescription());
            item.setText(2, productDescription.getPrice().toString() + currency);
        }

        table.addSelectionListener(new TableRowSelectionListener(vendingMachine));
    }

然后下一个组件又是一个只有两列的表格.当用户单击产品目录上的任何行时,会调用服务器以执行少量验证并计算含税的最终价格.然后第二个表填充了各种税收,这些税收与最终价格一起应用.所以在启动时,产品目录表被填充,第二个表被创建但留空(当用户做出选择时填充).代码片段:

Then the next component is again a Table with two columns only. When the user clicks on any row on product catalog, a call is made to server to perform few validations and to calculate the final price with tax. And then this second table gets populated with various taxes that were applied along with the final price which. So on start up, product catalog table is populated and this second table is created but is left empty (populated when user makes a selection). Code snippet:

private void displaySaleLineItem(List<Product> productList) {
        Group group = new Group(shell, SWT.NULL);
        group.setLayout(new GridLayout());
        Label label = new Label(group, SWT.NULL);
        label.setAlignment(SWT.CENTER);
        label.setText("Product Details.");

        saleLineItemTable = new Table(group, SWT.BORDER);
        saleLineItemTable.setLinesVisible(true);
        saleLineItemTable.setHeaderVisible(true);
        // GridData data = new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1);
        // saleLineItemTable.setLayoutData(data);
        for (int i = 0; i < 2; i++) {
            TableColumn column = new TableColumn(saleLineItemTable, SWT.BOLD | SWT.CENTER);
            column.setWidth(450);
        }
    }

填充第二个表的代码片段:

Code snippet where the second table is being populated:

@Override
    public void onPropertyEventBeforeSale(Sale sale) {
        TableItem item = new TableItem(saleLineItemTable, SWT.NONE);
        item.setText(0, sale.getProduct().getProductDescription().getDescription());
        item.setText(1, sale.getProduct().getProductDescription().getPrice().toString());

        for (TaxTypeModel taxTypeModel : sale.getTaxModel().getTaxTypeModelList()) {
            item = new TableItem(saleLineItemTable, SWT.NONE);
            item.setText(0, taxTypeModel.getTaxName());
            item.setText(1, taxTypeModel.getTaxValue().toString() + "%");
        }

        item = new TableItem(saleLineItemTable, SWT.NONE);
        item.setText(0, "TOTAL");
        item.setText(1, sale.getTaxModel().getProductPriceIncludingTax().toString());
    }

在 UI 启动时:

当用户选择产品时:

如您所见,第二个表格在填充表格时不会调整大小.虽然表格获得了垂直滚动窗格,但从用户的角度来看,这是一个不便.

As you can see, the second table doesn't resize when the table is populated. Although the table gets a vertical scroll pane, but it is an inconvenience from an User point of view.

你能在这里帮助我吗?我不确定这里到底出了什么问题.

Could you please help me here. I am not sure what exactly is wrong here.

推荐答案

如果您希望包含此数据的 Shell 调整大小,您需要调用 pack() 方法将条目添加到表后的 Shell.这将导致外壳重新计算外壳尺寸并重做组件布局.

If you want the Shell containing this data to resize you need to call the pack() method of the Shell after adding entries to the table. This will cause the shell to recalculate the shell size and redo the components layout.

这篇关于SWT:表格大小调整问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆