CodeNameOne - React on Tree expansion or collapse - java

I'm using a subclass of the Tree class in my project. I'm having two problems. First I would like to change the text of the not-leaf nodes when they expand or collapse. Second when expanding some nodes the tree exceeds the screen and even if scrollable is set to true, I can not scroll all the way to the bottom. To be specific the tree always shows as many nodes as in the beginning. If it begins with 10 nodes, then after expanding I will only be able to scroll the ten top nodes and not the whole tree.
While trying to figure out both points I looked for a callback on expansion/collapse, but it seems to be private. Is there any way to add a listener on expansion/collapse or other way to solve my issues?

Tree should be placed in a non-scrollable hierarchy where it can take responsibility over the scrolling e.g. in the center of a border layout on the Form.
You can override the createNode method of Tree and build any type of Component you want. It can change it's text when it's expanded based on the events and do anything you want.

Related

Add components dynamically in JavaFX

Updated: question is incorrect because of low knowledge of the subject. Sorry.
I'm trying to create a small application that shows a graph that contains nodes and connections between them. Both nodes and connections are complex, I mean they can have another components in it, like labels.
I have a big Pane which plays a canvas role. I'm going to add and remove elements from it. The problem is that I want to add or remove graph elements dynamically, using buttons or context menu. Sort of Paint for Graphs :) And I have no idea how to implement it.
Especially I desperately need help in dynamical adding/removing mechanism. I would be very grateful for your help!
Just get the child list of the pane you want to add stuff to, and add the stuff when the appropriate action occurs.
FlowPane pane = new FlowPane();
Button addNode = new Button("Add");
addNode.setOnAction(e -> pane.getChildren().add(new Circle(10));
Notes:
If you want to use a Pane rather than a FlowPane, then Pane has no internal layout, so you need to also set the layoutX and layoutY properties appropriately when you add to the Pane.
If you want to change the rendering order of nodes in the Pane (e.g. which nodes are rendered on the bottom and which on top), then you do that by adding new nodes at an appropriate position in the child list; for example, pane.getChildren().add(0, new Circle(10)), will add a circle that is rendered underneath all other children of the pane rather than on top.

Creating a fully dynamic GUI

I'm currently trying to learn JavaFX and FXML (and Java) and decided to write a textbased RPG. The basis for this was already written quite some time ago, but now I wanted to do the whole thing better. Including the visuals, that is, the GUI.
First of all: I'd like to do this using FXML. That does not mean however that I'm not interested in seeing a way using basic Java.
What I want to build is a fully dynamic GUI. No Matter how it is resized, the components (and ideally the text as well) would be at the same location, relative to the other components / window border.
The window would have some kind of top line with several buttons for saving, the menu, overview and whatnot. Below that, on the left side, would be Character information: Health, Experience, Money etc. On the right side would be the text output (using a Scrollpane) plus a text field, for user input. Below the text input/output I'd place the buttons used for actions and decisions. Bottom left corner does not contain anything, though it should be a separate area.
At first I tried using Splitpanes, not knowing that they can be resized anytime and have visible Dividers. Now I'm not sure what to do.
A Gridpane would give some of the functionality I need (separate the areas), but also does not give the flexibility I want (unless I just don't know how to do it). I couldn't get it to work. So I tried using simple Panels. But with them I couldn't figure out how to keep the panels keep their relative position and size, and how to make the Buttons stick to the borders.
So what would be the best way to go about this? GridPane? Panel? Something else I'm missing? Since I don't really know how to achieve this, any help in any direction would be highly appreciated.
Have you read the layout tutorial?
From your description, it sounds like a BorderPane might be best for the overall layout (i.e. the root of your scene graph): I'm not quite sure if you could easily make this give you the empty bottom left corner you want. Alternatively you could use a GridPane as you suggested, with appropriate ColumnConstraints and RowConstraints applied to size the cells in the pane.

How to add an image while we drag n drop tree item?

I have a tree with number of childs and i want to drag and drop these child items on a vertical panel. Right now its working fine.The only thing i want to know is can we add an image in the place of small icon which comes at the time of dragNdrop.
You need to override the newDragProxy() method of the PickupDragController in order to provide your own proxy widget (say, an image) when the drag starts.
Do note that you also need to use setBehaviorDragProxy(true) to allow dragging proxies instead of the original widgets (i.e., the original widgets will stay in place, and you simply drag a proxy of it, that you can style as you wish).

Assinging different icons to different nodes in a JTree

Is it possible to assign different icons to different nodes in a JTree using DefaultTreeCellRenderer.setOpenIcon()? Thanks.
The same cell renderer instance is used to render all the cells of the tree. The open icon is the little + symbol, or triangle symbol at the left of every tree node which allows to expand it (i.e. see its child nodes). I doubt this is the icon you want to change. It would be rather strange not to use the same one for all the nodes.
If you want to display a custom icon for a specific node, create a subclass of DefaultTreeCellRenderer, override the getTreeCellRendererComponent method, decide which icon to display based on the value passed to the method, and call setIcon.
See http://download.oracle.com/javase/tutorial/uiswing/components/tree.html#display for a similar example (which customized the tooltip, and not the icon, but the idea is the same).

JTree with left and right-aligned text in cells?

What I'm trying to do is create a JTree with each cell containing normal text but also containing a right-aligned text for each cell (right-aligned to the edge of the tree, regardless of the hierarchy level).
I've tried creating my own TreeCellRenderer, but the cells' sizes are not being updated. I also tried this idea with a custom tree UI but am experiencing similar issues.
I also have tried creating a custom component with a JPanel "glued" to the right of the tree. This has been the most successful, but I have been unable to have the tree cells extend to the right and touch this extra panel:
alt text http://img718.imageshack.us/img718/3676/problem.png
You could try to use a JXTreeTable, from the SwingX package.
jxtreetable example http://blogs.sun.com/geertjan/resource/outline-tim-browser.png
You would define then such an object with two columns (one for the tree, to other for the right aligned text), and it should work out fine.
To prevent it from looking like a table, though, I would recommend you to deactivate the header (setTableHeader(null)), and use their "packing" methods, to have the columns on optimal size.

Categories