JavaFX:将TextArea绑定到所选TreeItem的元素(包含在TreeView中) [英] JavaFX: Binding TextArea to element of selected TreeItem (contained in TreeView)
问题描述
我正在构建以下JavAFX,而且我对它完全不熟悉。以下是它的样子:
I'm building the following JavAFX and I'm pretty new to it altogether. Here's what it looks like:
http:// i.imgur.com/nfzQzIY.png
正如你所看到的那样,有一个 TreeView
左边以及中心的一些 TextArea
。请忽略其余部分。
As you can see there's a TreeView
on the left as well as some TextArea
s in the centre. Please ignore the rest.
左边的字母数字字符串 TreeView
是 PlugTreeItem
类对象,我为扩展TreeItem而构建,并为 Plug
项目添加了一个额外的持有者,如下所示:
The alphanumeric Strings on the left TreeView
are PlugTreeItem
class objects, which I've built to extend TreeItem and have an additional holder for a Plug
item, like so:
public class PlugTreeItem<T> extends TreeItem{
private Plug plugItem = null;
//########################### PROCS ########################################
public PlugTreeItem(Object t, Plug pl) {
super(t);
plugItem = pl;
}
public PlugTreeItem(Object t, Node node, Plug pl) {
super(t, node);
plugItem = pl;
}
public void setPlugItem(Plug plugItem) {
this.plugItem = plugItem;
}
public Plug getPlugItem() {
return plugItem;
}
}
TreeView
是通过从SQL DB读取 Plug
来构建的,创建 PlugTreeItem
对象,将插件链接到 PlugTreeItem
然后将其添加到树的根节点(最初创建为虚拟),我们从中创建 TreeView
:
注意: result_set包含SQL结果矩阵。 MAC是包含您在屏幕截图左侧看到的地址的字段。
And the TreeView
is built by reading Plug
s off an SQL DB, creating PlugTreeItem
objects, linking the plug to the PlugTreeItem
then adding that to the tree's root node (which was created initially, as a dummy), out of which we make the TreeView
:
NOTE: result_set contains the SQL result matrix. MAC is the field that contains the addresses you see in left of the screenshot.
treeItemRoot = new PlugTreeItem<>("Active Plugs", new Plug()); //Root of the tree, contains a dummy Plug object.
selectedTreeItem = treeItemRoot; //Holder of our currently selected TreeItem, see Listener below.
TreeView<String> treeView = new TreeView<>(treeItemRoot);
while(result_set.next()){
Plug pl = null;
pl = new Plug(result_set.getString("SIHUid"), result_set.getString("sensorID"), result_set.getString("Location"), result_set.getString("Appliance"), result_set.getString("Type"), result_set.getString("connection"));
PlugTreeItem<String> pti = new PlugTreeItem(pl.getMAC(),pl);
treeItemRoot.getChildren().add(pti);
}
最后,我有一些TextAreas,正如你在截图中看到的那样应用程序。我希望那些在 TreeView
中反映所选 PlugTreeItem
的元素,让我们从Plug MAC TextArea开始我想反映你在左边看到的相同值。
Finally, I've got some TextAreas as you see in the screenshot of the app. I want those to reflect the elements of the selected PlugTreeItem
in the TreeView
, let's just start with the Plug MAC TextArea that I want to reflect the same value that you see on the left.
我已将以下监听器添加到 selectedTreeItem
这样每次用户点击 TreeItem
时, selectedTreeItem
都会更新并保留对该特定<的引用 PlugTreeItem
:
I've added the following listener to selectedTreeItem
so that every time the user clicks on a TreeItem
, the selectedTreeItem
gets updated and holds a reference to that specific PlugTreeItem
:
treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
selectedTreeItem = (PlugTreeItem<String>) newValue;
System.out.println("Selection plug MAC: " + selectedTreeItem.getPlugItem().getMAC()); //MARKER: REMOVE
// do what ever you want
}
});
希望这符合我的想法。 System.out
命令确认每当我点击树上的不同项目时,我点击的MAC地址就会被打印出来。
Hopefully this does what I think it is. The System.out
command confirms that whenever I click on different items on the tree, the MAC address that I clicked on gets printed.
我怎样才能告诉我的TextAreas嘿,听看selectedTreeItem。每当它改变时,得到一个特定值并将其设置为你的文字?
How can I tell my TextAreas to "hey, listen to selectedTreeItem. Whenever it changes, get a specific value and set it as your text"?
我已经尝试在 Plug
的特定字段上添加第二个Listener,我已经从String更改为 StringProperty
所以它变成 ObservableValue
。但是,即使将TextArea的textProperty与StringProperty绑定在一起之后,它也不会更改其内容。
I've tried adding a second Listener on a specific field of Plug
that I've changed from String to StringProperty
so that it becomes an ObservableValue
. However, even after binding the TextArea's textProperty with the StringProperty, it doesn't change its contents.
我知道我可能因为其错误而错误地提出了我的问题。尺寸。如果我有任何其他信息或代码我可能已经省略或您认为不重要,请不要犹豫。
推荐答案
我不会仅为此创建TreeItem的子类。它被设计用于任何对象。对于TextArea绑定,只需在选择更改的侦听器中绑定和取消绑定。
I would not make a subclass of TreeItem just for that. It's designed to be used with any object. For the TextArea bindings, just bind and unbind in the selection changed listener.
package treebind;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class TreeBind extends Application {
@Override
public void start(Stage primaryStage) {
TreeItem<Plug> treeItemRoot = new TreeItem<>(new Plug("root","a"));
TreeView<Plug> treeView = new TreeView<>(treeItemRoot);
for (int i=0;i<10;i++)
treeItemRoot.getChildren().add(new TreeItem<>(new Plug("name"+String.valueOf(i),"")));
treeView.setMinWidth(150);
final TextArea ta1 = new TextArea();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
if (oldValue!=null){
((TreeItem<Plug>)oldValue).getValue().s1.unbindBidirectional(ta1.textProperty());
ta1.clear();
}
if (newValue!=null){
ta1.setText(((TreeItem<Plug>)newValue).getValue().s1.getValue());
((TreeItem<Plug>)newValue).getValue().s1.bindBidirectional(ta1.textProperty());
}
}
});
HBox root = new HBox();
root.getChildren().addAll(treeView,ta1);
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
private class Plug{
public final StringProperty name, s1;
Plug(String name, String s1){
this.name = new SimpleStringProperty(name);
this.s1 = new SimpleStringProperty(s1);
}
@Override
public String toString(){
return name.getValue();
}
}
}
这篇关于JavaFX:将TextArea绑定到所选TreeItem的元素(包含在TreeView中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!