1
votes

Im very new to javafx, but want's to make this dropdown (combobox), so that when you make a selection, the text in a textbox or list will change - eg. when choosing fruits or beverages in dropdown a list with different fruits or beverages come up.

I can't use fxml. Can anyone get me started on how i do that?

This is my 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;
TextField tf1;
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);

   /* comboBox.setOnAction ((ActionEvent event) -> {
        comboBox.getSelectionModel().getSelectedItem();
        if (comboBox.getValue().equals("Fruits")) {
            lv1.setItem(fruitVegList.getFruit());
        }
    }); */


    Label op2 = new Label("2.");
    op2.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(op2, 0, 2);

    tf1 = new TextField();
    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); }
}

I have an abstract class as its part of my assignment, but is that why I can't make it work?

package grocerylist;

import java.util.ArrayList;
import java.util.List;


public abstract class Groceries {

List<String> fruitVegList = new ArrayList<>();   
}

Fruit and veg 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;
}

}    

How do I make it so when selecting from comboBox it will display the content of the fruit list?

1
Welcome to StackOverflow. Your question, as it's stated, isn't a very good fit for this site. I recommend you take the tour and look, in particular, at the help center. The problem with the way your question is stated is that we don't really know which part of this you are stuck on: maybe it is the part where you write a listener, or the part where you change the text of a label, or the part where you figure out which property of a ComboBox changes when the selection changes, etc. Consequently, any answer to this would basically be an entire tutorial on JavaFX. - James_D
See if you can post a specific question, with code indicating exactly which part of what you have tried is not working. - James_D

1 Answers

1
votes

Here is a runnable class to get you started

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage){

        final String[] comboBoxItems = {"Fruit", "Beverages"};

        ComboBox comboBox = new ComboBox();

        TextField textField = new TextField();

        VBox root = new VBox();

        comboBox.getItems().addAll(comboBoxItems);

        comboBox.setOnAction(event -> {
            textField.setText(comboBox.getValue().toString());
        });

        root.getChildren().addAll(comboBox, textField);

        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}