I have some problems when i using javaFX GUI,the code below is the performance test when execute, i created thirty thousand button objects in javaFX and swing. When I executing the program, written in javaFX already occupied 700MB memory,and its increasing by time,but another sample written in swing only use 120MB memory and not increase.
this is the code using javaFX
public class ManyButtons_JavaFX extends Application{
private static final int ROWS = 300;
private static final int COLS = 100;
public void start( Stage stage ){
stage.setTitle("Many Buttons JavaFX");
stage.setWidth(600);
stage.setHeight(400);
GridPane grid = new GridPane() ;
grid.setHgap(10);
grid.setVgap(10) ;
for( int y = 0 ; y < ROWS ; y++ ){
for( int x = 0 ; x < COLS ; x ++ ) {
grid.add(new Button("Button " + x + "," + y ) , x , y ) ;
}
}
ScrollPane scroll = new ScrollPane(grid) ;
stage.setScene(new Scene(scroll) ) ;
stage.show() ;
}
public static void main( String[] args ){
launch(ManyButtons_JavaFX.class , args) ;
}
and that is written in swing
public class ManyButtons_Swing extends JFrame{
private static final int ROWS = 300;
private static final int COLS = 100;
ManyButtons_Swing(){
this.setTitle("Many Buttons Swing");
this.setSize(600,400) ;
JPanel grid = new JPanel(new GridLayout(ROWS , COLS , 10 , 10 )) ;
for( int y = 0 ; y < ROWS ; y++ ){
for( int x = 0 ; x < COLS ; x ++ ) {
grid.add(new JButton("Button " + x + "," + y ) ) ;
}
}
JScrollPane sc = new JScrollPane(grid) ;
this.setContentPane(sc);
this.setVisible(true);
}
public static void main( String[] args ){
new ManyButtons_Swing() ;
}
The program written in javaFX always use five times or more than use swing,and the execute windows cant be running fluent when the object is substantial increased(swing is ok). Do I have any ways to optimize it?