java - 返回组合框 javafx 的选择

标签 java javafx combobox fxml scenebuilder

我有一个带有 2 个 ComboBox 的应用程序,我想将用户的选择返回到一个变量中。我该怎么办呢? 这是我的 Controller 类:

package ch.makery.adress;

import java.awt.FileDialog;
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

public class HexaController implements Initializable {
       static JFrame fileDialog;
        @FXML
        private ComboBox<String> hexa;
        ObservableList<String> list = FXCollections.observableArrayList();
        @FXML
        private TextField entree;

        @FXML
        private TextField excel;

        @FXML
        private TextField sortie;


        @FXML
        private void dar(ActionEvent event){
            FileDialog fd1=new FileDialog(fileDialog,"Choisissez un fichier d'entree",FileDialog.LOAD);
            fd1.setDirectory("C:\\");
            fd1.setVisible(true);
            String filename1=fd1.getFile();
            String Directory1=fd1.getDirectory();
            String path1=Directory1 + filename1;
            entree.setText(path1);
        }

        @FXML
        private void modele(ActionEvent event){
            JFrame parentFrame=new JFrame();
             FileDialog filechooser = new FileDialog (parentFrame, "Choisir un modèle Excel à copier",FileDialog.LOAD);
             filechooser.setDirectory("C:\\");
             filechooser.setVisible(true);
             String directory_copy = filechooser.getDirectory();
             String name_copy= filechooser.getFile();
             String path_copy = (directory_copy+name_copy);
             excel.setText(path_copy);
        }

        @FXML
        private void sortie (ActionEvent event){
            JFrame parentFrame2=new JFrame();
             FileDialog filechooser2 = new FileDialog (parentFrame2, "Choisir une destination d'enregistrement",FileDialog.SAVE);
             filechooser2.setDirectory("C:\\");
             filechooser2.setVisible(true);
             String directory_save = filechooser2.getDirectory();
             String name_save= filechooser2.getFile();
             String path_save = (directory_save+name_save+".xls");
             sortie.setText(path_save);
        }
        @FXML
        private void annuler (ActionEvent event){
            System.exit(0);
        }


        @FXML
        private ComboBox<Integer>methode;
        ObservableList<Integer>nombre = FXCollections.observableArrayList();



public HexaController(){

}

public void initialize(URL url,ResourceBundle rb){

    list.add(new String("OUI"));
    list.add(new String("NON"));
    hexa.setItems(list);
    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);
}
}

我需要使用该变量来更改应用程序的工作方式。在“方法”组合框中,我想要一个带有多个文本字段的新窗口。例如,如果用户选择 3,它将打开一个带有 3 个文本字段的新窗口,或者(如果可能,只需在组合框下方添加 3 个测试字段) 谢谢

最佳答案

要访问 JavaFX 中 ComboBox 的选定值,请尝试以下操作:

hexa.getSelectionModel().getSelectedItem()

这将返回所选项目。在您的情况下,它是一个字符串,正如您在行 private ComboBox<String> hexa; 中声明的那样

我希望我现在就明白了。使用第二个 ComboBox“方法”时,您希望有“1”、“2”、“3”等选项吗?您可以像之前一样获取所选项目:

methode.getSelectionModel().getSelectedItem()

或者,如果您希望程序在单击“方法”组合框中的值时立即打开一个新窗口,则必须添加 ValueChangedListener监听值何时更改,然后使用上面的代码抓取所选项目,并打开一个包含所选项目信息的新窗口。

为了进一步研究,我建议查看 Oracle 的这个网站:https://docs.oracle.com/javafx/2/ui_controls/combo-box.htm

也许有一些对您来说有趣的补充。

希望这对您有帮助。

编辑:

静态问题

尝试这样的事情。这对我有用。

private ComboBox<String> hexa;
private Button changeBehavior;

changeBehavior.setOnAction.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            String userChoice = hexa.getSelectionModel().getSelectedItem()
            // do something with that string
            }
        });

方法组合框:

  private ComboBox<Integer>methode;

  methode.setOnAction((event) -> {
      int number = methode.getSelectionModel().getSelectedItem()
      paneYouWantToChange.getChildren().clear() // removes all displayed item

      /*Or if you want to replace somehting in your pane*/
      paneYouWantToChange.getChildren().set(indexOfItemToReplace, new TextField())

      /*Add new textfields*/
      paneYouWantToChange.getChildren().addAll(new TextField(), new TextField())
  });

关于java - 返回组合框 javafx 的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32329547/

相关文章:

java交换混合和最大数组越界

java - 如何将一张 Canvas 的内容复制到另一张 Canvas 上?

javascript - ExtJS:返回 json 存储中的总行/记录

javascript - 如何在 dojo 中获取选中的组合框

java - Android:屏幕关闭时检测长按键

java - AWS 在 Java 中获取实例名称

java - 如何将 List<Mono<T>> 转换为 Mono<List<T>>?

java - 如何在单击按钮后打开新的 fxml View - JavaFX

javafx - 使用java代码获取FXML文件节点

c# - 自定义组合框样式