Question

There is interesting approach in Groovy: SwingBuilder. Example:

swing.edt {
    frame(title:'Frame', defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true, show:true) {
        vbox {
            textlabel = label("Click the button!")
            button(
                text:'Click Me',
                actionPerformed: {
                    count++
                    textlabel.text = "Clicked ${count} time(s)."
                    println "Clicked!"
                }
            )
            widget(sharedPanel())
            widget(sharedPanel())
        }
    }
}

I think, it make code is much more readable and maintainable. And I use similar approach in Java for Swing or Vaadin applications.

Example:

@Theme("mytheme")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {

  private VerticalLayout layout;

  @WebServlet(value = "/*", asyncSupported = true)
  @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "i.x.songbook.main.AppWidgetSet")
  public static class Servlet extends VaadinServlet {
  }

  @Override
  protected void init(VaadinRequest request) {
    setContent(layout = new VerticalLayout() {
      {
        setMargin(true);
        addComponent(new HorizontalLayout() {
          {
            setWidth("100%");
            {
              Label expandingGap = new Label();
              expandingGap.setWidth("100%");
              addComponent(expandingGap);
              setExpandRatio(expandingGap, 5f);
            }
            {
              Label titleLabel = new Label("<center>Title</center>",
                  ContentMode.HTML);
              addComponent(titleLabel);
              setExpandRatio(titleLabel, 30f);
            }
            {
              Label fixedGap = new Label();
              fixedGap.setWidth("10px");
              addComponent(fixedGap);
            }
            {
              TextField searchField = new TextField();
              searchField.setWidth("100%");
              searchField.setDescription("Search");
              addComponent(searchField);
              setExpandRatio(searchField, 50f);
            }
            {
              Label expandingGap = new Label();
              expandingGap.setWidth("100%");
              addComponent(expandingGap);
              setExpandRatio(expandingGap, 5f);
            }
          }
        });
        addComponent(new Button("Button") {
          {
            addClickListener(new Button.ClickListener() {

              @Override
              public void buttonClick(ClickEvent event) {
                layout.addComponent(new Label("Thank you for clicking"));

              }
            });
          }
        });
      }

    });

  }
}

What do you think about it?

Was it helpful?

Solution

That it creates a zillion anonymous inner classes for no reason whatsoever.

I agree about grouping components with {} but I don't see making anonymous classes justified, e.g.

// This is unnecessary and unjustified:
setContent(layout = new VerticalLayout() {
    // ...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top