简体   繁体   中英

swt menu on click hide display composites

I am building a SWT application and have a menu created. Menu has multiple menu items like Add, Edit, Help. On click of each Menu Item, I want to show a composite which will display the details of it. I am able to build it, problem I am facing is, the space of hidden composite is not taken by visible composite. How can we make the composite occupy the entire space.

Also I am adding the selection listener to make the current composite visible and other composite hidden. In the current app there will multiple menu items and each one will have composite associated it. Listener needs reference of all composites to make them visible/hidden. Is there any better approach to do this.

public class MenuToggle {

    boolean startup = true;
    Menu menu, fileMenu, helpMenu;
    Composite composite1,composite2;

    public MenuToggle(Shell shell) {
        createMenu(shell);
        createFileView(shell);
        createHelpView(shell);
        startup = false;
    }

    public void createMenu(Shell shell) {
        //Menu Bar
        menu = new Menu(shell, SWT.BAR);
        //File Menu
        fileMenu = new Menu(shell, SWT.DROP_DOWN);
        
        MenuItem fileMenuHeader = new MenuItem(menu, SWT.CASCADE);
        fileMenuHeader.setText("&File");
        fileMenuHeader.setMenu(fileMenu);

        MenuItem fileSaveItem = new MenuItem(fileMenu, SWT.PUSH);
        fileSaveItem.setText("&Save");
        
        MenuItem fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
        fileExitItem.setText("E&xit");
        
        //Help Menu
        helpMenu = new Menu(shell, SWT.DROP_DOWN);
        MenuItem helpMenuHeader = new MenuItem(menu, SWT.CASCADE);
        helpMenuHeader.setText("&Help");
        helpMenuHeader.setMenu(helpMenu);

        MenuItem helpGetHelpItem = new MenuItem(helpMenu, SWT.PUSH);
        helpGetHelpItem.setText("&Get Help");
        
        shell.setMenuBar(menu);
        
        fileSaveItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                composite1.setVisible(true);
                ((GridData)composite1.getLayoutData()).exclude = false;
                composite2.setVisible(false);
                ((GridData)composite2.getLayoutData()).exclude = true;
                composite2.layout(true, true);
            }
        });
        helpGetHelpItem.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                composite1.setVisible(false);
                ((GridData)composite1.getLayoutData()).exclude = true;
                composite2.setVisible(true);
                ((GridData)composite2.getLayoutData()).exclude = false;
                composite2.layout(true, true);
            }
        });
    }
     
    public void createFileView(Shell shell) {
        composite1 = new Composite(shell, SWT.BORDER);
        composite1.setVisible(true);
        GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true);
        composite1.setLayoutData(gd1);
        composite1.setLayout(new GridLayout(1,true));
        Label label = new Label(composite1, SWT.CENTER);
        label.setBounds(composite1.getClientArea());
        label.setText("Saved");
    
    }
    
    public void createHelpView(Shell shell) {
        composite2 = new Composite(shell, SWT.BORDER);
        composite2.setVisible(false);
        GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);
        composite2.setLayoutData(gd2);
        composite2.setLayout(new GridLayout(1,true));
        Label label1 = new Label(composite2, SWT.CENTER);
        label1.setBounds(composite2.getClientArea());
        label1.setText("No worries!");
        
    }
    
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setText("Menu Display");
        MenuToggle instance = new MenuToggle(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }


}

There are a number of issues here.

You are using FillLayout for the Shell layout, so the GridData you are setting on the composites is ignored. You must use GridLayout for the Shell :

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

When you change the exclude settings you must call layout on the parent of the composite - the shell:

   fileSaveItem.addSelectionListener(new SelectionAdapter() {
       @Override
       public void widgetSelected(final SelectionEvent e) {
           composite1.setVisible(true);
           ((GridData)composite1.getLayoutData()).exclude = false;
           composite2.setVisible(false);
           ((GridData)composite2.getLayoutData()).exclude = true;
           shell.layout(true, true);  // change
       }
   });
   helpGetHelpItem.addSelectionListener(new SelectionAdapter() {
       @Override
       public void widgetSelected(final SelectionEvent e) {
           composite1.setVisible(false);
           ((GridData)composite1.getLayoutData()).exclude = true;
           composite2.setVisible(true);
           ((GridData)composite2.getLayoutData()).exclude = false;
           shell.layout(true, true); // change
       }
   });

You are calling setBounds on the Label controls, this does not work when you are using layouts because the layout also calls setBounds and overrides your settings, use setLayoutData instead

   Label label = new Label(composite1, SWT.CENTER);
   label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));  // replace
   //label.setBounds(composite1.getClientArea());  // wrong

As for dealing with lots of Composite controls you could call shell.getChildren and loop through the child controls. Or add the composites to a List and loop through that.

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