Related to Auto-Update of JTable - java

I have two radiobuttons(Say rbtn_Asia,rbtn_Europe)and one JTable. When I select rbtn_Asia, table must contains Asia's data. Similarly when I select rbtn_Europe, table must contains Europe's data. (Asia's data and Europe's data is in same database which will be updated periodically). I have implemented upto this.
My problem is like this: Consider the following case: I have selected rbtn_Asia and obviously table will contain Asia's data. Now let database has got two new tuples of Asia, how can I update the JTable dynamically without selecting the rbtn_Asia once again (because rbtn_Asia is already in selected state).

In your button handler, update your implementation of TableModel, which should then fire the appropriate event. A structure that supports clear() such as Map, shown here, is convenient. More examples may be found here.

Related

Linking an editable tableView to a database in javaFX

I'm working on a project for JavaFX and databasing practice (mostly I'm a beginner), where I have a unique class which holds records about concerts (name, location, date etc). I originally held these in an observable arraylist which populated a table view. From here the records could be editted, deleted or new ones added.
I am now storing these in a database using ORMLite and sqlite. This is so I can perform filtering on the data (ie show all events in a particular location) and then show this on the table.
My issue is that when I read in the data from the database I convert it to a ObservableArrayList so the table view can use it, but by creating the new array list my edit/new/delete buttons only effect this list and not the database. The problem is that every time I perform a query such as adding or deleting records to the database, it needs to re-produce the observableArrayList for the tableView which is taking around 5 seconds with ~250k records.
Is there a more efficient way to work with databases and javaFx tableViews?
Is there a more efficient way to work with databases and javaFx tableViews?
How about setting some sort of dirty flag on the records when you update certain fields and then go through your list of records and only make the DAO calls on those that are dirty? Maybe an enum with CREATE, DELETE, UPDATE, NONE.
Another idea would be to do your dao.update(...), dao.delete(...), or dao.create(...) calls whenever you update an record although you may want to do the chances all once time.
Last idea is to keep 4 arrays. One with the entire list, one for the new records, one for the dirty records, and one for the records to be deleted. You would add the records to the lists as edit/new/delete buttons are pressed and then at the end you can save them all at once.

Why do we have to use a TableModel for a JTable?

If I want to use a JTable in Java it seems to me for adding rows and doing alters from behind a button or so I always have to use a TableModel (this could be the default one or one created by your own) But my question is: Why do we have to use this. I can't find this in any of the posts I saw. Can someone explain how this works and why it is necessary? And why we can't just add rows to the JTable without a model.
It seems to me that if you want to just show a few records but at creation you don't know all the rows yet it would be easier to just do something like a table.add() to add the row.
You can create the table with data inside without a model attached to it. So why not add data?
Or am I just wrong and can you add also data without a model?
The TableModel interface defines the minimum methods needed by a JTable (view) to render its content (model). AbstractTableModel is an abstract implementation that provides the event plumbing and leaves just three methods that must be overridden. DefaultTableModel goes on to include an internal data model based on Vector and convenient methods to alter that internal model. See Creating a Table Model for a comparison and these contrasting examples.

Auto-change TableModel based on Table view?

So I am working on a GUI that involves working with tables that can be sorted. I am noticing that when I sort a table, and I select a row from the newly sorted table, the selected row index of that row points to the data row from before the sort. I understand that this is because the view has changed, but the model has not; thus, you have the need for convertRowIndexToModel. It is also to my understanding that one can automatically update the view based on changes to the model by firing TableModelEvent's.
So here is my question: is it possible to automatically update the TableModel, based on changes to the view, so that I would not have to worry about converting the view index to table index?
All the default table code does this automatically so you don't need to worry about this. That is if you reference the model by using the table.getValueAt(...) and table.setValueAt(...) methods then you won't have a problem.
Only code that you write that tries to access the TableModel directly will have a problem. In this case thats what the convertXXX(...) methods are for.

processing a jsf datatable

I've a page with a datatable which can be considered a view on a table of a database. Some fields are static, but some others are inputText, which are meant to modify that field (in particular, all the fields of a specific column).
I would like to press a button and save the whole table.
In general, I'd like to understand how to read an entire table in a managed bean.
Thanks!
EDIT:
I'll try to be more clear:
imagine you've a NxM table of only inputText. Outside the table, a button with an action like #{SomeBean.process}. I'd like to have, in that process method, a List with length N and Row is an object with M fields.
If I understand correctly
I think, you need to save not a datatable, you need to save a rows from this table. With JSF you can use binding and when you need to save data, you just get rows from this binding and make data operations.
If you used some kind of object-relatational mapping like JPA it would be quite easy. Then you could create an entity class of your database table and fill your datatable with a list of your entity objects.
Then you could submit the whole datatable and merge all the changes with your database.
Some IDEs like Netbeans even create entity classes automatically with the help of a wizard. If you are on Netbeans, I recommend to read the JSF Crud tutorial. For Eclipse there should be similiar tutorials, but I am not so familiar with this IDE.

Does SWT Table emit column created / removed events?

Skimmed through the Eclipse API doc and found no mentioning of this event. Could I have missed something? I'm building something that lists all table columns in a given table, and it needs to be automatically updated if the set of columns in the Table changes, so I need to listen to the Table structure changes and update my list accordingly.
What you are looking for doesn't exist. You could add dispose listeners to the TableColumns, and that could tell you when one is removed, but there are no events for telling you when widgets are created. You might be able to "fake it" by tracking control events on the table columns with TableColumn#addControlListener, but this is iffy. If you have control of the tables you could do something more advanced by wrapping them in a dynamic proxy that could perform that function for you.

Categories