我对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);
}
}
“不起作用”是什么意思?怎么了?你可以创建一个[MCVE](即将组合中的选项数量和文本字段的数量缩小到显示问题所需的绝对最小值),并发布* complete *代码(应用程序类,fxml和控制器)在问题中)。 –
另外,作为一个旁白,你应该考虑创建一个'Vehicle'类,使用'ComboBox',并用'Vehicle'对象填充它。这样你就可以在处理程序中摆脱荒谬的(不可缩放的)'if' -'else'构造。 –
@James_D谢谢您抽出时间回复,我添加了您请求的所有代码,但不是最小的,完整的和可验证的,我必须等到今晚晚些时候。通过不工作,当我从名为PresetVehicleComboBox的ComboBox中选择“Ford Transit 3.2L Duratorq TDCi”时,它将正确设置所有文本字段,但它没有将VehicleTypeComboBox的值设置为“Van”(我将它设置为“Car “ – Kannei