我有一个带有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);
}
}
我需要使用这个变量来改变应用程序的工作方式。在“methode”组合框中,我想要一个包含大量文本字段的新窗口。例如,如果用户选择3,它将打开一个包含3个文本字段的新窗口,或者(如果可能,只需在组合框下方添加3个测试字段)谢谢
要在JavaFX中访问组合框的选定值,请尝试以下操作:
hexa.getSelectionModel().getSelectedItem()
这将返回所选项目。在你的例子中,它是一个字符串,正如你在你的行private组合框中声明的那样
我希望我现在明白了。在你的第二个组合框“methode”中,你想有“1”、“2”、“3”等选项吗?在那里,你可以像以前一样获得所选项目:
methode.getSelectionModel().getSelectedItem()
或者,如果你想让你的程序在点击“methode”组合框时立即打开一个新窗口,你必须添加一个
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())
});
首先,我对我的英语感到抱歉。我是一个使用VS Express 2013的C#初学者程序员。 我认为这是我的简单问题:我有一个组合框(cboMantello),里面有两个项目。然后我有一个按钮,使用所选项目的属性并将它们添加到我的角色统计中。另一个按钮删除该属性。 为了避免用户垃圾邮件,我禁用了第一个按钮,并设置了我的组合框。启用为false。这时问题来了。禁用组合框后,它会返回列表中的第一项,因此
提前谢了。
我需要关于设置组合框按钮单元格的帮助。我使用一个组合框来显示可观察列表中的数据,该列表包含两个列的表中的数据,“步骤”和“下一步”(下一步包含一个插入在步骤列中的项目);我需要做的是显示带有“步骤”列表的组合框列表单元格和相对的“下一步”按钮单元格。现在,我可以正确地看到列表单元格,但我的按钮单元格总是空的。 代码: 提前感谢。
我创建了一个 (JavaFX) 组合框,我正在填充一个由 HBoxes 制成的可观察列表,以便我可以在每个列表单元格中显示带有一些文本的图像。 这显示得很好,除了每当您选择列表中的一个项目时,它都会消失。一旦您选择了每个项目,它根本不会呈现任何项目。(您仍然可以通过单击它们之前所在的空间来选择它们。 你知道我该怎么纠正吗? 我的部分代码显示如下: 提前感谢您的帮助!
注意:我实际上不确定为什么这种情况首先需要监听器,因为我甚至不想经常监听,而是在调用方法时得到结果。
我试图创建一个,它将显示所选的预览,但是会显示字符串值。 唯一有效的方法似乎是创建