0

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?

Ives
  • 505
  • 1
  • 4
  • 14
  • See [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) to make the two programs more comparable; [profile](http://stackoverflow.com/q/2064427/230513) to collect data for reference. – trashgod Mar 15 '16 at 02:27

1 Answers1

2

Consider using virtualized controls rather than a node for every data piece

30,000 buttons is a lot. In general, I don't advise adding many thousands of nodes to a scene. Instead, I suggest you use a virtualized control that only creates nodes to represent data visible on the screen, rather than all the possible data you have. This is how in-built controls such as TableView and ListView work. They have cell factories and that render dynamic cells that provide a view into the backing data and are continuously updated as the backing data changes (e.g. as the user scrolls up and down in the ListView). Such a strategy allows a TableView to efficiently represent a data structure with hundreds of thousands of items in it.

As you are basing your implementation on a GridPane, a virtualized control that is similar to GridPane functionality is a ControlsFX GridView. I suggest you take a look at that and see if it will fit your need.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • thank you,that is a good solution, but if i want to show all of nodes at the same time , javaFX cant bear it. – Ives Mar 17 '16 at 10:28
  • 1
    Yes, the user can't see all those nodes anyway because almost all of them do not fall within the visible area of your scroll pane. – jewelsea Mar 17 '16 at 17:50