java - 在 JavaFX 应用程序中设置和获取参数

标签 java javafx

我的应用程序中有这个方法,它使用辅助类Mode 构造RadioButton。我想为用户选择的 RadioButton 调出 createModesRadios 方法 .getText()。另外我想保存用户的选择,以便在以后的使用中记住它们。有没有一种简单的方法可以调用它们并将选项设置到 CheckBoxRadioButtonPrefixSelectionComboBox 中? 我正在使用模型类 Configuration.java 来存储信息,因为我需要处理一些用户输入的计算。我还想将上述 Controller 做出的选择存储在其中。

我类的一部分

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

配置.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;
}
}

最佳答案

您可以使用 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 循环 中从您的控件中检索值,您需要将它们添加到可从该循环外部访问的列表中。

创建适当的列表来保存创建的控件:

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

关于java - 在 JavaFX 应用程序中设置和获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51023378/

相关文章:

colors - 选中时如何更改 JavaFX TableCell 颜色?

java - 无法访问其他类中的 public static Vector<Vector> 对/数据?

java - Xpath解析: The prefix for element is not bound

Java:XML 转换、文本节点中的换行符、中断缩进

java - 限制文本字段中的文本输入

java - 使用持续时间为 1 毫秒且循环计数不定的时间线是否可以为秒表提供准确的时间?

java - 为什么我的 "Set-Cookie"响应 header 没有被翻译成实际的 cookie?

java - 如何将 File[] 的元素与对象列表中每个元素的名称进行比较

java - 矩形大小绑定(bind)到场景大小 - JavaFX

java - 如何使用切换按钮构建字符串列表