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












0















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 dealership website with car pictures. The user should be able to filter the pictures displayed by clicking checkboxes and/or radio buttons selection.



I'm first creating all the picture buttons with a for each loop. I could use if and if/else statements to filter through the pictures but there would be duplicates. I've heard of observablelist but I haven't learned those yet.



Can someone help me out with this one please? Thank you!



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());
}
}









share|improve this question























  • When you say you haven't learned about ObservableList yet, does that mean you are not able to use them? Is this for a homework assignment or are you open to learning how the ObservableList works?

    – Zephyr
    Nov 20 '18 at 21:17











  • Look into ListView.

    – Sedrick
    Nov 20 '18 at 22:12











  • @Zephyr. It's for homework but I can use ObservableList. I understand the theory of them so I could explain them. I'm just not familiar with the coding of them.

    – btwPipes
    Nov 20 '18 at 22:16
















0















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 dealership website with car pictures. The user should be able to filter the pictures displayed by clicking checkboxes and/or radio buttons selection.



I'm first creating all the picture buttons with a for each loop. I could use if and if/else statements to filter through the pictures but there would be duplicates. I've heard of observablelist but I haven't learned those yet.



Can someone help me out with this one please? Thank you!



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());
}
}









share|improve this question























  • When you say you haven't learned about ObservableList yet, does that mean you are not able to use them? Is this for a homework assignment or are you open to learning how the ObservableList works?

    – Zephyr
    Nov 20 '18 at 21:17











  • Look into ListView.

    – Sedrick
    Nov 20 '18 at 22:12











  • @Zephyr. It's for homework but I can use ObservableList. I understand the theory of them so I could explain them. I'm just not familiar with the coding of them.

    – btwPipes
    Nov 20 '18 at 22:16














0












0








0








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 dealership website with car pictures. The user should be able to filter the pictures displayed by clicking checkboxes and/or radio buttons selection.



I'm first creating all the picture buttons with a for each loop. I could use if and if/else statements to filter through the pictures but there would be duplicates. I've heard of observablelist but I haven't learned those yet.



Can someone help me out with this one please? Thank you!



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());
}
}









share|improve this question














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 dealership website with car pictures. The user should be able to filter the pictures displayed by clicking checkboxes and/or radio buttons selection.



I'm first creating all the picture buttons with a for each loop. I could use if and if/else statements to filter through the pictures but there would be duplicates. I've heard of observablelist but I haven't learned those yet.



Can someone help me out with this one please? Thank you!



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());
}
}






java javafx






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 20:54









btwPipesbtwPipes

1




1













  • When you say you haven't learned about ObservableList yet, does that mean you are not able to use them? Is this for a homework assignment or are you open to learning how the ObservableList works?

    – Zephyr
    Nov 20 '18 at 21:17











  • Look into ListView.

    – Sedrick
    Nov 20 '18 at 22:12











  • @Zephyr. It's for homework but I can use ObservableList. I understand the theory of them so I could explain them. I'm just not familiar with the coding of them.

    – btwPipes
    Nov 20 '18 at 22:16



















  • When you say you haven't learned about ObservableList yet, does that mean you are not able to use them? Is this for a homework assignment or are you open to learning how the ObservableList works?

    – Zephyr
    Nov 20 '18 at 21:17











  • Look into ListView.

    – Sedrick
    Nov 20 '18 at 22:12











  • @Zephyr. It's for homework but I can use ObservableList. I understand the theory of them so I could explain them. I'm just not familiar with the coding of them.

    – btwPipes
    Nov 20 '18 at 22:16

















When you say you haven't learned about ObservableList yet, does that mean you are not able to use them? Is this for a homework assignment or are you open to learning how the ObservableList works?

– Zephyr
Nov 20 '18 at 21:17





When you say you haven't learned about ObservableList yet, does that mean you are not able to use them? Is this for a homework assignment or are you open to learning how the ObservableList works?

– Zephyr
Nov 20 '18 at 21:17













Look into ListView.

– Sedrick
Nov 20 '18 at 22:12





Look into ListView.

– Sedrick
Nov 20 '18 at 22:12













@Zephyr. It's for homework but I can use ObservableList. I understand the theory of them so I could explain them. I'm just not familiar with the coding of them.

– btwPipes
Nov 20 '18 at 22:16





@Zephyr. It's for homework but I can use ObservableList. I understand the theory of them so I could explain them. I'm just not familiar with the coding of them.

