Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Henriette Svenningsen
3,163 PointsHow to get a comboBox show a listview after selection
This is a personal project, but im stuck and hoping someone here can help.
I have this application where I want to be able to select an option in a combobox, eg. fruits or beverages. The selection should then show a list with different fruits or different beverages in a listview - but how do I do that?
I've been working on something here:
main class:
package grocerylist;
import javafx.application.Application; import static javafx.application.Application.launch; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.Priority; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage;
public class GroceryList extends Application { FruitVeg fruitVegList; Beverages beverageList; Bread breadList; ComboBox comboBox; ListView lv1; ListView lv2;
@Override public void start(Stage stage){
fruitVegList = new FruitVeg(); beverageList = new Beverages(); breadList = new Bread();
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 = new ComboBox(); comboBox.getItems().addAll( "Fruits and Vegetables", "Beverages", "Bread"
); comboBox.setPromptText("Choose a department"); grid.add(comboBox, 1, 1);
Label op2 = new Label("2."); op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20)); grid.add(op2, 0, 2);
lv1 = new ListVie(); grid.add(tf1, 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);
comboBox.setOnAction(event -> {
comboBox.getSelectionModel().selectedItemProperty()
.addListener((ObservableValue observable, Object oldvalue,
Object newValue) -> {
if (newValue == "Fruit and Vegetables") {
tf1.setText(fruitVegList);
}
});
});
root.getChildren().addAll(grid);
stage.setTitle("MY SHOPPING LIST"); stage.setScene(scene); stage.show(); }
public static void main(String[] args) { launch(args); } }
Fruit class
package grocerylist;
import java.util.List;
public class FruitVeg extends Groceries {
public FruitVeg(){ fruitVegList.add("Apple"); fruitVegList.add("Banana"); fruitVegList.add("Cucumber"); fruitVegList.add("Carrot"); fruitVegList.add("Kale"); fruitVegList.add("Salad"); fruitVegList.add("Pear");
}
public List<String> getFruit() { return fruitVegList; }
}
abstract class
package grocerylist;
import java.util.ArrayList; import java.util.List;
public abstract class Groceries {
List<String> fruitVegList = new ArrayList<>();
}