Rahul Dev Mishra Rahul Dev Mishra - 1 year ago 77
Java Question

SWT: Table Resizing Issue

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

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.setText("Plese select a product by clicking on the desired row.");

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

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.setText("Product Details.");

saleLineItemTable = new Table(group, SWT.BORDER);
// 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);

Code snippet where the second table is being populated:

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:

enter image description here

When user selects a product:
enter image description here

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download