Does SWT Table emit column created / removed events? - java

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.

Related

Nattable loose selection just after loading data

I'm using Nattable for almost all my tables in my application. Now an user reported an issue regarding the selection in Nattable.
When my application loads data the data is shown, when the user select an item just after the data loading, it will remove the selection. When the user wait a few seconds and does the selection again it holds.
Is this a known behavior of Nattable? For me it looks like the data is still loading when the first selection took place. The data load is 11K items with multiple cell labelers.
Without seeing the details it is hard to tell. Actually I wonder how something is rendered while the data is still loaded. But I suppose you have some interesting lazy loading mechanism to make the table accessible fast.
From my experience the behavior you describe happens because of a StructuralChangeEvent. Once the data is completely loaded such an event is fired to update all layers accordingly. A structural change is used to completely clear, that also implies the selection when using the default SelectionModel. The selection in that model is stored by row index. But as a structural change means this could have been changed, the selection is removed to avoid that something is shown selected that was not selected before.
Either you use an extended selection model like the RowSelectionModel or the PreserveSelectionModel or you double check when the StructuralChangeEvent is fired and if you can deal with that. But I suppose an alternative selection model should be more interesting with regards to your data loading mechanism.

Sort Behavior of Table View in JavaFX

I intend to write an JFX-Application that displays data from database tables that contain up to 1M entries. Unfortunately, the standard API makes it difficult to support lazy loading. Reading the whole table is not an option (even though it performs well up to 100K entries).
Paging can be accomplished using a custom list class provided as model to the TableView. However, I also need to control the sorting behaviour of the TableView, since in my case, this would be left to the dbms.
An ugly solution is to make the columns unsortable, and add a Click-Event listener to the Column-Header. Apart from the coding, this also makes it impossible to display the current sort order in the usual way.
It is possible to listen to a change to columnSortOrder, but the standard sorting mechanism is still applied. What I would like to be able to do is display the standard ascending/descending icon in the column header while having full control of the sort behaviour of the TableView.
[Edit: In short, I want to implement lazy loading. I want a TableView that behaves exactly like normal TableView but does not sort the list when the user clicks on a column header. However, the sort order should still be displayed in the column header label.]
Is that somehow possible to achieve?
Many thanks for your answers in advance!
For reference:
This has been added to JavaFX-8. One can use TableView.setSortPolicy(...) to customize sorting.

Related to Auto-Update of JTable

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.

keeping the history of table in java

I need the sample program in Java for keeping the history of table if user inserted, updated and deleted on that table. Can anybody help in this?
Thanks in advance.
If you are working with Hibernate you can use Envers to solve this problem.
You have two options for this:
Let the database handle this automatically using triggers. I don't know what database you're using but all of them support triggers that you can use for this.
Write code in your program that does something similar when inserting, updating and deleting a user.
Personally, I prefer the first option. It probably requires less maintenance. There may be multiple places where you update a user, all those places need the code to update the other table. Besides, in the database you have more options for specifying required values and integrity constraints.
Well, we normally have our own history tables which (mostly) look like the original table. Since most of our tables already have the creation date, modification date and the respective users, all we need to do is copy the dataset from the live table to the history table with a creation date of now().
We're using Hibernate so this could be done in an interceptor, but there may be other options as well, e.g. some database trigger executing a script, etc.
How is this a Java question?
This should be moved in Database section.
You need to create a history table. Then create database triggers on the original table for "create or replace trigger before insert or update or delete on table for each row ...."
I think this can be achieved by creating a trigger in the sql-server.
you can create the TRIGGER as follows:
Syntax:
CREATE TRIGGER trigger_name
{BEFORE | AFTER } {INSERT | UPDATE |
DELETE } ON table_name FOR EACH ROW
triggered_statement
you'll have to create 2 triggers one for before the operation is performed and another after the operation is performed.
otherwise it can be achieved through code also but it would be a bit tedious for the code to handle in case of batch processes.
You should try using triggers. You can have a separate table (exact replica of your table of which you need to maintain history) .
This table will then be updated by trigger after every insert/update/delete on your main table.
Then you can write your java code to get these changes from the second history table.
I think you can use the redo log of your underlying database to keep track of the operation performed. Is there any particular reason to go for the program?
You could try creating say a List of the objects from the table (Assuming you have objects for the data). Which will allow you to loop through the list and compare to the current data in the table? You will then be able to see if any changes occurred.
You can even create another list with a object that contains an enumerator that gives you the action (DELETE, UPDATE, CREATE) along with the new data.
Haven't done this before, just a idea.
Like #Ashish mentioned, triggers can be used to insert into a seperate table - this is commonly referred as Audit-Trail table or audit log table.
Below are columns generally defined in such audit trail table : 'Action' (insert,update,delete) , tablename (table into which it was inserted/deleted/updated), key (primary key of that table on need basis) , timestamp (the time at which this action was done)
It is better to audit-log after the entire transaction is through. If not, in case of exception being passed back to code-side, seperate call to update audit tables will be needed. Hope this helps.
If you are talking about db tables you may use either triggers in db or add some extra code within your application - probably using aspects. If you are using JPA you may use entity listeners or perform some extra logic adding some aspect to your DAO object and apply specific aspect to all DAOs which perform CRUD on entities that needs to sustain historical data. If your DAO object is stateless bean you may use Interceptor to achive that in other case use java proxy functionality, cglib or other lib that may provide aspect functionality for you. If you are using Spring instead of EJB you may advise your DAOs within application context config file.
Triggers are not suggestable, when I stored my audit data in file else I didn't use the database...my suggestion is create table "AUDIT" and write java code with help of servlets and store the data in file or DB or another DB also ...

JTable - fire row changed when row deleted

In my system we use JTable with data model.
when data changes we remove it from the model, iterate the model and fire for each row listElementPropertyChanged (I think its intellij's). In this way removed lines are not deleted cause they are not in the model.
How do I refresh the whole table according to the model?
Simply use fireTableDataChanged(). This way, all listeners will now that all data may have changed.
However, use it with care, as usual behaviour for listeners will be to refresh the whole table.
You would have better using fireTableRowsDeleted(int, int) with the removed rows indexes.

Categories