JavaFX可以从TabContent(节点)访问Tab或TabPane [英] JavaFX get access to a Tab or TabPane from a TabContent(Node)
问题描述
有没有办法获得 Tab
或 TabPane
的引用,如果您参考了选项卡
中只有节点
,例如
Is there a way to get a reference to Tab
or TabPane
without no improvised way if you have reference to only the Node
inside the Tab
for eg
TabPane tabPane = new TabPane();
Tab tab = new Tab();
tab.setText("new tab");
Rectangle drect = new Rectangle(200,200, Color.LIGHTSTEELBLUE);
tab.setContent(drect);
tabPane.getTabs().addAll(tab,new Tab("tab 2"));
假设我只引用 drect
如何我可以获得标签
。
我对 Node.setUserData()
也不感兴趣 drect.getParent()。getClass()。getName()
返回
TabPaneSkin
anon内部类 TabContentRegion
:两个陌生人。
assume i only have reference to drect
how can i get tab
.
I am not interested in Node.setUserData()
also drect.getParent().getClass().getName()
returns
TabPaneSkin
anon inner class TabContentRegion
: two strangers.
我想说的是 Node.getParent()
应该返回父节点
,一般单知识( 节点在场景图中的表示可以使用getParent() )进行跟踪,但是,当涉及到标签
s全部错误,因此我的问题
All i am trying to say is Node.getParent()
should return the parent Node
, general uni-knowledge ( node's representation in the scene graph can be traced with getParent() ), but, when it comes to Tab
s its all wrong, hence my question
编辑
为什么我需要这个,假设你有 TabPane
包含太多 Tab
,在一个特定的标签
中,你有 TreeView
s作为其节点内容
从某些条件中选择,对于特定的 TreeView
,您有不同的自定义 Cell
s,因为它的 TreeItem
在UI和f方面有所不同类似于更改样式
绑定字体
&其他绑定和动画选项卡
隐藏 TabPane中的其他
,关闭其他选项卡
标签
s
Why i needed this, suppose you have TabPane
consisting of soo many Tab
, in one particular Tab
you have a TreeView
s as its Node-Content
which is selected out of some conditions, and for that particular TreeView
you have different Custom Cell
s because its TreeItem
s differ in UI and function- like changing styles
binding Fonts
& other bindables and Animating Tab
hiding other Tab
s in the TabPane
, closing other Tab
s
这种方法遵循这种方法对我来说似乎是合法的,你不做不必要的东西,比如让孩子暴露在恶劣的天气里。 :-)
with this scenario following this approach seems legit for me as, you do not do unnecessary stuff, like exposing children to bad weather. :-)
谢谢Sir James_D
Thanks Sir James_D
推荐答案
Tab
本身不是节点
,因此您无法通过以任何方式迭代场景图来获取选项卡。如果你在层次结构中走得足够远,你可以进入 TabPane
。例如:
The Tab
itself is not a Node
, so you can't get the tab just by iterating through the scene graph in any way. You can get to the TabPane
if you go up far enough in the hierarchy. For example:
private TabPane findTabPaneForNode(Node node) {
TabPane tabPane = null ;
for (Node n = node.getParent(); n != null && tabPane == null; n = n.getParent()) {
if (n instanceof TabPane) {
tabPane = (TabPane) n;
}
}
return tabPane ;
}
将返回最近的 TabPane
包含传入的节点,如果节点不在选项卡窗格中,则 null
。
will return the nearest TabPane
containing the node passed in, or null
if the node is not in a tab pane.
但是,需要这个似乎是你的设计错了。如果您在选项卡窗格中放置一个节点,则在某些时候创建一个选项卡,因此您应该只组织代码,以便可以使用该选项卡的引用。
However, needing this just seems like your design is wrong. If you put a node in a tab pane, at some point you create a tab, so you should just organize your code so you have the reference to the tab available.
这篇关于JavaFX可以从TabContent(节点)访问Tab或TabPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!