如何在javafx中基于复选框选择生成按钮(图片)
我的问题是,如何根据用户在javafx中选择的复选框和/或单选按钮生成带有汽车图片的按钮 我正在用汽车图片模拟一个汽车经销商网站。用户应能够通过单击复选框和/或单选按钮来过滤显示的图片 我首先创建所有图片按钮,每个循环都有一个。我可以使用if和if/else语句来过滤图片,但是会有重复的。我听说过observablelist,但我还没学会 有人能帮我解决这个问题吗?谢谢大家!如何在javafx中基于复选框选择生成按钮(图片),java,javafx,Java,Javafx,我的问题是,如何根据用户在javafx中选择的复选框和/或单选按钮生成带有汽车图片的按钮 我正在用汽车图片模拟一个汽车经销商网站。用户应能够通过单击复选框和/或单选按钮来过滤显示的图片 我首先创建所有图片按钮,每个循环都有一个。我可以使用if和if/else语句来过滤图片,但是会有重复的。我听说过observablelist,但我还没学会 有人能帮我解决这个问题吗?谢谢大家! ArrayList<Car> cars; for (Car r : cars) { for (in
ArrayList<Car> cars;
for (Car r : cars)
{
for (int i = 0; i < SIZE; i++)
{
// create buttons and set car pictures
carButton[i] = new Button();
carButton[i].setId(String.format("%d", i));
carButton[i].setGraphic(cars.get(i).getCarPicture());
}
}
arraylistcars;
用于(汽车r:汽车)
{
对于(int i=0;i
对于您的汽车,我建议使用可观察列表,而不是阵列列表
:
ObservableList<Car> carsList = FXCollections.observableArrayList<>();
结果:(点击“添加汽车”按钮几次后)
我建议您不要为您的汽车使用阵列列表
,而是使用可观察列表
:
ObservableList<Car> carsList = FXCollections.observableArrayList<>();
结果:(点击“添加汽车”按钮几次后)
这是一个糟糕的实现,但它将为您提供一些关于如何操作的想法。您需要研究FilteredList
、ListView
和谓词
。此实现一次不会处理多个复选框。它将只显示最后一个复选框
操作
卡利斯特
汽车
这是一个糟糕的实现,但它将为您提供一些关于如何操作的想法。您需要研究FilteredList
、ListView
和谓词
。此实现一次不会处理多个复选框。它将只显示最后一个复选框
操作
卡利斯特
汽车
查看ListView
@Zephyr。这是做作业用的,但我可以用观察列表。我理解他们的理论,所以我可以解释他们。我只是不熟悉它们的编码。看看ListView
@Zephyr。这是做作业用的,但我可以用观察列表。我理解他们的理论,所以我可以解释他们。我只是不熟悉它们的编码。谢谢你,西风。我认为我的问题是我如何设置我的初级阶段和我的经验不足。很难用语言来解释问题是什么。。。我相信有人能用你的技术在2秒钟内修好,但我只是个初学者。我知道这个项目我会得A,但更多的是个人的满足感。。。再次感谢您的帮助,我以后一定会使用可观察列表。谢谢西风。我认为我的问题是我如何设置我的初级阶段和我的经验不足。很难用语言来解释问题是什么。。。我相信有人能用你的技术在2秒钟内修好,但我只是个初学者。我知道这个项目我会得A,但更多的是个人的满足感。。。再次感谢您的帮助,我以后一定会使用可观察列表。谢谢塞德里克。这就是我在开始项目之前需要知道的。。。。我正在做的是设置为可见(false),因此与所选标准不匹配的按钮/分幅不可见。虽然不好看,但还是挺管用的。谢谢塞德里克。这就是我在开始项目之前需要知道的。。。。我正在做的是设置为可见(false),因此与所选标准不匹配的按钮/分幅不可见。虽然不漂亮,但它能起作用。
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author Sedrick
*/
public class CarList extends Application {
@Override
public void start(Stage primaryStage) {
List<Car> cars = new ArrayList();
cars.add(new Car("Honda", "2004"));
cars.add(new Car("Ford", "2005"));
cars.add(new Car("Ford", "2004"));
cars.add(new Car("Honda", "2005"));
cars.add(new Car("Toyota", "2004"));
cars.add(new Car("Cadillac", "2005"));
ListView<Car> view = new ListView();
view.setCellFactory((ListView<Car> param) -> {
ListCell<Car> cell = new ListCell<Car>() {
CarView carView = new CarView();
@Override
protected void updateItem(Car item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText("");
carView.setMake(item.getMake());
carView.setModel(item.getModel());
carView.setImageView(item.getUrl());
setGraphic(carView);
} else {
setText("");
setGraphic(null);
}
}
};
return cell;
});
ObservableList<Car> data = FXCollections.observableArrayList(cars);
FilteredList<Car> filteredList = new FilteredList(data);
view.setItems(filteredList);
HBox.setHgrow(view, Priority.ALWAYS);
CheckBox checkBox = new CheckBox("Honda");
checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(newValue)
{
filteredList.setPredicate((item) -> {
return item.getMake().equals("Honda");
});
}
else{
filteredList.setPredicate((item) -> {
return true;
});
}
});
CheckBox checkBox2 = new CheckBox("Ford");
checkBox2.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(newValue)
{
filteredList.setPredicate((item) -> {
return item.getMake().equals("Ford");
});
}
else{
filteredList.setPredicate((item) -> {
return true;
});
}
});
CheckBox checkBox3 = new CheckBox("2004");
checkBox3.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(newValue)
{
filteredList.setPredicate((item) -> {
return item.getModel().equals("2004");
});
}
else{
filteredList.setPredicate((item) -> {
return true;
});
}
});
CheckBox checkBox4 = new CheckBox("2005");
checkBox4.selectedProperty().addListener((observable, oldValue, newValue) -> {
if(newValue)
{
filteredList.setPredicate((item) -> {
return item.getModel().equals("2005");
});
}
else{
filteredList.setPredicate((item) -> {
return true;
});
}
});
VBox leftPanel = new VBox(checkBox, checkBox2, checkBox3, checkBox4);
HBox root = new HBox(leftPanel, view);
Scene scene = new Scene(root, 625, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
/**
*
* @author Sedrick
*/
final public class CarView extends HBox{
Label make = new Label();
Label model = new Label();
ImageView imageView = new ImageView();
public CarView(String make, String model, String url) {
this.make.setText(make);
this.model.setText(model);
HBox row1 = new HBox(new Label("Make: "), this.make);
HBox row2 = new HBox(new Label("Model: "), this.model);
VBox vbox = new VBox(row1, row2);
vbox.setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
StackPane stackPane1 = new StackPane(vbox);
HBox.setHgrow(stackPane1, Priority.ALWAYS);
Image image = new Image(url);
this.imageView.setImage(image);
this.imageView.setFitHeight(100);
this.imageView.setFitWidth(200);
StackPane stackPane2 = new StackPane(this.imageView);
stackPane2.setStyle("-fx-background-color: yellow");
getChildren().addAll(stackPane1, stackPane2);
setPrefSize(500, 125);
}
public CarView()
{
HBox row1 = new HBox(new Label("Make: "), this.make);
HBox row2 = new HBox(new Label("Model: "), this.model);
VBox vbox = new VBox(row1, row2);
vbox.setMaxSize(USE_PREF_SIZE, USE_PREF_SIZE);
StackPane stackPane1 = new StackPane(vbox);
HBox.setHgrow(stackPane1, Priority.ALWAYS);
this.imageView.setFitHeight(100);
this.imageView.setFitWidth(200);
StackPane stackPane2 = new StackPane(this.imageView);
stackPane2.setStyle("-fx-background-color: yellow");
getChildren().addAll(stackPane1, stackPane2);
setPrefSize(500, 125);
}
public void setImageView(String url) {
Image image = new Image(url);
this.imageView.setImage(image);
}
public void setMake(String make) {
this.make.setText(make);
}
public void setModel(String model)
{
this.model.setText(model);
}
}
/**
*
* @author Sedrick
*/
public class Car {
private String make;
private String model;
private String url = "https://cdn.pixabay.com/photo/2012/05/29/00/43/car-49278_960_720.jpg";
public Car(String make, String model) {
this.make = make;
this.model = model;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public String getUrl()
{
return url;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
}