我有一个通过Scene Builder创建的VBox,里面有一个Button,textField和一个Combobox.I将VBox放在listView中。我想点击一个添加按钮,另一个VBox出现在第一个底部。这样用户可以随意输入更多数据。
到目前为止,在我的代码中,当我点击添加按钮时,VBox会在listView中向下移动一步。我还是java的新手,所以我不确定代码中缺少什么,以便按预期工作。
我试图传递创建一个新的VBox,并在用户点击添加按钮时添加子项,但这会抛出 java.lang.IndexOutOfBoundsException:Index:4,Size:4
这是我用来尝试添加通过场景构建器创建的VBox时的代码:
private ObservableList<VBox> box = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
visibleList.setItems(box);
stack1.getChildren().add(visibleList);
}
@FXML
void addButton() {
box.add(vbox1);
}
这是我尝试创建新vbox并在运行时向其添加子代码时的代码:
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Button add;
@FXML
private TextField textfd;
@FXML
private ListView<VBox> visibleList;
@FXML
private Label defaultlabel;
@FXML
private StackPane stack1;
@FXML
private VBox vbox1;
@FXML
private HBox hbox1;
@FXML
private Button btn1;
@FXML
private TextField txt1;
@FXML
private ComboBox<String> combo1;
@FXML
private TextArea textarea1;
@FXML
private Button another;
private ObservableList<VBox> box = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
visibleList.setItems(box);
stack1.getChildren().add(visibleList);
}
@FXML
void addButton() {
VBox vb = new VBox();
vb.getChildren().addAll(btn1,txt1,combo1);
box.add(vb);
}
}
使用CustomCells程序与java.lang.reflect.InvocationTargetException崩溃: 这是我的代码:
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Button add;
@FXML
private TextField textfd;
@FXML
private ListView<VBox> visibleList;
@FXML
private Label defaultlabel;
@FXML
private StackPane stack1;
@FXML
private VBox vbox1;
@FXML
private HBox hbox1;
@FXML
private Button btn1;
@FXML
private TextField txt1;
@FXML
private ComboBox<String> combo1;
@FXML
private TextArea textarea1;
@FXML
private Button another;
private ObservableList<Car> cars = FXCollections.observableArrayList();
public void initialize(URL url, ResourceBundle rb) {
cars.addAll(new Car(CAR_TYPE.CAR1_tetbeeeee), new Car(CAR_TYPE.CAR2_iububdfcxkndns), new Car(CAR_TYPE.CAR3_nkrebsbcnevjds));
ListView<Car> carsListView = new ListView<>();
carsListView.setCellFactory(c -> new CarListCell());
carsListView.setItems(cars);
stack1.getChildren().add(carsListView);
}
private class CarListCell extends ListCell<Car> {
private HBox content = new HBox();
private VBox content2 = new VBox();
private ChoiceBox<CAR_TYPE> cb = new ChoiceBox<>();
TextField nametextbox = new TextField ();
TextArea taskinfo = new TextArea ();
Label taskid = new Label ("Task#");
Label taskstatus = new Label ("Incomplete");
DatePicker FromDatePicker = new DatePicker();
DatePicker ToDatePicker = new DatePicker();
ComboBox teamlead = new ComboBox(cars);
private Button add = new Button("+");
private Button sub = new Button("-");
public CarListCell() {
taskinfo.setPrefWidth(730.0);
teamlead.setPrefWidth(150.0);
FromDatePicker.setPrefWidth(120);
ToDatePicker.setPrefWidth(120);
taskinfo.setPrefHeight(100.0);
cb.setItems(FXCollections.observableArrayList(CAR_TYPE.values()));
cb.setMaxWidth(Double.MAX_VALUE);
taskinfo.setMaxWidth(USE_PREF_SIZE);
taskinfo.setPrefHeight(USE_PREF_SIZE);
teamlead.setMaxWidth(USE_PREF_SIZE);
FromDatePicker.setMaxWidth(USE_PREF_SIZE);
ToDatePicker.setMaxWidth(USE_PREF_SIZE);
// HBox.setHgrow(cb, Priority.ALWAYS);
content.getChildren().addAll(taskid,taskstatus,nametextbox,FromDatePicker,ToDatePicker, teamlead,sub,add);
content2.getChildren().addAll(content,taskinfo);
content.setSpacing(10);
content2.setSpacing(10);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(content2);
}
@Override
protected void updateItem(Car item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
setGraphic(content2);
//cb.setValue(item.getType());
add.setOnAction(e -> {
Car newCar = new Car(cb.getValue());
cars.add(newCar);
});
sub.setOnAction(e -> {
cars.remove(item);
});
}
}
}
private enum CAR_TYPE {
CAR1_tetbeeeee, CAR2_iububdfcxkndns, CAR3_nkrebsbcnevjds;
}
private class Car {
private CAR_TYPE type;
public Car(CAR_TYPE type) {
this.type = type;
}
public CAR_TYPE getType() {
return type;
}
public void setType(CAR_TYPE type) {
this.type = type;
}
}
}