JavaFX2-将自定义(fxml)面板动态添加到gridpane时,性能非常差 [英] JavaFX2 - very poor performance when adding custom made (fxml)panels to gridpane dynamically

查看:348
本文介绍了JavaFX2-将自定义(fxml)面板动态添加到gridpane时,性能非常差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题 我想在运行时将通过javafx场景构建器构建的定制面板添加到网格窗格中.我的定制面板有按钮,标签等.

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.

我在寻找什么 有没有一种方法可以通过javafx场景构建器来设计面板(结果是在fxml中包含此面板),然后在运行时将其添加到网格窗格中,而无需为每个实例使用此fxmlloader.由于fxml加载程序,我认为它的效果很差.当我添加标准按钮时如果没有fxml,速度会更快.

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.

推荐答案

简短答案:不,不是(从JavaFX 2.x和8.0开始).它可能是未来版本(JFX> 8)

Short answer: No, it is not (as of JavaFX 2.x and 8.0). It may be in a future version (JFX >8)

详细答案: FXMLLoader当前不设计为充当模板提供程序,该模板提供程序一遍又一遍地实例化同一项目.而是要成为大型GUI的一次性加载程序(或对其进行序列化).

Long answer: The FXMLLoader is currently not designed to perform as a template provider that instantiates the same item over and over again. Rather it is meant to be a one-time-loader for large GUIs (or to serialize them).

性能很差,因为在每次调用load()时,取决于FXML文件,FXMLLoader必须通过反射来查找类及其属性.这意味着:

The performance is poor because depending on the FXML file, on each call to load(), the FXMLLoader has to look up the classes and its properties via reflection. That means:

  1. 对于每个import语句,尝试加载每个类,直到可以成功加载该类.
  2. 对于每个类,创建一个BeanAdapter来查找该类具有的所有属性,并尝试将给定的参数应用于该属性.
  3. 再次通过反射将参数应用于属性.

对于代码中完成的对同一FXML文件的load()的后续调用,当前也没有任何改进.这意味着:不缓存找到的类,不缓存BeanAdapters等等.

There is also currently no improvement for subsequent calls to load() to the same FXML file done in the code. This means: no caching of found classes, no caching of BeanAdapters and so on.

但是,通过为FXMLLoader实例设置自定义类加载器,可以解决第1步的问题:

There is a workaround for the performance of step 1, though, by setting a custom classloader to the FXMLLoader instance:

import java.io.IOException; 
import java.net.URL; 
import java.util.Enumeration; 
import java.util.HashMap; 
import java.util.Map; 

public class MyClassLoader extends ClassLoader{ 
  private final Map<String, Class> classes = new HashMap<String, Class>(); 
  private final ClassLoader parent; 

  public MyClassLoader(ClassLoader parent) { 
    this.parent = parent; 
  } 

  @Override 
  public Class<?> loadClass(String name) throws ClassNotFoundException { 
    Class<?> c = findClass(name); 
    if ( c == null ) { 
      throw new ClassNotFoundException( name ); 
    } 
    return c; 
  } 

  @Override 
  protected Class<?> findClass( String className ) throws ClassNotFoundException { 
// System.out.print("try to load " + className); 
    if (classes.containsKey(className)) { 
      Class<?> result = classes.get(className); 
      return result; 
    } else { 
      try { 
        Class<?> result = parent.loadClass(className); 
// System.out.println(" -> success!"); 
        classes.put(className, result); 
        return result; 
      } catch (ClassNotFoundException ignore) { 
// System.out.println(); 
        classes.put(className, null); 
        return null; 
      } 
    } 
  } 

  // ========= delegating methods ============= 
  @Override 
  public URL getResource( String name ) { 
    return parent.getResource(name); 
  } 

  @Override 
  public Enumeration<URL> getResources( String name ) throws IOException { 
    return parent.getResources(name); 
  } 

  @Override 
  public String toString() { 
    return parent.toString(); 
  } 

  @Override 
  public void setDefaultAssertionStatus(boolean enabled) { 
    parent.setDefaultAssertionStatus(enabled); 
  } 

  @Override 
  public void setPackageAssertionStatus(String packageName, boolean enabled) { 
    parent.setPackageAssertionStatus(packageName, enabled); 
  } 

  @Override 
  public void setClassAssertionStatus(String className, boolean enabled) { 
    parent.setClassAssertionStatus(className, enabled); 
  } 

  @Override 
  public void clearAssertionStatus() { 
    parent.clearAssertionStatus(); 
  } 
}

用法:

public static ClassLoader cachingClassLoader = new MyClassLoader(FXMLLoader.getDefaultClassLoader()); 

FXMLLoader loader = new FXMLLoader(resource); 
loader.setClassLoader(cachingClassLoader); 

这大大提高了性能.但是,步骤2没有解决方法,因此这仍然可能是个问题.

This significantly speeds up the performance. However, there is no workaround for step 2, so this might still be a problem.

但是,在官方JavaFX jira中已经有对此功能的请求.您很高兴能够支持此请求.

However, there are already feature requests in the official JavaFX jira for this. It would be nice of you to support this requests.

链接:

  • FXMLLoader should be able to cache imports and properties between to load() calls:
    https://bugs.openjdk.java.net/browse/JDK-8090848

将setAdapterFactory()添加到FXMLLoader:
https://bugs.openjdk.java.net/browse/JDK-8102624

add setAdapterFactory() to the FXMLLoader:
https://bugs.openjdk.java.net/browse/JDK-8102624

这篇关于JavaFX2-将自定义(fxml)面板动态添加到gridpane时,性能非常差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