组合框中的JavaFX选择导致不同的textareas / listview

时间:2017-12-01 10:24:42

标签: java listview javafx combobox

我对javafx很新,但是想要制作这个组合框,这样当你做出选择时,文本框或列表中的文本会改变 - 例如。当在下拉列表中选择水果或饮料时,会在链接的textArea / ListView等中显示包含不同水果或饮料的列表。

主类

public class GroceryList extends Application { 
    String FruitList;
    TextArea ta1;
    ListView lv2;

    @Override
    public void start(Stage stage){

        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 750, 750);

        GridPane grid = new GridPane();
        grid.setPadding(new Insets(20,20,20,20));
        grid.setHgap(10);
        grid.setVgap(10);
        ColumnConstraints column1 = new ColumnConstraints(50);
        ColumnConstraints column2 = new ColumnConstraints(200, 200,
                Double.MAX_VALUE);
        ColumnConstraints column3 = new ColumnConstraints(200, 200,
                Double.MAX_VALUE);
        column1.setHgrow(Priority.ALWAYS);
        column3.setHgrow(Priority.ALWAYS);
        grid.getColumnConstraints().addAll(column1, column2, column3);
        //grid.setGridLinesVisible(true);

        Text scenetitle = new Text("My Shopping List");
        scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(scenetitle, 0, 0, 4, 1);


        Label op1 = new Label("1.");
        op1.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(op1, 0, 1);

        ComboBox<Department> comboBox = new ComboBox<>();
        ObservableList<Department> list = DepartmentList.getDepartmentList();
        comboBox.setItems(list);
        comboBox.setPromptText("Choose a department");
        grid.add(comboBox, 1, 1);

        /*comboBox.setOnAction((event) -> {
                ta1.setText("Fruit");
        });*/
        comboBox.setOnAction(event -> {   


            comboBox.getSelectionModel().selectedItemProperty()
            .addListener((ObservableValue observable, Object oldvalue, 
                    Object newValue) -> {
                        if (newValue == "Fruit") {
                            ta1.setText(FruitList);
                        }
                    });

            Label op2 = new Label("2.");
            op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
            grid.add(op2, 0, 2);

            ta1 = new TextArea();
            grid.add(ta1, 1, 2);

            Label op3 = new Label("3.");
            op3.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
            grid.add(op3, 2, 2);

            lv2 = new ListView();
            grid.add(lv2, 3, 2);


            root.getChildren().addAll(grid);

            stage.setTitle("MY SHOPPING LIST");
            stage.setScene(scene);
            stage.show();
        }

        public static void main(String[] args) { launch(args); }
    }
}

系类

public class Department {
    private String name;

    public Department() {
    }

    public Department(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString()  {
        return this.name;
    }
}

DepartmentList类

public class DepartmentList {

    public static ObservableList<Department> getDepartmentList() {
        Department fruit = new Department("Fruit");
        Department vegetables = new Department("Vegetables");
        Department beverages = new Department("Beverages");

        ObservableList<Department> list;
        list = FXCollections.observableArrayList(fruit, vegetables,
                beverages);

        return list;
    }
}

除此之外,我还有一个Fruit类和一个FruitList类,它们与Department和DepartmentList相同。 我基本上想要帮助了解如何在选择内容时让comboBox给我一个textArea或Listview。是OnAction,如果声明是一个组合或我如何处理它?<​​/ p>

1 个答案:

答案 0 :(得分:2)

这是一个示例应用。代码中的评论。

  

主要

xcodebuild
  

水果

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXApplication51 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        ListView<Fruit> fruitListView = new ListView(FruitList.getFruitList());//Create fruitListView and add its data
        ListView<Vegetables> vegetablesListView = new ListView(VegetableList.getVetetableList());//Create vegetablesListView and add its data

        ChoiceBox<Department> choiceBox = new ChoiceBox(DepartmentList.getDepartmentList());//Create ChoiceBox and add DepartmentList to it
        choiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) ->//This is used to observe when the ChoiceBox's value change
        {
            if (newValue != null)//
            {
                Department tempDepartment = (Department) newValue;
                switch (tempDepartment.toString())//Switch on the choosen Department value
                {
                    case "Fruit":
                        fruitListView.setVisible(true);//When Fruit is selected in the ChoiceBox show the fruitList
                        vegetablesListView.setVisible(false);
                        break;
                    case "Vegetables":
                        fruitListView.setVisible(false);
                        vegetablesListView.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
        fruitListView.setVisible(true);
        vegetablesListView.setVisible(false);
        //End Initial Setup

        StackPane stackPane = new StackPane(fruitListView, vegetablesListView);//Add both ListViews to the StackPane
        VBox vbox = new VBox(stackPane, choiceBox);//Add ListViews and ChoiceBox to VBox
        Scene scene = new Scene(vbox, 300, 250);//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);
    }
}
  

FruitList

public class Fruit
{

    private String name;

    public Fruit()
    {
    }

    public Fruit(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return this.name;
    }
}
  

DepartmentList

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class FruitList
{

    public static ObservableList<Fruit> getFruitList()
    {
        Fruit apple = new Fruit("Apple");
        Fruit orange = new Fruit("Orange");
        Fruit tomato = new Fruit("Tomato");

        ObservableList<Fruit> list;
        list = FXCollections.observableArrayList(apple, orange,
                tomato);

        return list;
    }
}

注意:其他类别是故意遗漏的。

enter image description here

enter image description here