webforj / webforj-docs-samples

Programs that sample the various webforJ components utilized in documentation
MIT License
1 stars 3 forks source link

Declare and instantiate components as member variables in demos rather than in the run method #38

Open MatthewHawkins opened 8 months ago

MatthewHawkins commented 8 months ago

For example:

Good ✔️:

public class HelloWorldJava extends App {

  // Declare and instantiate as member variables 
  Paragraph hello = new Paragraph("Hello World!");
  Button btn = new Button("Say Hello");

  @Override
  public void run() throws DwcjException {

    // Frame should stay in the run method
    Frame mainFrame = new Frame();

    // ...
  }
}

Incorrect ❌:

public class HelloWorldJava extends App {

  @Override
  public void run() throws DwcjException {

    // Frame should stay in the run method
    Frame mainFrame = new Frame();

    // Here the variables are declared and instantiated in the run method, should be outside
     Paragraph hello = new Paragraph("Hello World!");
     Button btn = new Button("Say Hello");
    // ...
  }
}
hyyan commented 3 months ago

@MatthewHawkins

// Frame should stay in the run method

FYI, Frames can be declared as as member variables if the constructor is defined

public class MyApp extends App {
  Frame frame = new Frame();

  public MyApp() throws WebforjAppInitializeException {
    super();
  }

  @Override
  public void run() throws WebforjException {
     //...
     // frame.add(com1, comp2);
  }
}