2016-07-14 18 views
5

में कोई ComboBox में आइटम करने के लिए एक मूल्य जोड़ने मैं एक कॉम्बो बॉक्स में आइटम करने के लिए एक मूल्य जोड़ सकते हैं करते हैं इसलिए जब उपयोगकर्ता एक आइटम ComboBox से मैं उस आइटम के लिए मूल्य प्रदर्शित करने में सक्षम हूँ का चयन करता हैमैं कैसे JavaFX

ईजी। यदि उपयोगकर्ता एक जानवर का चयन करता है तो मैं उस जानवर की कीमत प्रदर्शित कर सकता हूं। उपयोगकर्ता 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

आपको ComboBox पर डेटा मॉडल प्रदान करना चाहिए जो जानवर का नाम और मूल्य संग्रहीत करता है, उदाहरण के लिए कक्षा 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; 
    } 
} 

फिर अपनी 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

मेरे पास अब एक त्रुटि है animals.java:52: त्रुटि: कक्षा पशु सार्वजनिक है, जिसे Animal.java 'सार्वजनिक वर्ग पशु {' – user6587841