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

问题描述:

我刚刚开始javaFX,并且我做了一个ButtonMenu,只有几个选项。我想要得到用户从菜单中选择的内容。如何从菜单按钮javaFX中获取所选项目?

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

控制器类>

@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>

我曾尝试去甲骨文,但没有找到任何信息。任何帮助表示赞赏。

+0

我跑你的代码。在“格式”下拉菜单中没有可供选择的选项。 – Sedrick

+0

@SedrickJefferson如何在菜单中添加选择? – thvardhan

+0

你应该搜索非常简单的例子,只解决你面临的问题。 – Sedrick


我可以:

甲菜单按钮控制看起来像一个按钮,并且表现得像一个菜单。 当它被激活时(通过点击或其他方式),它以弹出菜单的形式显示 选项列表。菜单 中的选项列表保留在由getItems()方法返回的参考为 的ObservableList中。 要在选择菜单 选项时执行命令,您需要将ActionEvent处理程序添加到 MenuItems

所以,如果你正在使用RadioMenuItemCheckMenuItem与群体还没有建立在其菜单项选择的检测机构...

除此之外,它是其他story..In这种情况下,你可以得到从ToggleGroup中选择的项目用于对这些按钮进行分组。


解决方案:

对于每个MenuItem您已经添加到MenuButtonObservableList添加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); 
} 

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

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

你可以玩这个,直到你得到你想要的结果。