我的应用程序中有此方法,该方法正在使用辅助类RadioButton
来构建Mode
。我想针对用户选择的createModesRadios
调用.getText()
方法RadioButton
的方法。另外,我想保存用户的选择,以便在以后的使用中记住它们。是否有一种简单的方法可以调用它们并将选项设置到CheckBox
,RadioButton
和PrefixSelectionComboBox
中?
我正在使用模型类Configuration.java
来存储信息,因为我将需要使用用户输入来处理一些计算。我还要在其中存储从上述控制器中做出的选择。
我班的一部分
public class ConfigurationEditDialogController {
// General Tab
@FXML
private TextField configurationNameField;
@FXML
private TextField commentField;
@FXML
private TextField creationTimeField;
@FXML
private TextField startAddress;
@FXML
private ToggleSwitch triggerEnable;
@FXML
private ToggleSwitch softwareTrigger;
@FXML
private ToggleSwitch ctsEnable;
// Data Acquisition Tab
@FXML
private Pane dataAcquisitionTab;
@FXML
private Button okButton;
private Mode[] modes = new Mode[]{
new Mode("Vertical", 4),
new Mode("Hybrid", 2),
new Mode("Horizontal", 1)
};
private int count = Stream.of(modes).mapToInt(Mode::getCount).max().orElse(0);
private ToggleGroup group = new ToggleGroup();
private IntegerProperty elementCount = new SimpleIntegerProperty();
private ObservableMap<Integer, String> elements = FXCollections.observableHashMap();
CheckBox[] checkBoxes = new CheckBox[count];
private ObservableList<String> options = FXCollections.observableArrayList(
"ciao",
"hello",
"halo");
private Stage dialogStage;
private Configuration configuration;
private boolean okClicked = false;
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
HBox radioBox = createModesRadios(elementCount, modes);
GridPane grid = new GridPane();
VBox root = new VBox(20, radioBox);
root.setPrefSize(680, 377);
grid.setHgap(40);
grid.setVgap(30);
grid.setAlignment(Pos.CENTER);
elementCount.addListener((o, oldValue, newValue) -> {
// uncheck checkboxes, if too many are checked
updateCheckBoxes(checkBoxes, newValue.intValue(), -1);
});
for (int i = 0; i < count; i++) {
final Integer index = i;
CheckBox checkBox = new CheckBox();
checkBoxes[i] = checkBox;
PrefixSelectionComboBox<String> comboBox = new PrefixSelectionComboBox<>();
comboBox.setItems(options);
comboBox.setPromptText("Select Test Bus " + i);
comboBox.setPrefWidth(400);
comboBox.setVisibleRowCount(options.size() -1);
comboBox.valueProperty().addListener((o, oldValue, newValue) -> {
// modify value in map on value change
elements.put(index, newValue);
});
comboBox.setDisable(true);
checkBox.selectedProperty().addListener((o, oldValue, newValue) -> {
comboBox.setDisable(!newValue);
if (newValue) {
// put the current element in the map
elements.put(index, comboBox.getValue());
// uncheck checkboxes that exceed the required count keeping the current one unmodified
updateCheckBoxes(checkBoxes, elementCount.get(), index);
} else {
elements.remove(index);
}
});
grid.addRow(i, comboBox, checkBox);
}
// enable Ok button iff the number of elements is correct
okButton.disableProperty().bind(elementCount.isNotEqualTo(Bindings.size(elements)));
root.getChildren().add(grid);
dataAcquisitionTab.getChildren().add(root);
}
/**
* Create Radio Buttons with Mode class helper
*
* @param count
* @param modes
* @return
*/
private HBox createModesRadios(IntegerProperty count, Mode... modes) {
ToggleGroup group = new ToggleGroup();
HBox result = new HBox(50);
result.setPadding(new Insets(20, 0, 0, 0));
result.setAlignment(Pos.CENTER);
for (Mode mode : modes) {
RadioButton radio = new RadioButton(mode.getText());
radio.setToggleGroup(group);
radio.setUserData(mode);
result.getChildren().add(radio);
radio =
}
if (modes.length > 0) {
group.selectToggle((Toggle) result.getChildren().get(0));
count.bind(Bindings.createIntegerBinding(() -> ((Mode) group.getSelectedToggle().getUserData()).getCount(), group.selectedToggleProperty()));
} else {
count.set(0);
}
return result;
}
/**
* Method for updating CheckBoxes depending on the selected modek
*
* @param checkBoxes
* @param requiredCount
* @param unmodifiedIndex
*/
private static void updateCheckBoxes(CheckBox[] checkBoxes, int requiredCount, int unmodifiedIndex) {
if (unmodifiedIndex >= 0 && checkBoxes[unmodifiedIndex].isSelected()) {
requiredCount--;
}
int i;
for (i = 0; i < checkBoxes.length && requiredCount > 0; i++) {
if (i != unmodifiedIndex && checkBoxes[i].isSelected()) {
requiredCount--;
}
}
for (; i < checkBoxes.length; i++) {
if (i != unmodifiedIndex) {
checkBoxes[i].setSelected(false);
}
}
}
/**
* Sets the stage of this dialog.
*
* @param dialogStage
*/
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
/**
* Sets the configuration to be edited in the dialog.
*
* @param configuration
*/
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
configurationNameField.setText(configuration.getConfigurationName());
commentField.setText(configuration.getComment());
//Set the comboboxes and the checkboxes and the radiobutton
creationTimeField.setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")));
}
/**
* Returns true if the user clicked OK, false otherwise.
*
* @return
*/
public boolean isOkClicked() {
return okClicked;
}
/**
* Called when the user clicks ok.
*/
@FXML
private void handleOk() {
if (isInputValid()) {
configuration.setConfigurationName(configurationNameField.getText());
configuration.setComment(commentField.getText());
configuration.setTestBus1(elements.get(0));
configuration.setTestBus2(elements.get(1));
configuration.setTestBus3(elements.get(2));
configuration.setTestBus4(elements.get(3));
configuration.setCreationTime(DateTimeUtil.parse(creationTimeField.getText()));
// Store the information of the sample mode (vertical, hybrid or horizontal).
okClicked = true;
dialogStage.close();
}
}
模式助手类
public class Mode {
private final String text;
private final int count;
public Mode(String text, int count) {
this.text = text;
this.count = count;
}
public String getText() {
return text;
}
public int getCount() {
return count;
}
}
Configuration.java
public class Configuration {
private final StringProperty configurationName;
private final StringProperty comment;
private final StringProperty testBus1;
private final StringProperty testBus2;
private final StringProperty testBus3;
private final StringProperty testBus4;
private final StringProperty triggerSource;
private final StringProperty sampleMode;
private final StringProperty icDesign;
private final StringProperty triggerMode;
private final DoubleProperty acquisitionTime;
private final ObjectProperty<LocalDateTime> creationTime;
/**
* Default constructor.
*/
public Configuration() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param configurationName
* @param comment
*/
public Configuration(String configurationName, String comment) { //todo initialize null values
this.configurationName = new SimpleStringProperty(configurationName);
this.comment = new SimpleStringProperty(comment);
// Some initial sample data, just for testing.
this.testBus1 = new SimpleStringProperty("");
this.testBus2 = new SimpleStringProperty("");
this.testBus3 = new SimpleStringProperty("");
this.testBus4 = new SimpleStringProperty("");
this.triggerSource = new SimpleStringProperty("");
this.sampleMode = new SimpleStringProperty("");
this.icDesign = new SimpleStringProperty("Ceres");
this.triggerMode = new SimpleStringProperty("Internal");
this.acquisitionTime = new SimpleDoubleProperty(2346.45);
this.creationTime = new SimpleObjectProperty<>(LocalDateTime.of(2010, 10, 20,
15, 12));
}
/**
* Getters and Setters for the variables of the model class. '-Property' methods for the lambdas
* expressions to populate the table's columns (just a few used at the moment, but everything
* was created with scalability in mind, so more table columns can be added easily)
*/
// If more details in the table are needed just add a column in
// the ConfigurationOverview.fxml and use these methods.
// Configuration Name
public String getConfigurationName() {
return configurationName.get();
}
public void setConfigurationName(String configurationName) {
this.configurationName.set(configurationName);
}
public StringProperty configurationNameProperty() {
return configurationName;
}
// Comment
public String getComment() {
return comment.get();
}
public void setComment (String comment) {
this.comment.set(comment);
}
public StringProperty commentProperty() {
return comment;
}
// Test Bus 1
public String getTestBus1() {
return testBus1.get();
}
public void setTestBus1(String testBus1) {
this.testBus1.set(testBus1);
}
public StringProperty testBus1Property() {
return testBus1;
}
// Test Bus 2
public String getTestBus2() {
return testBus2.get();
}
public void setTestBus2(String testBus2) {
this.testBus2.set(testBus2);
}
public StringProperty testBus2Property() {
return testBus2;
}
// Test Bus 3
public String getTestBus3() {
return testBus3.get();
}
public void setTestBus3(String testBus3) {
this.testBus3.set(testBus3);
}
public StringProperty testBus3Property() {
return testBus3;
}
// Test Bus 4
public String getTestBus4() {
return testBus4.get();
}
public void setTestBus4(String testBus4) {
this.testBus4.set(testBus4);
}
public StringProperty testBus4Property() {
return testBus4;
}
// Trigger Source
public String getTriggerSource(){
return triggerSource.get();
}
public void setTriggerSource (String triggerSource){
this.triggerSource.set(triggerSource);
}
public StringProperty triggerSourceProperty() {
return triggerSource;
}
// Sample Mode
public String getSampleMode() {
return sampleMode.get();
}
public void setSampleMode (String sampleMode){
this.sampleMode.set(sampleMode);
}
public StringProperty sampleModeProperty() {return sampleMode;}
// IC Design
public String getIcDesign() {
return icDesign.get();
}
public void setIcDesign (String icDesign){
this.icDesign.set(icDesign);
}
public StringProperty icDesignProperty() {
return icDesign;
}
// Trigger Mode
public String getTriggerMode() {
return triggerMode.get();
}
public void setTriggerMode (String triggerMode){
this.triggerMode.set(triggerMode);
}
public StringProperty triggerModeProperty() {return triggerMode;}
// Acquisition Time
public double getAcquisitionTime() {
return acquisitionTime.get();
}
public void setAcquisitionTime(double acquisitionTime){
this.acquisitionTime.set(acquisitionTime);
}
public DoubleProperty acquisitionTimeProperty() {
return acquisitionTime;
}
// Last modified - Creation Time
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
public LocalDateTime getCreationTime() {
return creationTime.get();
}
public void setCreationTime(LocalDateTime creationTime){
this.creationTime.set(creationTime);
}
public ObjectProperty<LocalDateTime> creationTimeProperty() {
return creationTime;
}
}
答案 0 :(得分:1)
您可以使用java.util.Properties类轻松地向XML文件中读取/写入这样的设置。
保存到属性
Properties props = new Properties();
// Set the properties to be saved
props.setProperty("triggerMode", triggerMode.get());
props.setProperty("acquisitionTime", String.valueOf(acquisitionTime.get()));
// Write the file
try {
File configFile = new File("config.xml");
FileOutputStream out = new FileOutputStream(configFile);
props.storeToXML(out,"Configuration");
} catch (IOException e) {
e.printStackTrace();
}
这将创建config.xml
文件,并使用您使用props.setProperty()
方法设置的所有属性填充该文件。
在props.setProperty()
方法中,您有两个参数。第一个是属性的名称,第二个是实际值。该名称很重要,因为稍后将使用它来读取适当的值。
上面的代码输出以下XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Configuration</comment>
<entry key="acquisitionTime">12.0</entry>
<entry key="triggerMode">My Trigger Mode</entry>
</properties>
从属性中读取
从XML文件读取非常简单。使用loadFromXML()
方法:
FileInputStream in;
// Load the settings file
in = new FileInputStream(DataFiles.LOCAL_SETTINGS_FILE);
properties.loadFromXML(in);
从那里,您可以通过获取每个属性来设置Configuration
模型对象:
configuration.setAcquisitionTime(props.getProperty("acquisitionTime", "0.0"));
configuration.setTriggerMode(props.getProperty("triggerMode", "Manual"));
getProperty()
方法也有两个参数。首先显然是我们之前保存的属性的名称。第二个是XML文件中不存在请求的属性名称时使用的默认值。
非常简单!
我建议您更新Configuration.java
模型中的所有值并从那里保存属性(而不是尝试直接从文本字段,组合框等中保存属性)。
编辑
为了从for loop
中的控件中检索值,您需要将它们添加到可从该循环外部访问的列表中。
创建适当的列表以将控件保存为创建的版本:
List<PrefixSelectionComboBox<String>> comboBoxes = new ArrayList<PrefixSelectionComboBox<String>>();
List<CheckBox> checkBoxes = new ArrayList<>();
在循环的底部,将新控件添加到这些列表中:
comboBoxes.add(comboBox);
checkBoxes.add(checkBox);
grid.addRow(i, comboBox, checkBox);
现在,当您需要它们的值时,可以遍历这些列表以提取它们:
for (PrefixSelectionComboBox cb : comboBoxes) {
cb.getValue(); // Do with this what you must
}
for (CheckBox cb : checkBoxes) {
cb.isSelected();
}