Reputation: 184
I have an app with 2 ComboBox and I would like to return the choice of the user into a variable. How should I do it ? Here is my controller class :
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);
}
}
I need to use that variable to change the way the app is going to work. And on the "methode" combobox I want a new window with a number of TextField. For exemple If the user choose 3 it will open a new window with 3 textField or (if it's possible just add 3 TestField below the combobox) Thanks
Upvotes: 2
Views: 8801
Reputation: 2408
To get access to the selected value of a ComboBox in JavaFX try this:
hexa.getSelectionModel().getSelectedItem()
This returns the selected item. In your case it is an String as u declared it in your line private ComboBox<String> hexa;
I hope i understood it right now. With your second ComboBox "methode" you want to have options like "1","2","3" and so on ? There u can get the selected Item in the same way as we did it before:
methode.getSelectionModel().getSelectedItem()
Or if you want your program to immediately open a new window when a value is clicked on your "methode" ComboBox you have to add a ValueChangedListener
to listen when the value is changed and then grab the selected item with the code above and open a new window with the information of the selected item.
For further research I recommend to take a look at this site from Oracle: https://docs.oracle.com/javafx/2/ui_controls/combo-box.htm
Maybe there are some interesting additions for you.
I hope this helps you.
Edit:
Static problem
Try somtehing like this. This worked for me.
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
}
});
Methode combobox:
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())
});
Upvotes: 2