javajavafxscrollpanevboxhbox

Javafx and Java


I have a question and I really appreciate any kind of help and answers.

This is my problem:

I'm working with edmunds api. I already created the classes and I can parse my json data in my classes objects. Now, I want to display those object on as Scrollpane. To do that, I created a "for" loop to create an HBOX for every object, with labels containing name of my object's attribute.

Until now, displaying data in HBOXs and scrollpane is successful, but they are just HBoxs, and I can't select any of HBOX to work with.

In my scrollpane, I display car's make (like bmw, audi, etc) and when I select the HBOx of bmw for example, I just want to display all models of this make.

Please just tell me if you don't understand something

this is my class "make":

@JsonIgnoreProperties(ignoreUnknown = true)
public class Make {

    @JsonProperty("id")
    private int mk_id ;
    @JsonProperty("name")
    private String mk_name;
    @JsonProperty("niceName")
    private String mk_nicename;
    @JsonProperty("models")
    private List<Model> list_modele; }

my class Model:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Model {

    @JsonProperty("id")
    private String md_id ;
    @JsonProperty("name")
    private String md_name;
    @JsonProperty("niceName")
    private String md_nicename;
    @JsonProperty("years")
    private List<Years> list_years;}

and my loop for:

@FXML public void btn_clicked (javafx.event.ActionEvent e)
    {


        All_makes t = (All_makes)newparse_object <All_makes>(All_makes.class).ParseUri("https://api.edmunds.com/api/vehicle/v2/makes?state=new&fmt=json&api_key=wdxg7wh338vac3359m34qjj6");
        HBox o = new HBox();
        for( int i =0 ; i< t.get_list_makes().size();i++)
        {
        VBox b = new VBox(10);
        Label label = new Label(t.get_list_makes().get(i).get_mk_name());
        Label label2 = newLabel(t.get_list_makes().get(i).get_mk_nicename());
        b.getChildren().addAll(label, label2);
        b.setMaxSize(100, 100);
        o.getChildren().add(b);
        }
        o.maxWidth(5);
        scroll_pane.setContent(o);

}

Solution

  • A ListView provides selection functionality, as well as scrolling out of the box. Here's a quick example:

    import javafx.application.Application;
    import javafx.geometry.Orientation;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.ListCell;
    import javafx.scene.control.ListView;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class ListViewExample extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            ListView<Make> listView = new ListView<>();
            listView.setOrientation(Orientation.HORIZONTAL);
    
            listView.setMaxHeight(100);
    
            listView.setCellFactory(lv -> new ListCell<Make>() {
                private Label nameLabel = new Label();
                private Label niceNameLabel = new Label();
                private VBox vbox = new VBox(nameLabel, niceNameLabel);
    
                {
                    vbox.setMaxSize(100, 100);
                }
    
                @Override
                protected void updateItem(Make make, boolean empty) {
                    super.updateItem(make, empty);
                    if (empty) {
                        setGraphic(null);
                    } else {
                        nameLabel.setText(make.getMk_name());
                        niceNameLabel.setText(make.getMk_nicename());
                        setGraphic(vbox);
                    }
                }
            });
    
            listView.getSelectionModel().selectedItemProperty().addListener((obs, oldMake, newMake) -> {
                System.out.println(newMake.getMk_name() + " selected");
            });
    
            for (int i = 1 ; i <= 10 ; i++) {
                Make make = new Make();
                make.setMk_name("Make "+i);
                make.setMk_nicename("Description of make "+i);
                listView.getItems().add(new Make("Make "+i, "Description of make "+i));
            }
    
            Scene scene = new Scene(new StackPane(listView));
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }