how to generate button(picture) based on checkboxes selection in javafx

前端 未结 2 601
误落风尘
误落风尘 2021-01-28 08:56

My question is how to generate buttons, set with car pictures, based on the checkboxes and/or radio buttons selected by a user in javafx?

I\'m simulating a car dealershi

2条回答
  •  无人共我
    2021-01-28 09:35

    Instead of using an ArrayList for your cars, I recommend using an ObservableList:

    ObservableList carsList = FXCollections.observableArrayList<>();
    

    An ObservableList allows you to listen for changes and respond accordingly. For example, when a new Car is added to the list, you could trigger an event that automatically adds a new Button to your scene.

    Here is a short demo application that shows how this would work. I did comment the code below as well and many of the concepts being used may be beyond your level, but it's one method, at least.

    import javafx.application.Application;
    import javafx.collections.FXCollections;
    import javafx.collections.ListChangeListener;
    import javafx.collections.ObservableList;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.FlowPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            VBox root = new VBox(5);
            root.setPadding(new Insets(10));
            root.setAlignment(Pos.CENTER);
    
            // Create an ObservableList to hold our Cars
            ObservableList carsList = FXCollections.observableArrayList();
    
            // For our sample, let's use a FlowPane to display all of our buttons. We will add new buttons to this FlowPane
            // automatically as new Cars are added to carsList
            FlowPane flowPane = new FlowPane();
            flowPane.setHgap(10);
            flowPane.setVgap(5);
            flowPane.setAlignment(Pos.TOP_CENTER);
    
            // Create a ListChangeListener for our carsList. This allows us to perform some actions whenever an item is added
            // to or removed from the list. For our example, we will only do something when a new Car is added.
            carsList.addListener(new ListChangeListener() {
                @Override
                public void onChanged(Change c) {
    
                    System.out.println(carsList.size());
                    // Get the first change
                    c.next();
    
                    // If an item was added to the list...
                    if (c.wasAdded()) {
    
                        // Create a new button and add it to the FlowPane
                        // The Change (c) provides access to a List of items that were added during this change. Since we
                        // are only going to add one Car at a time, we only need to get the first item from the AddedSubList
                        Button button = new Button(c.getAddedSubList().get(0).getName());
                        button.setGraphic(c.getAddedSubList().get(0).getIcon());
                        button.setOnAction(event -> {
                            // The code to be executed when this button is clicked goes here
                        });
    
                        // Add the button to our FlowPane
                        flowPane.getChildren().add(button);
                    }
    
                }
            });
    
            // Now we need a Button that will add a new car to the List
            Button button = new Button("Add Car");
            button.setOnAction(event -> {
                // We'll just add a random car to the carsList
                carsList.add(new Car("Car #" + (carsList.size() + 1), new ImageView("icon.png")));
            });
    
            // Add our FlowPane and Button to the root layout
            root.getChildren().addAll(button, flowPane);
    
            primaryStage.setScene(new Scene(root, 550, 250));
            primaryStage.show();
        }
    }
    
    class Car {
    
        private final String name;
        private final ImageView icon;
    
        public Car(String name, ImageView icon) {
            this.name = name;
            this.icon = icon;
        }
    
        public String getName() {
            return name;
        }
    
        public ImageView getIcon() {
            return icon;
        }
    }
    

    The Results: (after clicking the "Add Car" button a few times)

提交回复
热议问题