JTree node labels change often but width is cached - java

Currently, I'm using a custom TreeCellRenderer to handle label and icon changes for my JTree. The problem comes when I need to change the text on a few nodes. The TreeCellRenderer works great in that the new text is displayed. However, the width from the initial text is cached and is not updated. I've attempted to hack it by overriding getPreferredSize in the TreeCellRenderer but that's not working properly since I have html strings. I've even tried parsing out the tags and getting the width, but it's still not perfect. I've used SwingUtilities, and FontMetrics.
It seems that using the tree's backing DefaultTreeModel.nodeChanged(TreeNode) is the proper way to change a node. However, I would need to have an additional data structure to handle accessing the nodes that need to be changed. Personally, I would like to only change the nodes that need changing. But, that would require another data structure to find all nodes that need to be updated kinda thing. What's the best way around the size caching of the backing JLabel in this situation?

I've found that using the way you suggested of calling nodeChanged has worked best for me.

Related

How to inject images into a Word template via docx4j without getting them resized

My program injects text and pictures into a Word template. This works great via content control data binding (thanks to docx4j and Content-Control-Toolkit).
My problem is, that images get resized after injection. What I actually want, is the behavoir that Jason decribed here: http://www.docx4java.org/forums/data-binding-java-f16/picture-content-control-size-t634.html
The current behaviour is to just let it be whatever its natural size is (at a given dpi), unless that is greater than page width, in which case it is scaled down.
According to that post, the behavoir of docx4j has been changed so that the pictures always fit the size of the content control with respect to the ratio.
Is it possible to get the "old" behavoir back? Do I have to do that on my own, or is the switch, that Jason wrote about, already implemented?
As the answer to How to force Docx4j to refresh a replaced image file states, the size of a picture is stored in the main document part. At the moment, I only use XPath to set content in the custom XML part. If there is any possibility to get what I need without touching the documents XML directly, I would really prefer that. A macro to set the size after opening the document in Word is no option for me.
The first thing to be aware of is that these days we prefer to have a picture in a rich text content control, as opposed to a picture content control.
This is because Word limits your ability to "float" a picture content control.
The handling for this is triggered by w:tag containing 'od:Handler=picture': datastorage/bind.xslt#L165
The basic behaviour is that if the w:sdtContent contains an existing w:drawing/wp:inline/a:graphic then reuse it, so any formatting thus configured is used.
But for a "legacy" picture content control which doesn't contain a:blip (when would this be?), xpathInjectImage is invoked with wp:extent passed in (see bind.xslt#L240).
At line 1143, if (cxl==0 || cyl==0) // Let BPAI work out size
So if you want the image at its natural size, you could try removing the when clause at bind.xslt#L212
By the way, we can also bind escaped XHTML. But there, we make an effort to fit any image not just to the page width, but if in a table cell, to that as well.

a4j:repeat changes behaviour?

I have a strange effect with a4j:repeat when using rich:toolbar in RichFaces. In my little example I just place a couple of icons on the toolbar. If I do it manually, they are all separately placed into <td>...</td>. But if let them generate by a list using a4j:repeat, they will all be placed together within one <td>...</td>.
The result of this is that those placed manually are more far from each other in the view. Those who were generated, stick together.
I believe the difference is, that the rich:toolbarGroup thinks of the a4j:repeat as one object, and of the manually placed ones as serveral objects.
I tried out with c:forEach as well, but I get exactly the same effect.
Does anyone know how to define the a4j:repeat objects as separate? Or if it is another problem, how to solve it?
If you're only worried about the spacing you can increase it by CSS.
<a4j:repeat> is not a good thing to use with the toolbar. If the icons you put in are supposed to have some sort of functionality attached to them you'd have a problem assigning it dynamically.

Making a Scrollable UI in Libgdx

Okay So today I sat down to make a options panel that would allow you to scroll down through the list of options and for some reason I drew a blank... Anyone got an example at where I could get started for some reason I can't seem to see in my mind how to do it though It seems really simple.
You should use ScrollPane() as the container of your list. and you can use List and List.ListStyle to set your options list if you to use text for it.You should set the style of List.ListStyle and ScrollPane.ScrollPaneStyle linked to a json file and therefore your Skin. And then add a table to the stage containing the ScrollPane Actor.
here is a video tutorial: http://youtu.be/CNjkPPveqG8 talking about this.
PEACE!

Finding the node where an instance belong

I generated a decision tree based on a set of data, then I converted this data to a xml file, after that I put it into a JTree. This part works fine but now I have to take a new instance(which contains the data received from the user), find it's place in the decision tree and change that node's color. And I can't find a way to do that. I am using weka and the J48 classifier to generate the decision tree. The xml is created in the Luc Sorel style: http://www.lucsorel.com/media/downloads/sample_decision_tree.xml
The first thing I tried was to classify the instance using the algorithm but that gets me only the class where it belongs, and I don't know how to locate it in the JTree.
What should I do? Any ideeas?
Like JTable, JTree uses a flyweight renderer to draw nodes. As the default renderer is a JLabel, you can set the foreground color or make the label opaque and set the background color. This related example changes the icon for emphasis. More examples may be found here.
Addendum: I cannot find … the node that I should color.
When getTreeCellRendererComponent() is called, value is a reference to the node to be rendered, and the tree parameter allows access to the TreeModel as a whole via getModel(). This example shows how to search a tree.

JList with custom cell renderer vs Jtable

My current application uses a JList and everything is well (the only customization I did was to set the italic font on some of the rows).
Now I want to "upgrade" the user interface and instead of just labels in the List, I want a checkbox and a text field to be able to update the entry.
I started changing the code and adding a custom cell renderer and a custom cell model. My current problem is that the JPanel that the cell renderer is returning is not using the whole width of the container, so several list items are actually shown on the same line.
But now, I am wondering whether I should just change the whole thing to use JTable. I still need to add / remove items in the list though...
Any suggestion which one is better ? and if going with the JList, how should I go about fixing my current problem ?
In my experience using JTable is usually easier as it allows more complex data and functionality out-of-the-box. Usually when I try to do something the JList can't do, I just switch to JTable without a second thought. What you want sounds like something that should be pretty trivial to implement in a table. I suggest you try it out with some mock data to see if you can make it look and work the way you like (especially in case you want it to look like a list).
Try calling setLayoutOrientation(JList.VERTICAL) on your JList. That will restrict JList to a single column.

Categories