tabs and db access

Hi,

i add tabsheet whith 4 tabs, in every tab i have some components added with some db access, the pbme is all db access are done when the tabsheet is created, i was expect every access is done when i click to the corresponding tab, as is done now is not optimized.

Walid

The TabSheet component only paints the selected tab and tabs that have already been seen by the user in the browser. However, when adding a tab to the TabSheet, the Component on that tab has to be constructed. If the database access is performed from the constructor, it happens before you even put the component in a TabSheet.

Adding a tab does call addComponent() and therefore also attach(), so currently attach events are generated for all added tabs, but you can listen to TabSheet.SelectedTabChangeEvent to initialize the contents of individual tabs when necessary, not before.

In general, with complex views or ones that access external data sources, it is a good idea to split the initialization phase so that no heavy operations or operations with external dependencies are performed from the constructor. Instead, one can e.g. define an interface with a method that should be called when the component is “activated”. This usually also improves modularity and makes testing easier.

Walid, as Henri already suggested, it is really a good idea is to register your listener and catch the tab activation event, so when user will switch the tab, your code get a control and may perform any database roundtrips tasks.

However, if those tasks are lengthy, this will stuck the tab switching which is also not very good from the user point of view (I mean usability). So in that case I’d recommend you to also add background thread invocation for data loading - this will allow for tab to be visually switched almost immediately and the tab contents will be then loaded separately and shown as completed.