在组合框事件上动态添加文本字段和标签
Dynamically add TextFields and Labels on a Combox Event
我制作了一个运行良好的应用程序,但我不想添加功能,因此我需要在更改组合框的值时添加一些 TextFields 和 Label。
这是我的主要代码:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Parent;
public class maincombo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("/combobox.fxml"));
Scene scene = new Scene(parent);
stage.setTitle("Application");
stage.setScene(scene);
stage.show();
}
}
我的控制器:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
public class comboboxcontroller implements Initializable {
@FXML
private ComboBox<Integer>methode;
ObservableList<Integer>nombre = FXCollections.observableArrayList();
@FXML
private void metho (ActionEvent event){
switch (methode.getValue()) {
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
default :
break;
}
}
public comboboxcontroller (){
}
public void initialize(URL url,ResourceBundle rb){
nombre.add(new Integer(0));
nombre.add(new Integer(1));
nombre.add(new Integer(2));
nombre.add(new Integer(3));
nombre.add(new Integer(4));
nombre.add(new Integer(5));
methode.setItems(nombre);
}
}
我的 fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="150.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="comboboxcontroller">
<children>
<GridPane layoutX="-180.0" layoutY="-205.0" prefHeight="0.0" prefWidth="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ComboBox fx:id="methode" prefWidth="150.0" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="5.0" right="5.0" />
</GridPane.margin>
</ComboBox>
<Label text="MyComboBox" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
</children>
</GridPane>
</children>
</AnchorPane>
我尝试用 switch 语句做一些事情,但没有成功。我想要的是当用户选择值 4 时,我希望它添加 4 个标签和文本字段,但如果他改变主意并选择值 2,它应该显示 2 个标签和文本字段。
编辑: 我试着把我所有的 5 个 TextFields 和标签放在一起,并尝试用这一行隐藏它们:
MyTextField.setVisible(false);
但一切仍然可见。所以我开始考虑动态添加它们或者使用弹出窗口 window 可能更容易。这就是为什么我在这里寻求帮助。
前几天我做了类似的事情,看看这个:
"Type"-枚举
public enum Type {
boObject("BO-Objekt", new Indicator("Test1"), new Indicator("Test2"), new Indicator("Test3")),
job("Job", new Indicator("Test1"), new Indicator("Test2")),
table("Tabelle", new Indicator("Test1")),
tableColumn("Tabellenspalte", new Indicator("Test1"), new Indicator("Test2"));
private String displayName;
private Indicator[] indics;
private Type(String displayName, Indicator ... indics) {
this.displayName = displayName;
this.indics = indics;
}
@Override public String toString() {
return displayName;
}
public Indicator[] getIndics() {
return indics;
}
public void setIndics(Indicator[] indics) {
this.indics = indics;
}
}
"Indicator"-class:
public class Indicator {
private String label;
public Indicator(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
组合框目标的onAction-方法:
@FXML private ComboBox<Type> target;
@FXML private VBox targetBox;
@FXML public void onTargetSelected() {
targetBox.getChildren().clear();
for(Indicator indic : target.getValue().getIndics()) {
VBox box = new VBox();
Text t = new Text(indic.getLabel());
box.getChildren().add(t);
TextField txt = new TextField();
txt.setMaxWidth(target.getWidth());
box.getChildren().add(txt);
targetBox.getChildren().add(box);
}
Controller.stage.close();
Controller.stage.show();
}
我制作了一个运行良好的应用程序,但我不想添加功能,因此我需要在更改组合框的值时添加一些 TextFields 和 Label。 这是我的主要代码:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Parent;
public class maincombo extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("/combobox.fxml"));
Scene scene = new Scene(parent);
stage.setTitle("Application");
stage.setScene(scene);
stage.show();
}
}
我的控制器:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
public class comboboxcontroller implements Initializable {
@FXML
private ComboBox<Integer>methode;
ObservableList<Integer>nombre = FXCollections.observableArrayList();
@FXML
private void metho (ActionEvent event){
switch (methode.getValue()) {
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
default :
break;
}
}
public comboboxcontroller (){
}
public void initialize(URL url,ResourceBundle rb){
nombre.add(new Integer(0));
nombre.add(new Integer(1));
nombre.add(new Integer(2));
nombre.add(new Integer(3));
nombre.add(new Integer(4));
nombre.add(new Integer(5));
methode.setItems(nombre);
}
}
我的 fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="150.0" prefWidth="150.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="comboboxcontroller">
<children>
<GridPane layoutX="-180.0" layoutY="-205.0" prefHeight="0.0" prefWidth="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ComboBox fx:id="methode" prefWidth="150.0" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="5.0" right="5.0" />
</GridPane.margin>
</ComboBox>
<Label text="MyComboBox" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
</children>
</GridPane>
</children>
</AnchorPane>
我尝试用 switch 语句做一些事情,但没有成功。我想要的是当用户选择值 4 时,我希望它添加 4 个标签和文本字段,但如果他改变主意并选择值 2,它应该显示 2 个标签和文本字段。
编辑: 我试着把我所有的 5 个 TextFields 和标签放在一起,并尝试用这一行隐藏它们:
MyTextField.setVisible(false);
但一切仍然可见。所以我开始考虑动态添加它们或者使用弹出窗口 window 可能更容易。这就是为什么我在这里寻求帮助。
前几天我做了类似的事情,看看这个:
"Type"-枚举
public enum Type {
boObject("BO-Objekt", new Indicator("Test1"), new Indicator("Test2"), new Indicator("Test3")),
job("Job", new Indicator("Test1"), new Indicator("Test2")),
table("Tabelle", new Indicator("Test1")),
tableColumn("Tabellenspalte", new Indicator("Test1"), new Indicator("Test2"));
private String displayName;
private Indicator[] indics;
private Type(String displayName, Indicator ... indics) {
this.displayName = displayName;
this.indics = indics;
}
@Override public String toString() {
return displayName;
}
public Indicator[] getIndics() {
return indics;
}
public void setIndics(Indicator[] indics) {
this.indics = indics;
}
}
"Indicator"-class:
public class Indicator {
private String label;
public Indicator(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
组合框目标的onAction-方法:
@FXML private ComboBox<Type> target;
@FXML private VBox targetBox;
@FXML public void onTargetSelected() {
targetBox.getChildren().clear();
for(Indicator indic : target.getValue().getIndics()) {
VBox box = new VBox();
Text t = new Text(indic.getLabel());
box.getChildren().add(t);
TextField txt = new TextField();
txt.setMaxWidth(target.getWidth());
box.getChildren().add(txt);
targetBox.getChildren().add(box);
}
Controller.stage.close();
Controller.stage.show();
}