I've got large Swing application with more than 50 views (forms, dialogs, menus, popups, etc..). What I need is some intelligent way of persisting view states and bringing them back to life. There are lots of tables, splitters, check boxes and other visual elements which users would want to have preserved. Of course I'd be able to do this manually.. but on such a scale it would be a monstrously tedious coding and bugs all around the place.
The question:
Is there any framework, library or a product which does this without too much hassle? The problem sounds to be pretty common place, so I'd rather find something solid instead of re-inventing the wheel.
First, separate your data (model) from your Swing components (view)
Once you have a complete data model, you can use an XML generator / XML parser to save and restore your data model, respectively. You can use a relational database to save and restore your data model. Whatever makes the most sense for the amount of data in your model.
I have an example of a Java Swing model and a view in this answer.
Edited to add: One of Dima's assertions is Take for example a JTable with 20 columns 5 of which user made visible while hiding others and also making columns of a certain width.
Fine. The TableModel, column visibility booleans, and column width integers or doubles had better be somewhere in your model.
Follow the link to my other answer and see that my GUI state data is part of the model.
Related
My task is to create a grid with different cars on one axis, and different dates on another one. The content of the grid is going to be different tasks that are going to be carried out by different cars on different days. Basically - its a scheduling program. There has to be a drag and drop function between a list of tasks and the grid. I've been trying to get started all day but have not come far. I really need your help me with the following:
What component should I use to create the grid? Labels for different dates with boxes that together form a grid, or should I use a specific grid function?
What component should I use to create the list of tasks? I would like to be able to add tasks to the list that I can then drag out to the grid, but it's not a requirement. Ive tried to use the Listselector because of its persuasive list-appearance, but as the name might reveal its a selector. I really cant find any method to add things to the list, or for that matter to create the drag function?
I would be exteremly thankful if you would lend me a helping hand. And please - feel free to PM me or write a comment here.
Have you seen the Vaadin Demo site? They have many widgets used in various scenarios. Click to see the source code.
Here is one that does almost exactly your case: Drag items between tree and table.
Vaadin is brilliantly designed, with consistency. Once you learn about a few widgets you will see the same patterns being used elsewhere. In particular, study the Item and Container interfaces and classes, as discussed in this chapter Collecting Items in Containers.
Read various chapters the Book of Vaadin web site. Lots of info and examples.
I am trying to model a simple diagram editor that stores data into GAE's HRD datastore, and I am facing problems that I am wondering if you can give me some advices on. Basically, the editor's model are:
[Diagram] --contains--> [Shapes] -- contains--> [Text]
Initially, I put these into the same entity group, to make sure the data is consistent. By doing so, whenever I add new shapes or add text objects to existing shapes, they show up properly (as it's queried from datastore); however, I am having problem because the user could be adding many shapes quickly, and that leads to more than one update to the diagram object per second, which leads to write contention.
Alternatively, I could design like so:
[Diagram] [ Shape - contains diagramId] [ Text - contains shapeId]
which put them in different entity group, and when I create a new shape I just have to save the shape object itself. This solves the write contention problem, but the data is no longer consistent -- depending on how soon HRD commits the write, I might get stale data.
I tried combinations such as putting the diagrams in the cache, and retrieve from the HRD only if the cache doesn't contain the diagram; however, this is unpredictable as I can't be sure that the diagram would be in the cache (I expect many diagrams to be edited at the same time)...
What's the best practice on dealing with such issue? I am using Java and JDO, if that makes any difference...
Few notes:
HRD reads are strongly consistent if you use get and eventually consistent if you use query (as queries rely on indexes and those need time to be built). You can get around your problem by using get, if possible.
If Texts and Shapes are part of one Diagram and if you do not need to access them separatelly AND you do not need to search by Text's or Shape's properties, then you could just serialize Text and Shape inside a Diagram. I use Objectify and this is simply achieved with the #Serialize annotation on a field (don't know about JDO as I don't use it).
I have a table that displays the output from a database, this can often be up to or over 10,000 rows. I'm looking to implement a 'quick find' function that will allow the user to easily scroll to a row with the value for which they search.
The idea is for this to be very similar to Chrome or Firefox's quick find. So:
User hits 'CTRL+F'
A box pops up:
The user can then quickly skim through the table pushing / or /\ to each matching value
My question is, does such a table already exist? I have looked at NatTable and XViewer but they both seem like rather large additions for a relatively minor change.
Another library I've looked at that is close to fitting the billl is Lucene TableSearcher unfortunately this is designed for filtering rather than highlighting. However it may be an avenue worth investigating.
It's not quite the same, but JFace does have support for filtering views. You type in a text box and view contents shrink to what matches the filter. It's basically another way to present search/find functionality to the user.
You can see this in action in Eclipse Preferences (the filter text field about the navigation tree in the dialog). See org.eclipse.ui.dialogs.FilteredTree class. I don't believe there is a FilteredTable or equivalent, but underlying facilities are pretty generic, so it shouldn't take much work to put one together. Take a look at FilteredTree source and go from there.
The solution I chose wasn't as simple as I'd hoped but has a satisfactory performance.
The tables involved are virtual tables that can be very large. So on top of this upon loading the dataset we start a thread to build an index in the form of a list. Because these tables can be very large (but aren't often) we set a 50mb limit to this list and stop indexing it when we hit it. For most cases this is perfectly acceptable but for the larger tables it means that whilst it may be slower, it is considerably more stable than other options.
The 'search' is a simple SWT textbox with ModifyListener that iterates over the list, performing a 'contains' on each entry. Upon a match the row and column are calculated from the index number (0=0,0, 1=0,1 ..).
It would have been nice to have a simple 'drop in' solution for this but all of the tables on offer had too many features.
Use FilteredItemsSelectionDialog dialog box
I have to generate results of searching i Vaadin.
Probably the fastest way to do it is to put results into table, but I need some custom layout and the FormLayout for every row would be the best.
Unfortunately if I put rows formatted by FormLayout into table - the generation of the results takes about 15-20 seconds.
If i put just some labels/buttons without putting it in a layout the generation takes about a second - and that's fine.
I've read the vaadin manual about optimising UI and I still don't know what to do to shorten the time needed to generate the results.
How do you generate results in Vaadin if it can't be presented as a table?
Ok, the solution is... use CssLayout inside a table. Although CssLayout still consists of 3 div's it's much faster than FormLayout.
I think that using a FlowPanel from GWT would be even better but I didn't have enough time to check it.
And don't forget about setPageLength method of a Table - the lazy loading is the main reason to use a Table :).
Offering a few more layout options:
WeeLayout add-on: fast layout, not many expensive layout calculations in JavaScript, and a lighter DOM structure than in CssLayout
DashLayout add-on: even lighter DOM structure (but more JS calculations), and offers more flexible styling options for CSS (arbitrary margin/border/padding values).
I know that GUI code in Java Swing must be put inside
SwingUtilities.invokeAndWait or SwingUtilities.invokeLater.
This way threading works fine.
Sadly, in my situation, the GUI update it that thing which takes much longer than background thread(s). More specific: I update a JTree with about just 400 entries, nesting depth is maximum 4, so should be nothing scary, right? But it takes sometimes one second! I need to ensure that the user is able to type in a JTextPane without delays. Well, guess what, the slow JTree updates do cause delays for JTextPane during input. It refreshes only as soon as the tree gets updated.
I am using Netbeans and know empirically that a Java app can update lots of information without freezing the rest of the UI.
How can it be done?
NOTE 1: All those DefaultMutableTreeNodes are prepared outside the invokeAndWait.
NOTE 2: When I replace invokeAndWait with invokeLater the tree doesn't get updated.
NOTE 3: Fond out that recursive tree expansion takes far the most time.
NOTE 4: I'm using custom tree cell renderer, will try without and report.
NOTE 4a: My tree cell renderer uses a map to cache and reuse created JTextComponents, depending on tree node (as a key).
CLUE 1: Wow! Without setting custom cell renderer it's 10 times faster. I think, I'll need few good tutorials on writing custom tree cell renderers. Sadly, I need a custom cell renderer.
This tutorial involving lazy loading might help, I'll work this through.
[There are some details missing, such as: what type of processing is happening each time the user updates the JTextPane? Is the whole tree being rebuilt?]
Anyway, what has worked for me in the past (when I experienced significant slowdown due to JTree updates) is that I rolled out my own TreeModel.
Most programmers choose to use DefaultTreeModel. This is indeed an off-the-shelf solution that works well in most cases. However, it is quite slow when significant parts of the tree needs to be updated. Obviously, Writing your own TableModel is more work than using a canned solution, but it is much less painful than you think.
In particular, my custom tree model is fast because it does not build any tree, per-se. It just observes my domain model and computes the answers to the method invoked on it (getChild(), getParent(), ...) by extracting the relevant information from that model. Try it. It works like a charm.
I do not know why it only works with invokeAndWait() (this is strange), but a simple hack would be to invoke this method in a new background thread.
I would also advise you not to populate all the tree at once. This is often a huge amount of work that just cannot be easily sped up. Instead, update only those nodes that are visible and update more nodes as the user expands them.