2016-07-14 11 views
5

コンボボックス内のアイテムに値を追加するにはどうすればいいですか?ComboBoxそのアイテムの価格を表示できますコンボボックス内の項目に値を追加するには

例:ユーザーが動物を選択すると、その動物の価格を表示することができます。 ユーザーがdogを選択すると、$45の価格を表示することができます。

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

私はコードを修正しようとしましたが、これは私が間違ってやっていることを知っているいくつかのエラーがまだありますか?

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; 

} 
} 

また、ユーザーが動物を選択した後、コンボボックスの下に価格を表示するにはどうすればよいですか?それで「その動物費の価格」と言うでしょう

答えて

9

Animalのインスタンスなど、動物の名前と価格を格納するComboBoxにデータモデルを提供する必要があります。

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

その後で、あなたのComboBoxあなたはこれらのAnimalインスタンスを表示することができます

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

唯一のものではなく、オブジェクト自体よりもComboBox上の動物の名前を表示するために残しました。これを実現するために、あなたは例えばStringConverter使用することができます値の変更で

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

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

を、出力は次のようである:

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

アン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

おかげで私は必要なのですかFXCollectionsのために何かをインポートするときに、このエラーが発生する シンボルが見つからない:FXCollections – user6587841

+1

はい、 'j avafx.collections.FXCollections'。 – DVarga

+0

私は今、1つのエラーがあります animals.java:52:エラー:クラスAnimalは、Animal.javaという名前のファイルで宣言する必要があります。 'publicクラスAnimal {' – user6587841

関連する問題