SWT: Table Resizing Issue

拜拜、爱过 提交于 2019-12-13 07:15:20

问题


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.

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

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

On UI start up:

When user selects a product:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/40743629/swt-table-resizing-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!