【问题标题】:Return the choice of a combobox javafx返回组合框 javafx 的选择
【发布时间】:2015-11-26 13:38:18
【问题描述】:

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

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

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

【问题讨论】:

    标签: java javafx combobox fxml scenebuilder


    【解决方案1】:

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

    hexa.getSelectionModel().getSelectedItem()
    

    这将返回所选项目。在您的情况下,它是您在private ComboBox&lt;String&gt; 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())
      });
    

    【讨论】:

    • 感谢它对开始的帮助!但是我想用我的第二个组合框“方法”做的是,例如,用户在组合框中选择选项 2,界面应该更改并添加 2 Textfield。我不知道我是否说清楚了。
    • 我也有一些问题,因为例如我想创建一个事件但我不能使用:methode.getSelectionModel().getSelectedItem() 它告诉我方法必须是静态的,但如果它是静态的,我有 NullpointerExeption
    • 编辑了我的答案。希望它有所帮助:)
    • 我很高兴听到这个消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2019-10-20
    • 1970-01-01
    • 2014-12-25
    • 2020-04-01
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多