JavaFX2 - very poor performance when adding custom made (fxml)panels to gridpane dynamically

Viewed 8123

Problem I want to add custom made panels, built via javafx scene builder, to a gridpane at runtime. My custom made panel exsits of buttons, labels and so on.

My Attempt I tried to extend from pane...

public class Celli extends Pane{
    public Celli() throws IOException{
        Parent root = FXMLLoader.load(getClass().getResource("Cell.fxml"));
        this.getChildren().add(root);     
    }
}

... and then use this panel in the adding method of the conroller

@FXML
private void textChange(KeyEvent event) {
    GridPane g = new GridPane();
        for (int i=0 : i<100; i++){
                g.getChildren().add(new Celli());
        }
    }
}

It works, but it performs very very poor.

What I am looking for Is there a way to design panels via javafx scene builder (and as a result having this panels in fxml) and then add it to a gridpane at runtime without make use of this fxmlloader for each instance. I think it performs poor because of the fxml loader. When I add a standard button e.g. whitout fxml it is very much faster.

3 Answers

Just adding code for "caching of already loaded classes" in @Sebastian sir given code. It is working for me. Please suggest changes in it for better performance.

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
    System.out.println("In Class loader");

    Class result;
    System.out.println(" >>>>>> Load class : "+name);
    result = (Class)classes.get(name);
    if(result != null){
        System.out.println(" >>>>>> returning cached class.");
        return result;
    }else{
    Class<?> c = findClass(name);
    if ( c == null ) {
      throw new ClassNotFoundException( name );
    }
    System.out.println(" >>>>>> loading new class for first time only");
    return c;
    }
}
Related