2016-07-14 9 views
5

hinzufügen Wie kann ich ein Element hinzufügen aus den ComboBox Wert auf Elemente in einem Kombinationsfeld so, wenn der Benutzer ich in der Lage bin für dieses Element der Preise angezeigt werdenWie kann ich einen Wert Element in einer ComboBox in JavaFX

Eg. Wenn der Benutzer ein Tier auswählt, kann ich den Preis dieses Tieres anzeigen. Die der Benutzer wählt dog dann kann ich den Preis von $45 anzeigen.

public class comboBox extends Application { 

    Stage window; 
    Scene scene; 
    Button button; 
    ComboBox<String> comboBox; 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     window = primaryStage; 
     window.setTitle("ComboBox"); 
     button = new Button("Submit"); 

     comboBox = new ComboBox<>(); 
     comboBox.getItems().addAll(
      "cat", 
      "dog", 
      "bird" 
     ); 

     comboBox.setPromptText("Please select one"); 
     button.setOnAction(e -> printPrice()); 

     VBox layout = new VBox(10); 
     layout.setPadding(new Insets(60, 60, 60, 60)); 
     layout.getChildren().addAll(comboBox, button); 

     scene = new Scene(layout, 450, 350); 
     window.setScene(scene); 
     window.show(); 
    } 

    private void printPrice(){ 
     System.out.println(comboBox.getValue()); 
    } 
} 

Ich habe versucht, den Code und das ist zu beheben, was ich habe es noch ein paar jemand Fehler wissen, was ich falsch mache?

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.collections.FXCollections; 

public class animals extends Application { 

Stage window; 
Scene scene; 
Button button; 
ComboBox<String> comboBox; 




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

@Override 
public void start(Stage primaryStage) throws Exception { 
    window = primaryStage; 
    window.setTitle("ComboBox "); 
    button = new Button("Submit"); 

    comboBox.setConverter(new StringConverter<Animal>() { 
@Override 
public String toString(Animal object) { 
    return object.getName(); 
} 

@Override 
public Animal fromString(String string) { 
    return null; 
} 
}); 


ComboBox<Animal> comboBox = new ComboBox<Animal>(); 
comboBox.setItems(FXCollections.observableArrayList(new Animal("Dog", 30.12), new Animal("Cat", 23.23), new Animal("Bird", 15.0))); 

comboBox.valueProperty().addListener((obs, oldVal, newVal) -> System.out.println("Price of the " + newVal.getName() + " is : " + newVal.getPrice())); } 

VBox layout = new VBox(10); 
    layout.setPadding(new Insets(60, 60, 60, 60)); 
    layout.getChildren().addAll(comboBox, button); 

    scene = new Scene(layout, 500, 350); 
    window.setScene(scene); 
    window.show(); 

} 

public class Animal { 
private String name; 
private Double price; 

public Double getPrice() { 
    return price; 
} 

public String getName() { 
    return name; 
} 

public Animal(String name, Double price) { 
    this.name = name; 
    this.price = price; 

} 
} 

auch, wie würde ich in der Lage sein, den Preis unter dem Kombinationsfeld angezeigt werden, nachdem der Benutzer ein Tier wählt? so würde es sagen "der Preis für diese Tierkosten"

Antwort

9

Sie sollten ein ComboBox Datenmodell zur Verfügung stellen, das den Namen und den Preis des Tieres speichert, zum Beispiel Fälle der Klasse Animal.

public class Animal { 
    private String name; 
    private Double price; 

    public Double getPrice() { 
     return price; 
    } 

    public String getName() { 
     return name; 
    } 

    public Animal(String name, Double price) { 
     this.name = name; 
     this.price = price; 
    } 
} 

Dann in Ihrem ComboBox können Sie diese Animal Instanzen angezeigt:

ComboBox<Animal> comboBox = new ComboBox<Animal>(); 
comboBox.setItems(FXCollections.observableArrayList(
    new Animal("Dog", 30.12), 
    new Animal("Cat", 23.23), 
    new Animal("Bird", 15.0))); 

comboBox.valueProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println("Price of the " + newVal.getName() + " is : " + newVal.getPrice())); 

Das einzige, was auf den ComboBox die Namen der Tiere angezeigt werden links und nicht die Objekte selbst. Um dies zu erreichen, können Sie beispielsweise verwenden, um eine StringConverter:

comboBox.setConverter(new StringConverter<Animal>() { 
    @Override 
    public String toString(Animal object) { 
     return object.getName(); 
    } 

    @Override 
    public Animal fromString(String string) { 
     return null; 
    } 
}); 

Auf Wertänderung, der Ausgang ist wie:

Price of the Cat is : 23.23 
Price of the Dog is : 30.12 
Price of the Bird is : 15.0 

Ein MCVE:

public class Animals extends Application { 
    private ComboBox<Animal> comboBox = new ComboBox<>(); 
    private Text textNamePrice = new Text(); 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     comboBox.setConverter(new StringConverter<Animal>() { 
      @Override 
      public String toString(Animal object) { 
       return object.getName(); 
      } 

      @Override 
      public Animal fromString(String string) { 
       return null; 
      } 
     }); 

     comboBox.setItems(FXCollections.observableArrayList(new Animal("Dog", 30.12), 
       new Animal("Cat", 23.23), 
       new Animal("Bird", 15.0))); 

     comboBox.valueProperty().addListener((obs, oldVal, newVal) -> { 
      String selectionText = "Price of the " + newVal.getName() + " is : " + newVal.getPrice(); 

      System.out.println(selectionText); 
      textNamePrice.setText(selectionText); 
     }); 

     VBox layout = new VBox(10); 
     layout.setPadding(new Insets(60, 60, 60, 60)); 
     layout.getChildren().addAll(comboBox, textNamePrice); 

     Scene scene = new Scene(layout, 500, 350); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public class Animal { 
     private String name; 
     private Double price; 

     public Double getPrice() { return price; } 

     public String getName() { return name; } 

     public Animal(String name, Double price) { 
      this.name = name; 
      this.price = price; 
     } 
    } 
} 
+0

Dank brauche ich um irgendetwas für die FXCollections zu importieren, wie ich diesen Fehler bekomme Kann nicht finden Symbol: FXCollections – user6587841

+1

ja, sollten Sie 'j importieren avafx.collections.FXCollections'. – DVarga

+0

Ich habe einen Fehler jetzt animals.java:52: Fehler: Klasse Animal ist öffentlich, sollte in einer Datei namens Animal.java deklariert werden 'öffentliche Klasse Animal {' – user6587841