I made a tilepane in JavaFX. And I put some imageview on tilepane.
tilepane => imageview => image
I want to get the value of image in imageview in tilepane(first row, first column).
this value
↓
| 0,0 | 0,1 | 0,2 | 0,3 | 0,4 |
| 1,0 | 1,1 | 1,2 | 1,3 | 1,4 |
I tried to
System.out.println(tilepane.getChildren().get(0))
=> result : Pane#2fc42278
I can access to tilepane(first row, first column).
But I can't get the inner value. My final goal is getting the image's name.
How can I get the value of image?
Thanks in advance!
A Node has a method setUserData into which you can put any object. You could assign the ImageView object a name via the setUserData method.
Iterating through the TitledPane would be like this:
TitledPane pane = ...
for( Node child: pane.getChildrenUnmodifiable()) {
if( child instanceof ImageView) {
ImageView imageView = (ImageView) child;
System.out.println( imageView.getUserData());
}
}
That way you check for ImageView. But of course you could as well check the userData of all nodes without checking for ImageView.
Related
I have 3 MenuButtons in my JavaFX-Application, each one filled with CheckMenuItems.
I want to get the Text of every selected Item of the MenuButton. How shall i proceed?
Take all MenuItems, filter selected and map them to String.
MenuButton mb = new MenuButton();
mb.getItems().addAll(new CheckMenuItem("One"), new CheckMenuItem("Two"), new CheckMenuItem("Three"));
mb.getItems().stream().forEach((MenuItem menuItem) -> menuItem.setOnAction(ev -> {
final List<String> selectedItems = mb.getItems().stream()
.filter(item -> CheckMenuItem.class.isInstance(item) && CheckMenuItem.class.cast(item).isSelected())
.map(MenuItem::getText)
.collect(Collectors.toList());
System.out.println(selectedItems);
}));
I have been given a dataset to work with that is layed out in the format of
Object | Type | Level | Comments | Parent | Child
So far I have been able to get the Object as the Parent Node and the Comments as a child node, however I need to get multiple children for this parent, and then children on them
An example of what I mean is like so
Object | Type | Level | Comments | Parent | Child
Dave | WH | 1 | comment | root | null
Simon | WH | 1 | comment | root | Fortnum
Simon | WH | 1 | comment | root | Mason
Tim | WH | 1 | comment | root | null
wallace| WH | 2 | comment | Simon | null
Mason | WH | 2 | comment | Simon | Mouse
Mouse | WH | 3 | comment | Mason | null
I need it to look like this
I have looked at the code from here Similar Stack Answer but its not working for me
I am pulling the sql data into a datatable and then looping through it to try and build the tree view.
This is the code that I am using that is only giving me the object as a parent node, and then the comment as a child node, but I need to be able to locate the actual children of the and then add them to the treeview.
For Each row As DataRow In dt.Rows
node = Searchnode(row.Item(4).ToString(), TreeView1)
If node IsNot Nothing Then
subNode = New TreeNode(row.Item(3).ToString())
node.ChildNodes.Add(subNode)
Else
node = New TreeNode(row.Item(0).ToString())
subNode = New TreeNode(row.Item(3).ToString())
node.ChildNodes.Add(subNode)
TreeView1.Nodes.Add(node)
End If
Next
then the function
Private Function Searchnode(ByVal nodetext As String, ByVal trv As TreeView) As TreeNode
For Each node As TreeNode In trv.Nodes
If node.Text = nodetext Then
Return node
End If
Next
End Function
Ive never really worked with treeviews before in ASP.Net but would be very grateful if anyone can help me.
Its not necessary to add child nodes when you add a new node. If the datatable is sorted the way your example shows the childnode will come up with a reference to its parent after the parent has already been added to the treeview.
so prior to this example edit the datatable and remove the duplicate objects
like remove one of the simons. the(child) column is not needed. also (optionally) going to add a reference to the parent when adding the child.
For Each row As DataRow In dt.Rows
node = Searchnode(row.Item(4).ToString(), TreeView1.Nodes)
If node IsNot Nothing Then
subNode = New TreeNode(row.Item(0).ToString()){ parent= node }
node.Nodes.Add(subNode)
Else 'root node
node = New TreeNode(row.Item(0).ToString())
TreeView1.Nodes.Add(node)
End If
Next
also searchnode needs to be recursive to check child nodes.
Private Function Searchnode(ByVal nodetext As String, ByVal tn As TreeNodeCollection) As TreeNode
TreeNode ret = nothing
For Each node As TreeNode In tn.Nodes
If node.Text = nodetext Then
ret = node
exit for
Else
ret = Searchnode(nodetext, node.Nodes)
End If
Next
Return ret
End Function
I have a Qt application that contains a QTreeWidget.
I want to replace one of the items.
I have a pointer:
QTreeWidgetItem *elementToSubstitute;
and a function that returns a QTreeWidgetItem*.
I want to overwrite the previous one with this new element, in the same place.
If I delete the previous element and insert the new one after that, the new item is placed at the bottom of the tree.
How should I proceed to have the new branch replace exactly the previous one?
EDIT for example
void MainWindow::addRoot(QString name ,QString description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->tree);
itm->setText(0,name);
itm->setText(1,description);
QTreeWidgetItem *child1 = addChild(itm,"one","hello");
QTreeWidgetItem *child2 = addChild(itm,"two","hello");
QTreeWidgetItem *child3 = addChild(itm,"tree","hello");
QTreeWidgetItem *child4 = addChild(itm,"four","hello");
QTreeWidgetItem *parent = child2->parent();
int itemIndex = parent-indexOfChid(child2);
parent->removeChild(child2);
parent->insertChild(itemIndex, child4 );
}
QTreeWidgetItem MainWindow::addChild(QTreeWidgetItem *parent,QString name ,QString description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem();
itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled );
itm->setText(0,name);
itm->setText(1,description);
addChild2(itm,"sub-one","subchild2");
addChild2(itm,"sub-two","subchild2");
addChild2(itm,"sub-tree","subchild2");
parent->addChild(itm);
return itm;
}
void MainWindow::addChild2(QTreeWidgetItem *parent,QString name ,QString description)
{
QTreeWidgetItem *itm = new QTreeWidgetItem();
itm->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);
itm->setText(0,name);
itm->setText(1,description);
itm->setText(2,"test third coloumn");
parent->addChild(itm);
}
.....
Now i have a pointer to one of the chiled. ( let's sa of level 1) ans i want to replace it with another. for instance i want to replace child "2" with child "4" but i want this to be put in the place f child 2.
You could try doing something like this:
QTreeWidgetItem *parent = elementToSubstitute->parent();
int itemIndex = parent->indexOfChid(elementToSubstitute);
parent->removeChild(elementToSubstitute);
parent->insertChild(itemIndex, yourNewTreeItem);
Note: if yourNewTreeItem is already in the tree, this will have no effect on its position, i.e. it will not duplicate a node.
If your item is top-level then you could use indexOfTopLevelItem to get it's index, then remove it from tree widget, then insert replacement item using insertTopLevelItem passing index you obtained. If you item is not top-level then you could use indexOfChild and insertChild.
Why do you need to replace the whole item? Just change the contents of the current item, or copy the new item into the current one.
//assuming you have a pointer to the new and the current Item
*currentItem = *newItem; //done
Hi my required tree structure is as follows
a -- b -- d
| |
| |
| -- e
-- c
I have my string array as follows
a/b,c
b/d,e
d/
e
where the component before / represents parent and the children of the corresponding parent are separated by ,
Can anyone provide me the logic to create a array collection for this hierarchy to set as dataprovider to my tree.
Thanks and regards
Here's the general idea. (I'm not going to type all of the nodes out) Another approach is to create an object that has label and children properties rather than creating all of this dynamically with Objects. Hope that helps.
var dp:Array = new Array();
dp[0] = new Object();
dp[0].label = "a";
dp[0].children = new Array();
dp[0].children[0] = new Object();
dp[0].children[0].label = "b";
myTree.dataProvider = dp;
So I have a TreeView and it has about 7 parent nodes that have 3-5 children each. All of these children nodes when click navigate to a URL. What I would like to do is have one parent node auto-expand based on the URL and the other parent nodes to collapse.
If that wasn't clear, here is an example:
Root
|
|--Admin
| |
| |--Add.aspx
| |--Delete.aspx
|
|
|
|--Purchases
|
|--Orders.aspx
|--Stock.aspx
Lets say the user clicked on Orders.aspx, this would navigate them to that page, and when it does, I would want the tree view to collapse all parent nodes, and expand the current parent node. So Admin would be collapsed and Purchases would be expanded.
What I've attempted so far is this:
Protected Sub resize(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.Load
For Each node As TreeNode In (CType(sender, TreeView)).Nodes
If node.NavigateUrl = GetCurrentPage() Then
For Each parentN As TreeNode In (CType(sender, TreeView)).Nodes
If Not (parentN.Parent.Selected = True And node.Parent.Text = parentN.Parent.Text) Then
parentN.Collapse()
Else : parentN.Expand()
End If
Next
End If
Next
End Sub
Public Shared Function GetCurrentPage() As String
Return System.IO.Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath).ToLower
End Function
I'm not really sure how to go about this.
Unless your tree is more complicated than you're describing, why won't this loop work?
For Each node As TreeNode In (CType(sender, TreeView)).Nodes
If node.NavigateUrl = GetCurrentPage() Then
node.Expand()
Else
node.Collapse()
End If
Next