1

Now there are a couple answers on "how to add data from database to TableView" which I am only going to link one as they are all pretty much duplicates.

So why am I creating a new "duplicate question"? I'm not. I understand the approach used in the answers and even in some blogs, but I don't like it.

The reason I'm using a database is because I wanted to stop wasting memory on holding the data in some sort of list in my program. Since I've done some android development I learned how useful databases can be. Okay, what is my question really about then?

All these approaches use ObservableLists and I wanted to avoid that.

Android has something called a Cursor which pretty much holds the selected data and can be used to fill all kind of content using an adapter.

I have the feeling using ObservableList makes the database useless as in the end, I got everything in a List again.

Is there any way to fill the tables without the use of ObservableLists as it is described in the answer above?

Community
  • 1
  • 1
Maverick283
  • 1,284
  • 3
  • 16
  • 33
  • 1
    Hmm, ObservableLists aren't particularly inefficient IMO for many things. Of course, it varies based upon the application, but an [observableArrayList](https://docs.oracle.com/javase/8/javafx/api/javafx/collections/FXCollections.html#observableArrayList--) will suffice for many applications. It is probably not the choice of data structure which is causing your concern but the size of it. Perhaps what you really seek is just a [paginated table mechanism](http://stackoverflow.com/questions/15349185/javafx-tableview-paginator). – jewelsea Jun 28 '16 at 11:24

3 Answers3

1

I am not aware of an exact solution to your requirement (unless one could use the Android classes directly), but I would explore DataFx as my "adapter" and Flowless as a lazy, dumb and performant ListView.

Oliver Jan Krylow
  • 1,758
  • 15
  • 22
1

I would say the use of a list is probably not required.....however no matter what you present in the table....it is probably going to be a list as you said yourself,you can present your content in the list but what's the point since you have it all in your database? Well the answer is that you can query your database and display a limited amount of content depending on what is required, the use of the database is usually for containing a large amount of content that you usually do not want stored in ram all at once, at-least in most cases, and that you can also perform query operations on. So when you consider your implementation you are going to have to consider whether or not you really need the database more than whether you need the list. So in short, consider whether you can query parts of your information from the database and present that in a list rather than trying to avoid using a list.

See this example for how to query and add the results to an observable list here

Community
  • 1
  • 1
D3181
  • 2,037
  • 5
  • 19
  • 44
-1

Include this

rs2xml.jar

in your projects libraries then in your class import this class

 import net.proteanit.sql.DbUtils; 

In your code if your table component has a variable name e.g. tblUsers and you have fetched your results from the database e.g.

 sql = "SELECT id, name FROM users";

 pst = con.prepareStatement(sql);

 result = pst.executeQuery();

 //the below code will populate the table with database values

 tblUsers.setModel(DbUtils.resultSetToTableModel(result));
Mfuon Leonard
  • 192
  • 4
  • 19
  • And how are the columns from the database and the ones from my TableView associated? A.k.a. how does it know which column to put `id` in? – Maverick283 Jun 28 '16 at 08:43
  • It populates the table with the order of headers in the table from the database. For the table component you can define the table headers by its properties. select the table component then right-click on it and navigate to its properties. – Mfuon Leonard Jun 28 '16 at 08:49