2017-02-21 83 views
0

我對javaFX和GUI一般不熟悉,但我試圖從另一個組合框中選擇「Ford Transit 3.2L Duratorq TDCi」時,將「VehicleTypeComboBox」的值從「Car」更改爲「Van」。組合框被填充在FXML文檔中,控制器位於Java文件中。如何用FXML文檔中的代碼更改JavaFX中Combobox的值?

我假設它與組合框有關係在FXML中填充並且工作在java文檔中完成。

Java代碼設定值時,一個問題是最後一個作爲的SetValue不起作用:

package vehiclelistgui; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.ChoiceBox; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.DatePicker; 
import javafx.scene.control.RadioButton; 
import javafx.scene.control.TableView; 
import javafx.scene.control.TextField; 

public class FXMLDocumentController implements Initializable{ 



    @FXML 
    private TableView<?> table; 

    @FXML 
    private ComboBox PresetVehicleComboBox; 


    @FXML 
    private TextField reg; 

    @FXML 
    private TextField make; 

    @FXML 
    private TextField model; 

    @FXML 
    private TextField engineSize; 

    @FXML 
    private TextField fuelType; 

    @FXML 
    private TextField colour; 

    @FXML 
    private TextField mileage; 

    @FXML 
    private TextField warrantyName; 

    @FXML 
    private TextField warrantyAddress; 

    @FXML 
    private RadioButton hasWarranty; 

    @FXML 
    private DatePicker lastServiceDate; 

    @FXML 
    private DatePicker motRD; 

    @FXML 
    private DatePicker warrantyExp; 

    @FXML 
    private ComboBox VehicleTypeComboBox; 

    @FXML 
    private Button submit; 

    @FXML 
    private Button reset; 

    @FXML 
    private Button delete; 

    @FXML 
    private Button backToMain; 

    @FXML 
    private Button confirmButton; 

