如何从菜单按钮javaFX获取所选项目?

问题描述 投票:0回答:2

我刚刚启动了javaFX,并且制作了一个带有几个选项的ButtonMenu。我想获取用户从菜单中选择的内容。

这是我到目前为止的代码->

控制器类>

@FXML
private JFXTextField locF;
private File file;
private Set<String> extensions;
@FXML
private MenuButton format;


public void openFileBrowser(ActionEvent event){


    DirectoryChooser chooser = new DirectoryChooser();
    chooser.setTitle("JavaFX Projects");
    chooser.setInitialDirectory(new File("c:/"));
    File selectedDirectory = chooser.showDialog(Main.stage);
    locF.setText(selectedDirectory+"");
    extensions=getFileTypes(selectedDirectory.listFiles());//just gets all different types format present in the folder and later add it to menu
    Iterator<String> it=extensions.iterator();
    while (it.hasNext()){
     format.getItems().addAll(new MenuItem(it.next()));
    }
}

private Set<String> getFileTypes(File[] list){
    Set<String> ext=new HashSet<>();
    for (File i:
         list) {

        if (i.isFile()){


                ext.add(i.getName().substring(i.getName().lastIndexOf(".")));



        }

    }

    return ext;
}

}

FXML ->

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuButton?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
      <GridPane BorderPane.alignment="CENTER">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="57.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="93.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="564.0" minWidth="10.0" prefWidth="311.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="575.0" minWidth="0.0" prefWidth="77.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="575.0" minWidth="10.0" prefWidth="69.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <JFXTextField fx:id="locF" focusColor="#47c64d" unFocusColor="#821dda" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <JFXButton fx:id="browse" mnemonicParsing="false" onAction="#openFileBrowser" prefHeight="78.0" prefWidth="123.0" ripplerFill="#2de823" text="Browse" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Label fx:id="locL" prefHeight="48.0" prefWidth="143.0" text="Enter Location" GridPane.columnIndex="1" GridPane.rowIndex="1" />
         </children>
      </GridPane>
   </top>
   <right>
      <GridPane BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="95.0" minWidth="10.0" prefWidth="61.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="139.0" minWidth="10.0" prefWidth="139.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints maxHeight="98.0" minHeight="0.0" prefHeight="17.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="227.0" minHeight="10.0" prefHeight="227.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <MenuButton fx:id="format" mnemonicParsing="false" prefHeight="29.0" prefWidth="109.0" text="Format" GridPane.columnIndex="1">

            </MenuButton>
         </children>
      </GridPane>
   </right>
</BorderPane>

我尝试去甲骨文但没有找到任何信息。如有任何帮助,我们将不胜感激。

java button javafx menu
2个回答
4
投票

MenuButton 控件看起来像一个按钮,并且行为像一个菜单。 当它被激活时(通过单击或其他方式),它会显示一个列表 弹出菜单形式的选项。菜单中的选项列表 维护在一个 ObservableList 中,其引用是 由 getItems() 方法返回。 当菜单出现时执行命令 选择选项后,您需要将 ActionEvent 处理程序添加到 菜单项.

所以它没有内置机制来检测选择了哪个菜单项...

除非您将

 RadioMenuItem
CheckMenuItem
与组一起使用,那就是另一回事了。在这种情况下,您可以从用于对这些按钮进行分组的
ToggleGroup
中获取所选项目。


解决方案:

对于您添加到

MenuItem
MenuButton
中的每个
ObservableList
,添加一个
actionListener

 while (it.hasNext()){
     MenuItem item = new MenuItem(it.next());
     item.setOnAction(a->{ //lambda expression
        //..code logic here for each extension
     });
     format.getItems().add(item);
 }

1
投票

我通过谷歌搜索找到了这个解决方案。

Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root);
    stage.setScene(scene);


    ChoiceBox<String> cb = new ChoiceBox(FXCollections.observableArrayList("item1", "item2", "item3"));
    cb.getSelectionModel().selectedIndexProperty().addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
        String selectedItem = cb.getValue();
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Example");
        alert.setContentText("You clicked " + cb.getItems().get((Integer)newValue));
        alert.showAndWait();
    });


    root.getChildren().add(cb);
    stage.show();
}

您可以尝试一下,直到获得想要的结果。

© www.soinside.com 2019 - 2024. All rights reserved.