在Combox事件上动态添加TextFields和Labels

时间:2015-09-08 12:18:45

标签: java javafx combobox textfield fxml

我已经创建了一个可以正常工作的应用程序但是我不想添加一个函数,这样做我需要在更改组合框的值时添加一些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和Labels放在一起,并尝试使用此行隐藏它们:

MyTextField.setVisible(false);

但一切仍然可见。所以我开始认为,动态添加它们或者使用弹出窗口可能更容易。这就是为什么我在这里寻求帮助。

1 个答案:

答案 0 :(得分:1)

几天前我做了类似的事情,看看这个:

“类型” -Enum

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;
}
}

“指示符” 级:

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-Combobox目标方法:

@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();
}