2017-02-21 3 views
0

Ich bin ziemlich unerfahren mit javaFX und GUI im Allgemeinen, aber ich versuche, den Wert von "VehicleTypeComboBox" zu "Van" von "Car" zu ändern, wenn ich "Ford Transit 3.2L Duratorq TDCi" aus einem anderen Kombinationsfeld auswähle. Die Kombinationsfelder sind im FXML-Dokument enthalten und die Controller befinden sich in der Java-Datei.Wie ändere ich den Wert einer Combobox in JavaFX mit Code im FXML-Dokument?

Ich nehme an, es hat etwas mit der Combo-Box zu tun ist in FXML aufgefüllt und die Arbeit ist im Java-Dokument getan.

Java-Code für den Wert, ist die in Frage die letzte als SetValue nicht funktioniert:

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> 

Hauptklasse:

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

Was bedeutet "es funktioniert nicht"? Was geschieht? Können Sie ein [MCVE] erstellen (dh die Anzahl der Auswahlmöglichkeiten in der Combo und die Anzahl der Textfelder auf das absolute Minimum eingrenzen, um das Problem anzuzeigen, und den * complete * -Code (Anwendungsklasse, fxml und Controller) veröffentlichen in der Frage). –

+0

Außerdem sollten Sie eine Klasse 'Vehicle' erstellen, indem Sie eine' ComboBox 'verwenden und sie mit' Vehicle'-Objekten füllen. Auf diese Weise können Sie das lächerliche (nicht skalierbare) 'if'-'Esel'-Konstrukt im Handler loswerden. –

+0

@James_D Danke, dass du dir die Zeit genommen hast zu antworten, ich habe den ganzen Code hinzugefügt, den du angefordert hast, aber nicht das minimale, vollständige und überprüfbare, ich werde bis heute Nacht warten müssen. Wenn ich "Ford Transit 3.2L Duratorq TDCi" aus einer ComboBox mit der Bezeichnung PresetVehicleComboBox nicht auswähle, werden alle Textfelder richtig gesetzt, aber der Wert der VehicleTypeComboBox wird nicht auf "Van" gesetzt (ich habe ihn auf "Car" gesetzt) "by defualt. – Kannei

Antwort

0

Sie haben

@FXML 
private ComboBox VehicleTypeComboBox; 

in der Steuerung, aber Sie haben

<ComboBox fx:id="VehicleType" ...> 

in der FXML Datei.

+0

Vielen Dank, Ich kann nicht glauben, dass ich das nicht gesehen habe – Kannei

+0

@Kannei Wenn Sie 2 Minuten damit verbracht hätten, den Stack-Trace zu lesen, hätten Sie das wahrscheinlich getan. –

Verwandte Themen