[英]JavaFX: Binding TextArea to element of selected TreeItem (contained in TreeView)
I'm building the following JavAFX and I'm pretty new to it altogether. 我正在构建以下JavAFX,并且对它来说我是一个新手。 Here's what it looks like:
看起来是这样的:
http://i.imgur.com/nfzQzIY.png http://i.imgur.com/nfzQzIY.png
As you can see there's a TreeView
on the left as well as some TextArea
s in the centre. 如您所见,左边有一个
TreeView
,中间有一些TextArea
。 Please ignore the rest. 请忽略其余部分。
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: 左侧
TreeView
上的字母数字字符串是PlugTreeItem
类对象,我将其构建为扩展TreeItem并具有一个用于Plug
项的附加支架,例如:
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;
}
}
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. 而
TreeView
是通过读取内置Plug
小号断的SQL数据库,创建PlugTreeItem
对象,插头连接到PlugTreeItem
然后补充说,树的根节点(这是最初创建的,作为一个虚拟的),其中有我们做TreeView
注意: result_set包含SQL结果矩阵。 MAC is the field that contains the addresses you see in left of the screenshot. MAC是包含您在屏幕截图左侧看到的地址的字段。
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);
}
Finally, I've got some TextAreas as you see in the screenshot of the app. 最后,您在应用程序的屏幕截图中看到了一些TextAreas。 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. 我希望它们反映出
TreeView
选定的PlugTreeItem
的元素,让我们从我想反映与您在左侧看到的相同值的Plug MAC TextArea开始。
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
: 我已将以下侦听器添加到
selectedTreeItem
以便每次用户单击TreeItem
, selectedTreeItem
都会更新并保存对该特定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
}
});
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. System.out
命令确认,每当我单击树上的不同项目时,都将打印我单击的MAC地址。
How can I tell my TextAreas to "hey, listen to selectedTreeItem. Whenever it changes, get a specific value and set it as your text"? 如何告诉我的TextAreas“嘿,听selectedTreeItem。每当更改时,获取一个特定值并将其设置为您的文本”?
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
. 我尝试在我已从String更改为
StringProperty
的Plug
的特定字段上添加第二个侦听器,以使其成为ObservableValue
。 However, even after binding the TextArea's textProperty with the StringProperty, it doesn't change its contents. 但是,即使将TextArea的textProperty与StringProperty绑定后,它也不会更改其内容。
I know I may have worded my question incorrectly due to its size. 我知道我可能由于问题的大小而措辞不正确。 Please do not hesitate to ask me for any additional info or code I may have omitted or you deem imoprtant.
请不要犹豫,询问我可能遗漏或您认为不合适的任何其他信息或代码。
I would not make a subclass of TreeItem just for that. 我不会仅仅为此做TreeItem的子类。 It's designed to be used with any object.
它旨在与任何对象一起使用。 For the TextArea bindings, just bind and unbind in the selection changed listener.
对于TextArea绑定,只需在选择更改的侦听器中进行绑定和取消绑定即可。
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.