– btwPipes
Nov 20 '18 at 22:16












2 Answers
2






active

oldest

votes


















0














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



ObservableList<Car> 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<Car> 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<Car>() {
@Override
public void onChanged(Change<? extends Car> 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)




screenshot






share|improve this answer
























  • Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

    – btwPipes
    Nov 21 '18 at 2:13



















0














This is a terrible implementation but It will give you some ideas on how to do things. You need to research FilteredList, ListView, and Predicate. This implementation does not handle more than one CheckBox at a time. It will only display the last CheckBox action.




CarList




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);
}

}



CarView




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);
}

}



Car




/**
*
* @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;
}
}


enter image description here






share|improve this answer


























  • Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

    – btwPipes
    Nov 21 '18 at 2:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401366%2fhow-to-generate-buttonpicture-based-on-checkboxes-selection-in-javafx%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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



ObservableList<Car> 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<Car> 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<Car>() {
@Override
public void onChanged(Change<? extends Car> 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)




screenshot






share|improve this answer
























  • Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

    – btwPipes
    Nov 21 '18 at 2:13
















0














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



ObservableList<Car> 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<Car> 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<Car>() {
@Override
public void onChanged(Change<? extends Car> 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)




screenshot






share|improve this answer
























  • Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

    – btwPipes
    Nov 21 '18 at 2:13














0












0








0







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



ObservableList<Car> 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<Car> 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<Car>() {
@Override
public void onChanged(Change<? extends Car> 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)




screenshot






share|improve this answer













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



ObservableList<Car> 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<Car> 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<Car>() {
@Override
public void onChanged(Change<? extends Car> 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)




screenshot







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 22:47









ZephyrZephyr

4,1492932




4,1492932













  • Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

    – btwPipes
    Nov 21 '18 at 2:13



















  • Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

    – btwPipes
    Nov 21 '18 at 2:13

















Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

– btwPipes
Nov 21 '18 at 2:13





Thank you Zephyr. I think my issue is how I setup my primaryStage and my inexperience. It's hard to explain in words what the issue is... I'm sure someone with your skill could fix it in 2 seconds but I'm just a beginner. I know I'll get an A on the project but it's more of a personal satisfaction... Thanks again for your help, I'll sure use observable list in the future.

– btwPipes
Nov 21 '18 at 2:13













0














This is a terrible implementation but It will give you some ideas on how to do things. You need to research FilteredList, ListView, and Predicate. This implementation does not handle more than one CheckBox at a time. It will only display the last CheckBox action.




CarList




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);
}

}



CarView




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);
}

}



Car




/**
*
* @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;
}
}


enter image description here






share|improve this answer


























  • Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

    – btwPipes
    Nov 21 '18 at 2:26
















0














This is a terrible implementation but It will give you some ideas on how to do things. You need to research FilteredList, ListView, and Predicate. This implementation does not handle more than one CheckBox at a time. It will only display the last CheckBox action.




CarList




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);
}

}



CarView




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);
}

}



Car




/**
*
* @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;
}
}


enter image description here






share|improve this answer


























  • Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

    – btwPipes
    Nov 21 '18 at 2:26














0












0








0







This is a terrible implementation but It will give you some ideas on how to do things. You need to research FilteredList, ListView, and Predicate. This implementation does not handle more than one CheckBox at a time. It will only display the last CheckBox action.




CarList




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);
}

}



CarView




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);
}

}



Car




/**
*
* @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;
}
}


enter image description here






share|improve this answer















This is a terrible implementation but It will give you some ideas on how to do things. You need to research FilteredList, ListView, and Predicate. This implementation does not handle more than one CheckBox at a time. It will only display the last CheckBox action.




CarList




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);
}

}



CarView




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);
}

}



Car




/**
*
* @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;
}
}


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 16:56

























answered Nov 21 '18 at 2:04









SedrickSedrick

6,42932040




6,42932040













  • Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

    – btwPipes
    Nov 21 '18 at 2:26



















  • Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

    – btwPipes
    Nov 21 '18 at 2:26

















Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

– btwPipes
Nov 21 '18 at 2:26





Thanks Sedrick. That is what I needed to know before starting the project.... What I'm doing is setVisible(false) so the buttons/tiles not matching the criterias selected are not visible. It's not pretty but it kinda does the job.

– btwPipes
Nov 21 '18 at 2:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401366%2fhow-to-generate-buttonpicture-based-on-checkboxes-selection-in-javafx%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)