简体   繁体   English

如何获取SimpleMenuButton以在JavaFX中显示MenuItem文本?

[英]How do you get the SimpleMenuButton to display the MenuItem text in JavaFX?

I am building an inventory management system for work and am having a lot of trouble getting the SplitMenuButton to display the MenuItem when I click on the item. 我正在构建用于工作的库存管理系统,并且在单击该项时,让SplitMenuButton显示MenuItem遇到很多麻烦。 I can not find much information on the 'SplitMenuButton' on the internet and have tried just MenuButton as well with no luck. 我在Internet上的“ SplitMenuButton”上找不到太多信息,并且也尝试过MenuButton,但是没有运气。 For example, the default text is 'Department' and I would like it to display 'Aseptic' when that menu item is selected or 'Facilities' when that menu item is selected. 例如,默认文本是“部门”,并且我希望在选择该菜单项时显示“无菌”,或者在选择该菜单项时显示“设施”。

I have tried to create a new instance of SplitMenuButton and setText("Aseptic") when the #buildDataAseptic method is run, but this still didn't work. 我尝试在#buildDataAseptic方法运行时创建SplitMenuButton和setText(“ Aseptic”)的新实例,但这仍然无法正常工作。

My fxml code is: 我的fxml代码是:

<SplitMenuButton mnemonicParsing="false" prefHeight="25.0" prefWidth="217.0" text="Department" textAlignment="CENTER">
                <items>
                  <MenuItem fx:id="asepticMenuItem" mnemonicParsing="false" onAction="#buildDataAseptic" text="Aseptic" />
                  <MenuItem fx:id="generalMenuItem" mnemonicParsing="false" onAction="#buildDataGeneral" text="General" />
                    <MenuItem fx:id="facilitiesMenuItem" mnemonicParsing="false" onAction="#buildDataFacilities" text="Facilities" />
                </items>
              </SplitMenuButton>

any help is greatly appreacitead, thanks! 非常感谢您的任何帮助,谢谢!

As others have pointed out, ComboBox is a much better choice for your use case. 正如其他人指出的那样,对于您的用例,ComboBox是更好的选择。

Here is a self contained example. 这是一个自包含的示例。 First the FXML: 首先是FXML:

<?import javafx.scene.control.ComboBox?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>
<ComboBox xmlns:fx="http://javafx.com/javafx/null">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="Aseptic" />
            <String fx:value="General" />
            <String fx:value="Facilities" />
        </FXCollections>
    </items>
</ComboBox>

And the application with comments: 并在应用程序中加上注释:

package sample;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;

public class Main extends Application {

    public void start(Stage primaryStage) throws Exception {
        ComboBox<String> comboBox = FXMLLoader.load(getClass().getResource("test.fxml"));

        // Create an observable property for the selection
        SimpleStringProperty selected = new SimpleStringProperty();

        // Bind the property to the comboBox
        selected.bindBidirectional(comboBox.valueProperty());

        // Set initial value
        selected.set("Facilities");

        // React to changes
        selected.addListener((observable, oldValue, newValue) -> {
            // newValue contains the new selection, update database
            System.out.println(newValue);
        });

        primaryStage.setScene(new Scene(comboBox));
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

General advice: Don't hang on to the SplitMenuButton just because you've spent a lot of time on it. 一般建议:不要仅仅因为您花了很多时间就挂在SplitMenuButton上。 Think of this as a learning experience. 将此视为学习经验。 If you don't learn to let go and refactor, you'll never create good software :) 如果您不学会放手和重构,就永远不会创建优秀的软件:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM