简体   繁体   中英

How to design a Composite in SWT

I want to put 3 fixed size group in the composite in A side. And I want to place the image and the label middle of this group. My example image and code is below. The problem is the groups are resize according to label size in them and labels were written top of the group, but I want the place groups as equal fixed size to cover width of A side and labels should be vertically middle of the group.

private void designComposite() {

    Section sectionA = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
    sectionA.setText("A");

    Composite sectionClientA = toolkit.createComposite(sectionA);
    sectionClientA.setLayout(new RowLayout());

    Composite dynamicDataComp = toolkit.createComposite(sectionClientA);

    dynamicDataComp.setLayout(new RowLayout());

    Group group_incom = new Group(dynamicDataComp, SWT.NONE);
    group_incom.setLayout(new RowLayout());
    Label lbl_img_incom = new Label(group_incom, SWT.CENTER);
    Image img_incom = new Image(lbl_img_incom.getDisplay(),
            "<path>");
    lbl_img_incom.setImage(img_incom);
    group_incom.setText("# of Incoming Messages :");
    Label lbl_incomMsg = toolkit.createLabel(group_incom, "99", SWT.CENTER | SWT.VERTICAL);
    Font incomFont = new Font(lbl_incomMsg.getDisplay(), new FontData("Arial", 12, SWT.BOLD));
    lbl_incomMsg.setFont(incomFont);
    lbl_incomMsg.pack();

    Group group_outgo = new Group(dynamicDataComp, SWT.NONE);
    group_outgo.setLayout(new RowLayout());
    Label lbl_img_outgo = new Label(group_outgo, SWT.CENTER);
    Image img_outgo = new Image(lbl_img_outgo.getDisplay(),
            "<path>");
    lbl_img_outgo.setImage(img_outgo);
    group_outgo.setText("# of Outgoing Messages :");
    Label lbl_outgoMsg = toolkit.createLabel(group_outgo, "145639612", SWT.CENTER);
    Font outgoFont = new Font(lbl_outgoMsg.getDisplay(), new FontData("Arial", 13, SWT.BOLD));
    lbl_outgoMsg.setFont(outgoFont);
    lbl_outgoMsg.pack();

    Group group_error = new Group(dynamicDataComp, SWT.NONE);
    group_error.setLayout(new RowLayout());
    Label lbl_img_error = new Label(group_error, SWT.CENTER);
    Image img_error = new Image(lbl_img_error.getDisplay(),
            "<path>");
    lbl_img_error.setImage(img_error);
    group_error.setText("# of Error Messages :");
    Label lbl_errorMsg = toolkit.createLabel(group_error, "345", SWT.CENTER);
    Font errorFont = new Font(lbl_errorMsg.getDisplay(), new FontData("Arial", 13, SWT.BOLD));
    lbl_errorMsg.setFont(errorFont);
    lbl_errorMsg.pack();

    sectionA.setClient(sectionClientA);

}

在此处输入图片说明

One way is to use GridLayout for the parent composite and tell it use 3 equal sized columns:

Composite dynamicDataComp = toolkit.createComposite(sectionClientA);

dynamicDataComp.setLayout(new GridLayout(3, true));

Use the GridLayout and set the columns equal width:

Composite dynamicDataComp = new Composite(parent, SWT.NONE);
    dynamicDataComp.setLayout(new GridLayout(3, true));
    dynamicDataComp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

Group group_incom = new Group(dynamicDataComp, SWT.NONE);
        group_incom.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        group_incom.setLayout(new RowLayout());

//...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM