2016-02-27 25 views
1

我正在構建工作庫存管理系統,並且在點擊該項目時讓SplitMenuButton顯示MenuItem時遇到了很多麻煩。我無法在互聯網上找到有關'SplitMenuButton'的很多信息,並且只嘗試過MenuButton,也沒有運氣。例如,默認文本是'部門',我希望當該菜單項被選中時顯示'無菌',或者當該菜單項被選擇時顯示'設施'。如何讓SimpleMenuButton在JavaFX中顯示MenuItem文本?

當#buildDataAseptic方法運行時,我試圖創建SplitMenuButton和setText(「Aseptic」)的新實例,但這仍然無效。

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

任何幫助是極大的appreacitead,謝謝!

+0

這聽起來更像是一個['ChoiceBox'](http://docs.oracle.com/javase/8/javafx /api/javafx/scene/control/ChoiceBox.html)或['ComboBox'](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ComboBox.html)。你實際上從這個控制中尋找什麼行爲? –

+0

感謝您的回覆,我希望它能夠顯示部門,然後下載選擇無菌,一般或設施。我正在根據此選擇更改部件的數據庫,但即使它更改了數據庫,它也始終顯示Department。我希望它顯示Aseptic無菌選擇,一般用於一般選擇和相同的設施。 – dubski

+0

因此用戶可以看到他們選擇了哪個部門。 – dubski

回答

0

正如其他人指出的那樣,ComboBox對於您的用例來說是一個更好的選擇。

這裏是一個獨立的例子。首先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> 

並與評論應用:

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

一般的建議:僅僅因爲你已經花了很多時間在上面,不要掛在了SplitMenuButton。將此視爲學習體驗。如果你不學會放手重構,你永遠不會創建好軟件:)

相關問題