2016-04-08 27 views
0

Projemde JavaFX, Hibernate, Spring kullanıyorum.JavaFX combobox, nesnelerden doğru değeri göstermedi

Ve combobox'ı nesne değerlerimle doldurmam gerekiyor. Açılan kutucumda modelimin yalnızca başlık değerini göstermem gerekiyor.

Benim modelim sınıfı: okuduğum (yaklaşık hücre fabrikası: combobox bu verilerle diğer bazı manipülasyonlar yapmak için bazı yöntem-dinleyici yoktur

public class Sector extends Identifier { 
    private String title; 

    private List<Stage> stageList; 

    public List<Stage> getStageList() { 
     return stageList; 
    } 

    public void setStageList(List<Stage> stageList) { 
     this.stageList = stageList; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    @Override 
    public String toString() { 
     return "Sector{" + 
       "id='" + getId() + '\'' + 
       "title='" + title + '\'' + 
       ", stageList=" + stageList + 
       '}'; 
    } 
} 

ve benim denetleyicisi olarak

public class Stage extends Identifier { 
    private String name; 

    private Station firstStation; 

    private Station secondStation; 

    private List<CommunicationDistance> communicationDistanceList; 

    public Stage() { 
    } 

    public Stage(String name, Station firstStation, Station secondStation) { 
     this.name = name; 
     this.firstStation = firstStation; 
     this.secondStation = secondStation; 
    } 

    public List<CommunicationDistance> getCommunicationDistanceList() { 
     return communicationDistanceList; 
    } 

    public void setCommunicationDistanceList(List<CommunicationDistance> communicationDistanceList) { 
     this.communicationDistanceList = communicationDistanceList; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public Station getFirstStation() { 
     return firstStation; 
    } 

    public void setFirstStation(Station firstStation) { 
     this.firstStation = firstStation; 
    } 

    public Station getSecondStation() { 
     return secondStation; 
    } 

    public void setSecondStation(Station secondStation) { 
     this.secondStation = secondStation; 
    } 

    @Override 
    public String toString() { 
     return "Stage{" + 
       "id='" + getId() + '\'' + 
       "name='" + name + '\'' + 
       ", firstStation=" + firstStation + 
       ", secondStation=" + secondStation + 
       ", communicationDistanceList=" + communicationDistanceList + 
       '}'; 
    } 

this question ve ayrıca from here)

Tüm çalışıyor, ama benim comboboxes değerler bunlar gibi gösterilen: enter image description here

Bu, benim doğru nesnelerdir ama hala benim Sektör ve diğer nesnelerden sadece başlık alanını göstermek için combobox biçimlendirmek anlayamıyorum ? Birleşik giriş çıkışımı biçimlendirmek için geçerli/doğru örnek gösterebilir misiniz?

Düzenleme 1: Benim init yöntemimde, yalnızca benim nesnelerimin listesini açılan kutuya ekledim. Bu doğru bir yol olduğunu değil eminim, ama combobox değerler seçildikten sonra bu verileri doğrulamak istiyorsanız - ı combobox içinde tam bir nesne ayarlamak için gerekir:

@FXML 
     public ComboBox sectorComboBox; 

    @Override 
     public void initialize(URL location, ResourceBundle resources) { 
      sectorComboBox.setItems(FXCollections.observableArrayList(sectorService.listAll())); 
     } 
+1

Hata ayıklamadınız mı? setText (item.getName()); Burada bir String döndürülür ve combobox bu String'i gösterir, yani problemin combobox'ınızda değil, "getName()" yanlış değeri döndürdüğü anlamına gelir -> Model belki doğru ayarlanmamıştır. – DVarga

+0

Hi DVarga. Lütfen yeni düzenlememe bakın. –

+1

Haklıydın. Kayıtlı verilerde, combobox'ta bir sorun vardı. ItachiUchiha varyantı ve tavsiyenizle - bu sorunu çözdüm. Teşekkürler! –

cevap

4

Sen geçersiz kılma bir StringConverter yerine kullanmalıdır ListCell. Bu, aynı sonucu elde etmenin daha zarif bir yoludur.

StringConverter<Sector> converter = new StringConverter<Sector>() { 
    @Override 
    public String toString(Sector object) { 
     return object.getTitle(); 
    } 

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

MCVE

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.StringConverter; 

public class ComboBoxConverter extends Application { 
    @Override 
    public void start(Stage primaryStage) throws Exception { 

     ComboBox<Sector> comboBox = new ComboBox<>(); 
     StringConverter<Sector> converter = new StringConverter<Sector>() { 
      @Override 
      public String toString(Sector object) { 
       return object.getTitle(); 
      } 

      @Override 
      public Sector fromString(String string) { 
       return null; 
      } 
     }; 
     comboBox.setConverter(converter); 

     comboBox.setItems(FXCollections.observableArrayList(new Sector("Title1", 24), new Sector("Title2", 50))); 
     comboBox.getSelectionModel().selectFirst(); 
     VBox box = new VBox(comboBox); 
     box.setAlignment(Pos.CENTER); 
     Scene scene = new Scene(box, 200, 200); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 

sektör sınıfı iki alan ile basit POJO olduğunu.

public class Sector { 
    private String title; 
    private double area; 

    public Sector(String title, double area) { 
     this.title = title; 
     this.area = area; 
    } 

    public double getArea() { 
     return area; 
    } 

    public void setArea(double area) { 
     this.area = area; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 
} 
+0

Teşekkür ederim :) Bu yardımcı oldu. Ayrıca benim varlığım ve çözümünüzle ilgili bir sorun buldum - iyi çalışıyor. –