如何从Tabpane JavaFX中的Tab获取数据 [英] How to get data from Tab in Tabpane JavaFX
本文介绍了如何从Tabpane JavaFX中的Tab获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从Tabpane JavaFX中的Tab获取数据
I want get data from Tab in Tabpane JavaFX
我在Tabpane中有2个标签,并且每个标签中都有一个TextArea,我希望Button可以从2个标签中获取数据
I have 2 Tab in Tabpane, And each Tab I have a TextArea, I want click Button will get data from 2 tab
这是我的代码:
btnThem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
i++;
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("/fxml/tab.fxml"));
Parent parent = (Parent) fxmlLoader.load();
Tab tab = new Tab("Điểm " + i);
tab.setContent(parent);
tab.setClosable(true);
tabPane.getTabs().add(tab);
controllerTab = (ControllerTab) fxmlLoader.getController();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
});
推荐答案
您的问题模棱两可,但似乎您想从tab
中的每个textArea
获取数据.为此,您应该获取nodes
tab
中并使用lookup()
进行确认,然后将node
解析为textArea
.我试图弄清楚您的scene
并举了这个例子来帮助您:
Your question is ambiguous but it seems you want to get data from each textArea
in tab
.To do this you should get nodes
(children)
from each tab
and by using lookup()
we confirm and we parse node
to textArea
.I tried to figure your scene
and made this example to help you :
public class example extends Application {
TextArea textArea = new TextArea();
TextArea textArea1 = new TextArea();
Button button = new Button("button");
@Override
public void start(Stage primaryStage) {
Tab tab1 = new Tab("tab1");
Tab tab2 = new Tab("tab2");
tab1.setContent(textArea);
tab2.setContent(textArea1);
TabPane pane = new TabPane();
pane.getTabs().addAll(tab1, tab2);
Node node1 = tab1.getContent();
Node node2 = tab2.getContent();
button.setOnAction((ActionEvent event) -> {
if (node1.lookup("TextArea") != null && node2.lookup("TextArea") != null) {
TextArea area1 = (TextArea) node1.lookup("TextArea");
TextArea area2 = (TextArea) node2.lookup("TextArea");
System.out.println(area1.getText() + " " + area2.getText());
}
});
VBox root = new VBox();
root.setAlignment(Pos.TOP_RIGHT);
root.getChildren().add(pane);
root.getChildren().add(button);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
您可以看到结果:
Hello ,tab1 Hello ,tab2
Deleting directory C:\Users\Electron\Documents\NetBeansProjects\Buttono\dist\run341573612
jfxsa-run:
BUILD SUCCESSFUL (total time: 22 seconds)
这篇关于如何从Tabpane JavaFX中的Tab获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文