【问题标题】:Dynamically add TextFields and Labels on a Combox Event在组合框事件上动态添加文本字段和标签
【发布时间】:2015-09-08 12:18:45
【问题描述】:

我已经制作了一个运行良好的应用程序,但我不想添加功能,为此我需要在更改组合框的值时添加一些 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 个文本字段和标签,并尝试用这一行隐藏它们:

MyTextField.setVisible(false);

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

【问题讨论】:

  • 是的,但是当我不放任何代码时,每个人都在要求它,而当我放我的代码时,没有人想要它!我不知道我可以从该代码中删除什么,因为我需要让自己清楚我的代码结构。它还展示了它是如何制作的,并且可能可以帮助我添加我想要的内容并最终改进它。但是,如果您在这里看到任何无用的内容,请告诉我,我会更改它!
  • 这里就完成了。拿走我所有的代码,你就可以像我一样执行它!我怎么能向你证明我不知道该怎么做呢?因为这实际上是我的问题!我没有任何异常或错误。我只是不知道。所有这些都与问题有关,因为当我更改组合框上的选择时,我需要添加 Textfield 和 Label !只是觉得它是最小的(即使它很多我不知道如何减少它或者它不会是可执行的),完整的(不知道你还需要什么),可验证的(你可以看到什么都没有发生时更改我的组合框的值
  • 那个新的呢?
  • 我再次编辑主题

标签: java javafx combobox textfield fxml


【解决方案1】:

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

“类型”-枚举

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

Combobox目标的onAction-Method:

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

【讨论】:

  • 您能否详细解释一下您的“Type-Enum”和“Indicator”类。我真的不明白它是什么。
  • Indicator-class 定义了文本字段的标签(将显示在文本字段上方)。在 Type-enum 中,您定义了 Combobox 的所有可能值。在构造函数中,您定义将显示多少个文本字段以及所选值(例如 boObject 有三个文本字段)
  • 所以 boObjekt, job, table,tableColumn 是您的 ComboBox 中可用的不同值吗?
  • 是的,你是对的。用“target.getItems().addAll(Arrays.asList(Type.values()));”您可以将所有定义的值添加到 Combobox ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多