2014-11-12 1 views
0

콤보로 텍스트를 표시하는 데 사용 된 개체의 내용이 변경되면 내 콤보 상자를 업데이트하고 싶습니다.콤보 상자 새로 고침 값 및 개체 내용 변경시 목록보기

package com.javafx.example.combobox; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Priority; 
import javafx.stage.Stage; 
import javafx.util.StringConverter; 

public class ComboboxSample extends Application { 


    class Sequence { 
     public StringProperty name = new SimpleStringProperty(); 

     public Sequence(String name) { 
      super(); 
      this.name.set(name); 
     } 

     @Override 
     public String toString() { 
      return "null"; 
     } 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("ComboBoxSample"); 

     ComboBox<Sequence> combo = new ComboBox<>(); 
     combo.setItems(FXCollections.observableArrayList(
       new Sequence("Toto"), 
       new Sequence("Titi"))); 
     combo.getSelectionModel().selectFirst(); 
     combo.setConverter(new StringConverter<ComboboxSample.Sequence>() { 
      @Override 
      public String toString(Sequence sequence) { 
       return sequence.name.get(); 
      } 

      @Override 
      public Sequence fromString(String string) { 
       System.out.println("call fromString"); 
       return null; 
      } 
     }); 

     TextField text = new TextField(); 
     Button renameButton = new Button("Rename"); 

     renameButton.setOnAction(evt -> combo.getValue().name.set(text.getText())); 

     HBox root = new HBox(combo, text, renameButton); 
     HBox.setHgrow(text, Priority.ALWAYS); 

     stage.setScene(new Scene(root)); 
     stage.show(); 

    } 

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

콤보 상자는 속성 이름을 가진 개체를 포함 : 여기

는 샘플입니다. 이 속성의 이름을 변경하면 표시가 변경되지 않거나 때로는 변경되지 않지만 항상 변경 될 수 있습니다. 이는 콘텐트가 변경 될 때가 아니라 객체가 변경 될 때 콤보 박스 업데이트와 같은 표준 동작입니다.

어떻게하면 콤보 상자에서 값을 새로 고치고 목록보기를 변경하면 강제로 업데이트 할 수 있습니까?

감사

EDIT1 : observaleList에 콜백을 사용

는 해결책이 될 것으로 보인다. package com.javafx.example.combobox;

import javafx.application.Application; 
import javafx.beans.Observable; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Priority; 
import javafx.stage.Stage; 
import javafx.util.Callback; 
import javafx.util.StringConverter; 

public class ComboboxSample extends Application { 

    ObservableList<Sequence> sequences; 


    class Sequence { 
     public StringProperty name = new SimpleStringProperty(); 

     public Sequence(String name) { 
      super(); 
      this.name.set(name); 
     } 

     @Override 
     public String toString() { 
      return "null"; 
     } 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("ComboBoxSample"); 

     Callback<Sequence, Observable[]> extractor = new Callback<Sequence, Observable[]>() { 
      @Override 
      public Observable[] call(Sequence s) { 
       return new Observable[] {s.name}; 
      } 
     }; 
     sequences = FXCollections.observableArrayList(extractor); 
     sequences.addAll(
       new Sequence("Toto"), 
       new Sequence("Titi")); 

     ComboBox<Sequence> combo = new ComboBox<>(); 
     combo.setItems(sequences); 
     combo.getSelectionModel().selectFirst(); 
     combo.setConverter(new StringConverter<ComboboxSample.Sequence>() { 
      @Override 
      public String toString(Sequence sequence) { 
       return sequence.name.get(); 
      } 

      @Override 
      public Sequence fromString(String string) { 
       System.out.println("call fromString"); 
       return null; 
      } 
     }); 
     combo.valueProperty().addListener((obs, oldValue, newValue) -> System.out.println("Change from " + oldValue.name.get() + " to " + newValue.name.get())); 

     TextField text = new TextField(); 
     Button renameButton = new Button("Rename"); 

     renameButton.setOnAction(evt -> { 
      combo.getValue().name.set(text.getText()); 
     }); 

     HBox root = new HBox(combo, text, renameButton); 
     HBox.setHgrow(text, Priority.ALWAYS); 

     stage.setScene(new Scene(root)); 
     stage.show(); 

    } 

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

답변

2

새 값이 개체 목록에 추가 될 때마다 combobox 값을 다시 설정하십시오. 나를 위해 일합니다. 이것을 보여주기 위해 작은 예를 들었습니다. 도움이되기를 바랍니다.

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.ComboBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 


public class ComboTest extends Application { 
    int i =0; 
    ObservableList<String> list = FXCollections.observableArrayList("A","B"); 
    ComboBox<String> combo = new ComboBox(); 
    @Override 
    public void start(Stage primaryStage) { 
     Button btn = new Button(); 
     combo.setPromptText("Testing combobox"); 
     combo.setPrefWidth(300); 
     btn.setText("Add items to list"); 
     btn.setOnAction(new EventHandler<ActionEvent>() { 

      @Override 
      public void handle(ActionEvent event) { 
       list.add(String.valueOf(i)); 
       System.out.println("size of list " + list.size()); 
       i++; 
       combo.setItems(list); 
      } 
     }); 
     combo.setItems(list); 
     VBox root = new VBox(); 
     root.getChildren().addAll(btn,combo); 
     root.setSpacing(20); 
     root.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(root); 

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

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

} 
관련 문제