8

I am using javafx table view control to display my table data. Now the data is quite huge and when I show the full data in the control it crashes. Is there any way to do so or will I have to cache the data so that I can display it in chunks. Should I use JTable instead ?

Vladimir Sachek
  • 1,126
  • 1
  • 7
  • 20
Aakash
  • 1,860
  • 19
  • 30
  • 2
    Implement table view pagination yourself and load data in chunks. – Uluk Biy Aug 22 '12 at 08:18
  • Are there any libraries or apis already developed that do this in Java or JavaFx ? – Aakash Aug 22 '12 at 08:20
  • There maybe 3rd party apis for Swing, but AFAIK there is no for JavaFX. It actually depends on your environment. For example, JPA has a paginated querying etc. The view part is not so hard to implement. – Uluk Biy Aug 22 '12 at 09:08
  • 2
    JavaFX has an inbuilt [pagination control](http://docs.oracle.com/javafx/2/ui_controls/pagination.htm) but (for JavaFX2.2) there is no code in the core (or 3rd party libraries I am aware of) that hooks the pagination control together to a JavaFX TableView - so you would need to handle that part yourself. – jewelsea Aug 22 '12 at 16:35
  • Thanks everyone for your opinions, I guess its of no use to wait for the guys at oracle. I should implement it myself. Thanks again 1! – Aakash Aug 23 '12 at 06:36
  • I'm very interested if you have write some code to do that ! – reyman64 Jan 20 '13 at 17:43
  • @reyman64 I was not satisfied with the speed TableView offered despite all my efforts so I used JTable from swing which was much faster, for now I will stick to it. – Aakash Oct 23 '13 at 09:18

2 Answers2

4

I made a lazy-load on tableview using the CellFactory class... I´ll try to explain, anyone with better english than mine fell free to edit and make it more understandable!

First I use a mandatory property in a Bean to check if that bean has already been loaded or not. This way I don't need to create an attribute specifically for that.

Second, I loaded into TableView my Bean collection just with the 'id' property loaded, what makes my table display all in blank.

At last, I create a CellFactory, like this one below, to check if object has loaded or not, and if not do it now.

namecolumn.setCellFactory(new Callback<TableColumn<PersonBean, String>, TableCell<PersonBean, String>>() {
@Override
  public TableCell<PersonBean, String> call(TableColumn<PersonBean, String> param) {
    TableCell<PersonBean, String> newtablecell = new TableCell<PersonBean, String>() {
      @Override
      protected void updateItem(String item, boolean empty) {
        // Since I got some strange errors when method getIndex() return greatter value than my collection size, I need to validate before getting the object
        if (getIndex() < getItems().size()) {
          //Retrieve the collection object
          PersonBean personbean = getItems().get(getIndex());
          // Name is mandatory, so if it is null my bean was not loaded yet
          if (personbean.getName() == null) {
            // Call other centralized method to make load, details explained later
            loadPersonBean(personbean);
          }
          // Now I put the value on TableCell. Remember "item" still empty so we need to use Bean
          setText(personbean.getName());
          setAlignment(Pos.CENTER);
        }
      }
    };
    return newtablecell;
  }
});

In the method loadPersonBean I pass the object contained in TableView Collection and load it from DataBase. Than I COPY all the needed data to the bean received from the table. I didn't try to swap the object in TableView, but I believe it may result a Concurrent Exception.

Other observation: In all my tests TableView always respects the column order to call my CellFactory, but I did not test if it still respects the order if user swap the column order. So I prefer to add a CellFactory with the "LazyLoadCheck" part of code to all columns.

Hope I made my self clear enough to help! If any questions remains, comment and I'll try to do a better explanation.

Community
  • 1
  • 1
Rodrigo Leitão
  • 949
  • 1
  • 11
  • 21
0

I'm not quite sure if I understand your question right, but what about lazy loading of the items? Actually there is an example within the Docs directly. Though it is for the TreeView and not for the TableView, you should be able to simply adapt the idea behind.

Hope that helps, at least for my TreeView, it solved a lot of problems so far.

Cheers, Regine

bully
  • 5,525
  • 3
  • 22
  • 26