2013-12-13 183 views
0

我想創建其顯示爲一個組合框組的列表:標籤未格式化

import java.util.List; 
import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ListCell; 
import javafx.scene.control.ListView; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class Listobject extends Application 
{ 

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

    @Override 
    public void start(Stage stage) 
    { 

     // Insert Some data 
     ListGroupsObj ob = ListGroupsObj.newInstance().groupId(12).groupName("Test"); 
     ListGroupsObj osb = ListGroupsObj.newInstance().groupId(13).groupName("Test2"); 

     final ComboBox<ListGroupsObj> listGroups = new ComboBox(); 

     listGroups.setButtonCell(new GroupListCell()); 
     listGroups.setCellFactory(new Callback<ListView<ListGroupsObj>, ListCell<ListGroupsObj>>() 
     { 
      @Override 
      public ListCell<ListGroupsObj> call(ListView<ListGroupsObj> p) 
      { 
       return new GroupListCell(); 
      } 
     }); 

     listGroups.setEditable(true); 

     listGroups.getItems().addAll(ob, osb); 
     listGroups.setValue(ob); 

     // Display the selected Group 
     listGroups.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<ListGroupsObj>() 
     { 

      @Override 
      public void changed(ObservableValue<? extends ListGroupsObj> arg0, ListGroupsObj arg1, ListGroupsObj arg2) 
      { 
       if (arg2 != null) 
       { 
        System.out.println("Selected Group: " + arg1.getGroupId() + " - " + arg2.getGroupName()); 
       } 
      } 
     }); 

     final StackPane layout = new StackPane(); 
     layout.getChildren().add(listGroups); 
     layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 15;"); 
     stage.setScene(new Scene(layout)); 
     stage.show(); 
    } 

    class GroupListCell extends ListCell<ListGroupsObj> 
    { 
     @Override 
     protected void updateItem(ListGroupsObj item, boolean empty) 
     { 
      super.updateItem(item, empty); 
      if (item != null) 
      { 
       setText(item.getGroupId() + " - " + item.getGroupName()); 
      } 
     } 
    } 

    private List<ListGroupsObj> listGroups; 

    public static class ListGroupsObj 
    { 

     private int groupId; 
     private String groupName; 

     public static ListGroupsObj newInstance() 
     { 
      return new ListGroupsObj(); 
     } 

     public ListGroupsObj() 
     { 
     } 

     public ListGroupsObj groupId(int groupId) 
     { 
      this.groupId = groupId; 
      return this; 
     } 

     public ListGroupsObj groupName(String groupName) 
     { 
      this.groupName = groupName; 
      return this; 
     } 

     public int getGroupId() 
     { 
      return groupId; 
     } 

     public String getGroupName() 
     { 
      return groupName; 
     } 

     //  @Override 
//  public String toString() 
//  { 
//   return serverName; 
//  } 
    } 
} 

enter image description here

當我添加listGroups.setEditable(真);默認選定組的標籤不可讀。你能幫我解決這個問題嗎?你也可以告訴我如何優化這段代碼使其易於使用嗎?

+0

的可能重複的[了JavaFx編輯ComboBox:上項選擇顯示的toString(http://stackoverflow.com/questions/19242747/javafx-editable-combobox-showing-tostring-on-item-selection) –

回答

2

向組合框提供一個converter,將字符串轉換爲ListGroupsObj類型並返回。

+0

還有其他更簡單的解決方案嗎? –

+0

不,我不確定我甚至可以想象任何比提供轉換對象更簡單的解決方案。 –

-1
import javafx.application.Application; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ListCell; 
import javafx.scene.control.ListView; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

/** 
* 
* @author Vishal 
*/ 
public class Listobject extends Application { 

    ComboBox<ListGroups> cmbListgroup; 

    @Override 
    public void start(Stage stage) { 

     cmbListgroup = new ComboBox<>(); 
     cmbListgroup.setEditable(true); 

     ListGroups listGroups = ListGroups.newInstance().groupId(1).groupName("Group 001"); 
     cmbListgroup.getItems().addAll(listGroups); 
     ListGroups listGroups2 = ListGroups.newInstance().groupId(2).groupName("Group 002"); 
     cmbListgroup.getItems().addAll(listGroups2); 
     cmbListgroup.setValue(listGroups); 

     cmbListgroup.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() { 

      @Override 
      public void changed(ObservableValue<? extends Object> olValue, Object t, Object newValue) { 
       System.out.println("Name : " + newValue); 
      } 
     }); 

     StackPane layout = new StackPane(); 
     layout.getChildren().add(cmbListgroup); 
     layout.setStyle("-fx-background-color: white; -fx-padding: 10;"); 
     stage.setTitle("List Groups in Combo !"); 
     stage.setScene(new Scene(layout, 320, 100)); 
     stage.show(); 
    } 

    public static class ListGroups { 

     private int groupId; 
     private String groupName; 

     public static ListGroups newInstance() { 
      return new ListGroups(); 
     } 

     public ListGroups() { 
     } 

     public ListGroups(int groupId, String groupName) { 
      this.groupId = groupId; 
      this.groupName = groupName; 
     } 

     public ListGroups groupId(int groupId) { 
      this.groupId = groupId; 
      return this; 
     } 

     public ListGroups groupName(String groupName) { 
      this.groupName = groupName; 
      return this; 
     } 

     public int getGroupId() { 
      return groupId; 
     } 

     public String getGroupName() { 
      return groupName; 
     } 

     @Override 
     public String toString() { 
      return groupName; 
     } 
    } 

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

歡迎來到SO,謝謝你試圖幫助別人:)請解釋你的解決方案(與簡單地顯示一些代碼)。至於代碼:據我所知,你建議覆蓋toString方法返回一些容易閱讀的東西?如果是這樣,那就錯了,就像完全錯誤。永遠不要實現一個數據對象來滿足一些視覺上下文的需求!你會如何顯示不同的視覺要求?按照@James_D的建議使用轉換器是正確的選擇。 – kleopatra