Search code examples
eclipsebuttonpluginsswtzest

Plugin development: composite of button in a view plugin


I am using zest to create plugin that show graph. I am trying to create button that refresh the plugin. The button is working, and I see the graph in the view also. But, the button is taking a lot of space in the view, and limit the place for the graph.I want that the button want limit the graph place.. How can I fix it? Thanks![enter image description here]

public void createPartControl(Composite parent) {

   //create refresh button

   Composite container = new Composite(parent, SWT.NONE);

   container.setLayout(new GridLayout(1, false));
   Button btnMybutton = new Button(container, SWT.PUSH);
   btnMybutton.setBounds(0, 10, 75, 25);
   btnMybutton.setText("Refresh Graph");
   btnMybutton.addSelectionListener(new SelectionListener() {

      @Override
      public void widgetSelected(SelectionEvent e) {
         init();
      }

      @Override
      public void widgetDefaultSelected(SelectionEvent e) {
         // TODO Auto-generated method stub
      }
   });

   // Graph will hold all other objects
   graph = new Graph(parent, SWT.NONE);
}

Solution

  • If you want to show the button on top of the graph, you should use a FormLayout instead of a GridLayout:

    public void createPartControl(Composite parent) {
    
       //create refresh button
    
       Composite container = new Composite(parent, SWT.NONE);
       container.setLayout(new FormLayout()); // Use FormLayout instead of GridLayout
    
       Button btnMybutton = new Button(container, SWT.PUSH);
       // btnMybutton.setBounds(0, 10, 75, 25); This line is unnecessary
    
       // Assign the right FormData to the button
       FormData formData = new FormData();
       formData.left = new FormAttachment(0, 5);
       formData.top = new FormAttachment(0, 5);
       btnMybutton.setLayoutData(formData);
    
       btnMybutton.setText("Refresh Graph");
       // Use SelectionAdapter instead of SelectionListener
       // (it's not necessary but saves a few lines of code)
       btnMybutton.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
             init();
          }
       });
    
       // Graph will hold all other objects
       graph = new Graph(container, SWT.NONE); // Note parent changed to container
    
       // Assignt the right FormData to the graph
       FormData formData2 = new FormData();
       formData2.left = new FormAttachment(0, 0);
       formData2.top = new FormAttachment(0, 0);
       formData2.right = new FormAttachment(100, 0);
       formData2.bottom = new FormAttachment(100, 0);
       graph.setLayoutData(formData2);
    }