    @FXML 
    private void handleChoiceBoxAction(){ 
     if(PresetVehicleComboBox.getValue().equals("Kia Sportage 2.4 Theta II")){ 
      make.setText("Kia"); 
      model.setText("Sportage 2.4 Theta II"); 
      engineSize.setText("2.4 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Ford Focus RS MK3")){ 
      make.setText("Ford"); 
      model.setText("Focus RS MK3"); 
      engineSize.setText("2.3 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Nissan Qashqui 1.6i DIG-T 4X2")){ 
      make.setText("Nissan"); 
      model.setText("Qashqui 1.6i DIG-T 4X2"); 
      engineSize.setText("1.6 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Nissan Skyline R32")){ 
      make.setText("Nissan"); 
      model.setText("Skyline R32"); 
      engineSize.setText("2.6 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Vaxhaull Corsa OPC")){ 
      make.setText("Vaxhuall"); 
      model.setText("Corsa OPC"); 
      engineSize.setText("1.6 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Nissan Juke 1.6 DIG-T (Nismo)")){ 
      make.setText("Nissan"); 
      model.setText("Juke 1.6 DIG-T (Nismo)"); 
      engineSize.setText("1.6 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Vaxhuall Astra 1.6 SIDI Turbo S/S")){ 
      make.setText("Vaxhuall"); 
      model.setText("Astra 1.6 SIDI Turbo S/S"); 
      engineSize.setText("1.6 litre"); 
      fuelType.setText("Disel"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Volkswagon Golf R32")){ 
      make.setText("Volkswagon"); 
      model.setText("Golf R32"); 
      engineSize.setText("3.6 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Toyota AE86 Trueno")){ 
      make.setText("Toyota"); 
      model.setText("AE86 Trueno"); 
      engineSize.setText("1.6 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Mercedes C Class AMG")){ 
      make.setText("Mercedes"); 
      model.setText("C Class AMG"); 
      engineSize.setText("6.0 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("BMW E92 M3 GTS")){ 
      make.setText("BMW"); 
      model.setText("E92 M3 GTS"); 
      engineSize.setText("4.4 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("BMW E82 M4")){ 
      make.setText("BMW"); 
      model.setText("E82 M4"); 
      engineSize.setText("3.0 litre"); 
      fuelType.setText("Petrol"); 
     } 
     else if(PresetVehicleComboBox.getValue().equals("Ford Transit 3.2L Duratorq TDCi")){ 
      make.setText("Ford"); 
      model.setText("Transit 3.2L Duratorq TDCi"); 
      engineSize.setText("3.2 litre"); 
      fuelType.setText("Disel"); 
      VehicleTypeComboBox.setValue("Van"); 
     } 
    } 

    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("You clicked me!"); 
     make.setText("Hello World!"); 
    } 
    @Override 
    public void initialize(URL url, ResourceBundle rb) 
    { 


    } 




} 

FXML:

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

<?import java.lang.String?> 
<?import javafx.collections.FXCollections?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.ComboBox?> 
<?import javafx.scene.control.DatePicker?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.Menu?> 
<?import javafx.scene.control.MenuBar?> 
<?import javafx.scene.control.MenuItem?> 
<?import javafx.scene.control.RadioButton?> 
<?import javafx.scene.control.ScrollPane?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 

<AnchorPane id="AnchorPane" fx:id="comfirmButton" prefHeight="546.0" prefWidth="793.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="vehiclelistgui.FXMLDocumentController"> 
    <children> 
     <MenuBar prefHeight="29.0" prefWidth="793.0"> 
     <menus> 
      <Menu mnemonicParsing="false" text="File"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
      </items> 
      </Menu> 
     </menus> 
     </MenuBar> 
     <Label layoutX="21.0" layoutY="44.0" text="Vehicle List"> 
     <font> 
      <Font size="21.0" /> 
     </font> 
     </Label> 
     <Label layoutX="595.0" layoutY="95.0" text="Preset Vehicle" /> 
     <TextField fx:id="reg" layoutX="21.0" layoutY="117.0" promptText="Registration" /> 
     <TextField fx:id="make" layoutX="21.0" layoutY="154.0" promptText="Make" /> 
     <TextField fx:id="model" layoutX="21.0" layoutY="190.0" promptText="Model" /> 
     <TextField fx:id="engineSize" layoutX="21.0" layoutY="227.0" promptText="Engine Size" /> 
     <TextField fx:id="fuelType" layoutX="202.0" layoutY="117.0" promptText="Fuel Type" /> 
     <TextField fx:id="colour" layoutX="202.0" layoutY="154.0" promptText="Colour" /> 
     <TextField fx:id="mileage" layoutX="202.0" layoutY="190.0" promptText="Mileage" /> 
     <TextField fx:id="warrantyName" layoutX="385.0" layoutY="117.0" promptText="Warranty Name" /> 
     <TextField fx:id="warrantyAddress" layoutX="385.0" layoutY="154.0" promptText="Warranty Address" /> 
     <RadioButton fx:id="hasWarranty" layoutX="419.0" layoutY="85.0" mnemonicParsing="false" text="Warranty" /> 
     <DatePicker fx:id="lastServiceDate" layoutX="385.0" layoutY="227.0" prefHeight="27.0" prefWidth="167.0" promptText="Last Service Date" /> 
     <DatePicker fx:id="motRD" layoutX="202.0" layoutY="227.0" prefHeight="27.0" prefWidth="167.0" promptText="MOT Renewal Date" /> 
     <DatePicker fx:id="warrantyExp" layoutX="385.0" layoutY="190.0" prefHeight="27.0" prefWidth="167.0" promptText="Warranty Expirery" /> 
     <ComboBox fx:id="VehicleType" layoutX="202.0" layoutY="81.0" prefHeight="27.0" prefWidth="167.0" promptText="Vehicle Type"> 
      <items> 
     <FXCollections fx:factory="observableArrayList"> 
      <String fx:value="Car" /> 
      <String fx:value="Truck" /> 
      <String fx:value="Van" /> 
     </FXCollections> 
    </items> 
    <value> 
     <String fx:value="Car" /> 
    </value> 
     </ComboBox> 
     <Button fx:id="submit" layoutX="657.0" layoutY="227.0" mnemonicParsing="false" text="Submit" /> 
     <Button fx:id="reset" layoutX="657.0" layoutY="190.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="62.0" text="Reset" /> 
     <Button fx:id="delete" layoutX="552.0" layoutY="492.0" mnemonicParsing="false" text="Delete" /> 
     <Button fx:id="backToMain" layoutX="627.0" layoutY="492.0" mnemonicParsing="false" text="Back To Menu" /> 
     <Label layoutX="245.0" layoutY="57.0" text="Vehicle Type" /> 
     <ScrollPane layoutX="37.0" layoutY="277.0" prefHeight="200.0" prefWidth="723.0" vbarPolicy="NEVER"> 
     <content> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="1310.0"> 
       <children> 
        <TableView prefHeight="200.0" prefWidth="1329.0"> 
        <columns> 
         <TableColumn prefWidth="97.0" text="Vehicle Type" /> 
         <TableColumn prefWidth="95.0" text="Registration" /> 
         <TableColumn prefWidth="75.0" text="Make" /> 
         <TableColumn prefWidth="75.0" text="Model" /> 
         <TableColumn prefWidth="88.2720947265625" text="Engine Size" /> 
         <TableColumn prefWidth="76.3780517578125" text="Fuel Type" /> 
         <TableColumn prefWidth="60.6219482421875" text="Colour" /> 
         <TableColumn prefWidth="75.0" text="Mileage" /> 
         <TableColumn prefWidth="143.6239013671875" text="MOT Renewal Date" /> 
         <TableColumn minWidth="0.0" prefWidth="6.3760986328125" text="Has Warranty" /> 
         <TableColumn prefWidth="109.3118896484375" text="Warranty Name" /> 
         <TableColumn minWidth="1.22918701171875" prefWidth="141.73388671875" text="Warranty Address" /> 
         <TableColumn minWidth="0.0" prefWidth="132.302734375" text="Warranty Expirery" /> 
         <TableColumn prefWidth="140.565673828125" text="Last Service Date" /> 
        </columns> 
        </TableView> 
       </children> 
      </AnchorPane> 
     </content> 
     </ScrollPane> 
     <ComboBox fx:id="PresetVehicleComboBox" layoutX="566.0" layoutY="116.0" onAction="#handleChoiceBoxAction" prefWidth="150.0"> 
      <items> 
     <FXCollections fx:factory="observableArrayList"> 
      <String fx:value="Custom" /> 
      <String fx:value="Ford Focus RS MK3" /> 
      <String fx:value="Kia Sportage 2.4 Theta II" /> 
      <String fx:value="Nissan Qashqui 1.6i DIG-T 4X2" /> 
      <String fx:value="Vaxhaull Corsa OPC" /> 
      <String fx:value="Nissan Juke 1.6 DIG-T (Nismo)" /> 
      <String fx:value="Vaxhuall Astra 1.6 SIDI Turbo S/S" /> 
      <String fx:value="Volkswagon Golf R32" /> 
      <String fx:value="Mercedes C Class AMG" /> 
      <String fx:value="Nissan Skyline R32" /> 
      <String fx:value="Toyota AE86 Trueno" /> 
      <String fx:value="BMW E92 M3 GTS" /> 
      <String fx:value="BMW E82 M4" /> 
      <String fx:value="Ford Transit 3.2L Duratorq TDCi" /> 
     </FXCollections> 
      </items> 
      <value> 
       <String fx:value="Custom" /> 
      </value> 
     </ComboBox> 
     <Button layoutX="721.0" layoutY="116.0" mnemonicParsing="false" text="Confirm" /> 
    </children> 
</AnchorPane> 

主類:

package vehiclelistgui; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author vincent 
*/ 
public class VehicleListGUI extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

「不起作用」是什麼意思?怎麼了?你可以創建一個[MCVE](即將組合中的選項數量和文本字段的數量縮小到顯示問題所需的絕對最小值),併發布* complete *代碼(應用程序類,fxml和控制器)在問題中)。 –

+0

另外,作爲一個旁白,你應該考慮創建一個'Vehicle'類,使用'ComboBox ',並用'Vehicle'對象填充它。這樣你就可以在處理程序中擺脫荒謬的(不可縮放的)'if' -'else'構造。 –

+0

@James_D謝謝您抽出時間回覆,我添加了您請求的所有代碼,但不是最小的,完整的和可驗證的,我必須等到今晚晚些時候。通過不工作,當我從名爲PresetVehicleComboBox的ComboBox中選擇「Ford Transit 3.2L Duratorq TDCi」時,它將正確設置所有文本字段,但它沒有將VehicleTypeComboBox的值設置爲「Van」(我將它設置爲「Car 「 – Kannei

回答

0

您有

@FXML 
private ComboBox VehicleTypeComboBox; 

控制器,但你必須

<ComboBox fx:id="VehicleType" ...> 
在FXML文件

+0

非常感謝,我不敢相信我沒有看到。 – Kannei

+0

@Kannei如果你花了2分鐘閱讀堆棧跟蹤,你可能會做。 –

相關問題