我根本无法使用此按钮 - 我做错了什么?
该按钮仅用于将所选项目从右侧列表移动到左侧列表 - 这就是全部。
我已经在StackOverflow上查看了其他建议,但最终在添加和删除部分时出现错误。解决方案来自FXML项目 - 它在JavaFX中被称为不同的东西吗?
此外 - 是否可以让按钮与几个不同的列表视图一起使用?如果是,那怎么办?
public class GroceryList extends Application {
private ListView<String> boxSelected;
private ListView <String> boxFruit;
String fruit;
ObservableList<String> dataSelected =
FXCollections.observableArrayList();
ListView<String> listSelected = new ListView<String>();
ListView<Department> listFruit = new ListView
(FruitList.getFruitList());//Create fruitListView and add its data
ListView<Department> listVegetable = new ListView
(VegetableList.getVegetableList());
private void moveAction(ActionEvent action){
String selectedItem = boxFruit.getSelectionModel().getSelectedItem();
listFruit.remove(selectedItem);
listSelected.add(fruit);
}
@Override
public void start(Stage primaryStage) {
boxFruit.setItems((ObservableList<String>) listFruit);
boxSelected.setItems(dataSelected);
ChoiceBox<Department> choiceBox = new ChoiceBox
(DepartmentList.getDepartmentList());
choiceBox.getSelectionModel()
.selectedItemProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null) { //
Department tempDepartment = (Department) newValue;
switch (tempDepartment.toString()) { //Switch on the choosen
Department value
case "Fruit":
listFruit.setVisible(true);//When Fruit is selected
in the ChoiceBox show the fruitList
listVegetable.setVisible(false);
break;
case "Vegetables":
listFruit.setVisible(false);
listVegetable.setVisible(true);//When Vegatables is
selected in the ChoiceBox show the vegetablesList
break;
case "Beverages":
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Not Implemented Error!");
alert.setContentText("You have not implemented this
option yet! Do it now!");
alert.showAndWait();
}
}
});
//Inital Setup
choiceBox.setValue(choiceBox.getItems().get(0));//Set the ChoiceBox's
inital value to Fruit
//Since fruit is the inital value for the ChoicBox, set the
fruitListView to visisble and the others to not visible
listFruit.setVisible(true);
listVegetable.setVisible(false);
//End Initial Setup
StackPane stackPane = new StackPane(listFruit, listVegetable);//Add
both ListViews to the StackPane
VBox vbox1 = new VBox();
vbox1.setSpacing(10);
vbox1.setPadding(new Insets(20, 10, 10, 10));
Label op1 = new Label("1. choose department:");
op1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
vbox1.getChildren().addAll(op1, choiceBox, stackPane);
VBox vbox2 = new VBox();
vbox2.setSpacing(10);
vbox2.setPadding(new Insets(55, 10, 10, 10));
Label op2 = new Label("Your shopping list");
op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
vbox2.getChildren().addAll(op2, listSelected);
VBox vbox3 = new VBox();
vbox3.setSpacing(10);
vbox3.setAlignment(Pos.CENTER);
Button btn = new Button("Add");
vbox3.getChildren().add(btn);
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.TOP_CENTER);
Text sceneTitle = new Text("My Shopping List");
sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 25));
hbox.getChildren().addAll(sceneTitle);
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(15, 12, 15, 12));
bp.setTop(hbox);
bp.setLeft(vbox1);
bp.setRight(vbox2);
bp.setCenter(vbox3);
/*private void moveAction(ActionEvent action){
String selectedItem = boxFruit.getSelectionModel().getSelectedItem();
listFruit.remove(selectedItem);
listSelected.add(fruit);
}*/
Scene scene = new Scene(bp, 650, 500);//Add VBox to root
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
答案 0 :(得分:0)
我建议您不要使用StackPane一次只显示一件事。而是,切换出你想要显示的内容。然后按钮可以查看正在显示的内容,并将其扔到另一侧。试试这个:
public class GroceryList extends Application {
String fruit;
ObservableList<String> dataSelected =
FXCollections.observableArrayList();
ListView<String> listSelected = new ListView<String>();
ListView<Department> listFruit = new ListView(FruitList.getFruitList());//Create fruitListView and add its data
ListView<Department> listVegetable = new ListView(VegetableList.getVegetableList());
@Override
public void start(Stage primaryStage) {
ChoiceBox<Department> choiceBox = new ChoiceBox<Department>(DepartmentList.getDepartmentList());
//Inital Setup
choiceBox.setValue(choiceBox.getItems().get(0));//Set the ChoiceBox's inital value to Fruit
VBox vbox1 = new VBox();
vbox1.setSpacing(10);
vbox1.setPadding(new Insets(20, 10, 10, 10));
Label op1 = new Label("1. choose department:");
op1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
vbox1.getChildren().addAll(op1, choiceBox, listFruit);
VBox vbox2 = new VBox();
vbox2.setSpacing(10);
vbox2.setPadding(new Insets(55, 10, 10, 10));
Label op2 = new Label("Your shopping list");
op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
vbox2.getChildren().addAll(op2, listSelected);
VBox vbox3 = new VBox();
vbox3.setSpacing(10);
vbox3.setAlignment(Pos.CENTER);
Button btn = new Button("Add");
btn.setOnAction(e -> {
for(Department item : ((ListView<Department>)vbox1.getChildren().get(2)).getSelectionModel().getSelectedItems())
listSelected.getItems().add(item.toString());
});
vbox3.getChildren().add(btn);
HBox hbox = new HBox();
hbox.setSpacing(10);
hbox.setAlignment(Pos.TOP_CENTER);
Text sceneTitle = new Text("My Shopping List");
sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 25));
hbox.getChildren().addAll(sceneTitle);
BorderPane bp = new BorderPane();
bp.setPadding(new Insets(15, 12, 15, 12));
bp.setTop(hbox);
bp.setLeft(vbox1);
bp.setRight(vbox2);
bp.setCenter(vbox3);
choiceBox.getSelectionModel()
.selectedItemProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null) { //
Department tempDepartment = newValue;
switch (tempDepartment.toString()) { //Switch on the choosen Department value
case "Fruit":
vbox1.getChildren().set(2, listFruit);
break;
case "Vegetables":
vbox1.getChildren().set(2, listVegetable);
break;
case "Beverages":
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Not Implemented Error!");
alert.setContentText("You have not implemented this option yet! Do it now!");
alert.showAndWait();
}
}
});
Scene scene = new Scene(bp, 650, 500);//Add VBox to root
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}