2017-12-01 5 views
-1

これはjavafxの新機能ですが、このコンボボックスを作成したいので、選択するとテキストボックスやリストのテキストが変更されます。ドロップダウンで果物や飲み物を選ぶときに別の果物や飲み物を持つリストは、リンクにtextArea/ListViewコントロールに出てくるなどコンボボックスでのJavaFXの選択結果が異なるtextareas/listview

メインクラス

public class GroceryList extends Application { 
    String FruitList; 
    TextArea ta1; 
    ListView lv2; 

    @Override 
    public void start(Stage stage){ 

     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<Department> comboBox = new ComboBox<>(); 
     ObservableList<Department> list = DepartmentList.getDepartmentList(); 
     comboBox.setItems(list); 
     comboBox.setPromptText("Choose a department"); 
     grid.add(comboBox, 1, 1); 

     /*comboBox.setOnAction((event) -> { 
       ta1.setText("Fruit"); 
     });*/ 
     comboBox.setOnAction(event -> { 


      comboBox.getSelectionModel().selectedItemProperty() 
      .addListener((ObservableValue observable, Object oldvalue, 
        Object newValue) -> { 
         if (newValue == "Fruit") { 
          ta1.setText(FruitList); 
         } 
        }); 

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

      ta1 = new TextArea(); 
      grid.add(ta1, 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); 


      root.getChildren().addAll(grid); 

      stage.setTitle("MY SHOPPING LIST"); 
      stage.setScene(scene); 
      stage.show(); 
     } 

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

省クラス

public class Department { 
    private String name; 

    public Department() { 
    } 

    public Department(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return this.name; 
    } 
} 

DepartmentListクラス

public class DepartmentList { 

    public static ObservableList<Department> getDepartmentList() { 
     Department fruit = new Department("Fruit"); 
     Department vegetables = new Department("Vegetables"); 
     Department beverages = new Department("Beverages"); 

     ObservableList<Department> list; 
     list = FXCollections.observableArrayList(fruit, vegetables, 
       beverages); 

     return list; 
    } 
} 

以上、私はFruitクラスとFruitListを持っていますクラスで、DepartmentおよびDepartmentListと同じです。 私は基本的に、何かを選択するときに、どのようにしてcomboBoxでtextAreaまたはListviewを得ることができるかを理解する助けが必要です。 OnAction、if文はcombiですか、私はこれにどのようにアプローチしますか?

答えて

1

ここにサンプルアプリケーションがあります。コード内のコメント

メイン

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Alert; 
import javafx.scene.control.ChoiceBox; 
import javafx.scene.control.ListView; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class JavaFXApplication51 extends Application 
{ 

    @Override 
    public void start(Stage primaryStage) 
    { 
     ListView<Fruit> fruitListView = new ListView(FruitList.getFruitList());//Create fruitListView and add its data 
     ListView<Vegetables> vegetablesListView = new ListView(VegetableList.getVetetableList());//Create vegetablesListView and add its data 

     ChoiceBox<Department> choiceBox = new ChoiceBox(DepartmentList.getDepartmentList());//Create ChoiceBox and add DepartmentList to it 
     choiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) ->//This is used to observe when the ChoiceBox's value change 
     { 
      if (newValue != null)// 
      { 
       Department tempDepartment = (Department) newValue; 
       switch (tempDepartment.toString())//Switch on the choosen Department value 
       { 
        case "Fruit": 
         fruitListView.setVisible(true);//When Fruit is selected in the ChoiceBox show the fruitList 
         vegetablesListView.setVisible(false); 
         break; 
        case "Vegetables": 
         fruitListView.setVisible(false); 
         vegetablesListView.setVisible(true);//When Vegatables is selected in the ChoiceBox show the vegetablesList 
         break; 
        case "Beverages": 
         Alert alert = new Alert(Alert.AlertType.ERROR); 
         alert.setHeaderText("Not Implemented Error!"); 
         alert.setContentText("You have not implemented this option yet! Do it now!"); 
         alert.showAndWait(); 

       } 
      } 
     }); 

     //Inital Setup 
     choiceBox.setValue(choiceBox.getItems().get(0));//Set the ChoiceBox's inital value to Fruit 

     //Since fruit is the inital value for the ChoicBox, set the fruitListView to visisble and the others to not visible 
     fruitListView.setVisible(true); 
     vegetablesListView.setVisible(false); 
     //End Initial Setup 

     StackPane stackPane = new StackPane(fruitListView, vegetablesListView);//Add both ListViews to the StackPane 
     VBox vbox = new VBox(stackPane, choiceBox);//Add ListViews and ChoiceBox to VBox 
     Scene scene = new Scene(vbox, 300, 250);//Add VBox to root 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 
} 

フルーツ

public class Fruit 
{ 

    private String name; 

    public Fruit() 
    { 
    } 

    public Fruit(String name) 
    { 
     this.name = name; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public void setName(String name) 
    { 
     this.name = name; 
    } 

    @Override 
    public String toString() 
    { 
     return this.name; 
    } 
} 

FruitList

import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 

public class FruitList 
{ 

    public static ObservableList<Fruit> getFruitList() 
    { 
     Fruit apple = new Fruit("Apple"); 
     Fruit orange = new Fruit("Orange"); 
     Fruit tomato = new Fruit("Tomato"); 

     ObservableList<Fruit> list; 
     list = FXCollections.observableArrayList(apple, orange, 
       tomato); 

     return list; 
    } 
} 

DepartmentList

import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 

public class DepartmentList 
{ 

    public static ObservableList<Department> getDepartmentList() 
    { 
     Department fruit = new Department("Fruit"); 
     Department vegetables = new Department("Vegetables"); 
     Department beverages = new Department("Beverages"); 

     ObservableList<Department> list; 
     list = FXCollections.observableArrayList(fruit, vegetables, 
       beverages); 

     return list; 
    } 
} 

注:目的に取り残さ他のクラス。

enter image description here

enter image description here

+0

アメージング!!!!私は今それを得る - ありがとう。学科、果物と野菜が同じではないのでもう少し質問がありますが、どういうわけか授業のようにすることは可能でしょうか? – HRS

+0

おそらくそれをすべて1つにしようとしないことが最善です。それは 'Department'クラスの再構築で行うことができます。課題は何を求めていますか? – Sedrick

+0

これは少なくとも3つのクラスを呼び出し、オブジェクトのリストを保持するためにArrayListを使用し、ボタン付きのJavaFX GUIを持っています(購入する選択項目を移動できる別のリストビューを作成しようとします)ボタン)、抽象クラスを持つか、すべてのメソッドに対してインターフェース、セッターメソッド、ゲッターメソッド、JavaDocアノテーションを実装します。だからまだ少ししてください;) – HRS

関連する問題