2014-11-11 2 views
3

나는 다음 문서를 읽고 http://docs.oracle.com/javafx/2/ui_controls/combo-box.htm과 나는 나의 필요와 비슷한 것을 찾지 못했다. 콤보 상자에서 옵션을 그룹화 할 방법을 찾고있었습니다. 내 콤보 박스가 지속 시간이라고 가정합니다. 다음 옵션이 있습니다 : - 지난 1 시간, 지난 2 시간, 지난 24 시간, 지난 주, 지난 30 일, 지난 3 개월, 작년. 콤보 상자에 "짧은 지속 시간"과 "긴 지속 시간"이라는 레이블을 추가했습니다. 사용자는 하나만 선택할 수 있지만 다음과 같이 표시됩니다.콤보 박스의 옵션에 라벨을 어떻게 추가하고 나열합니까?

Short Duration 
Last Hour 
Last 2 hours 
Last 24 Hours 

Long Duration 
Last Month 
Last year 

짧은 지속 시간과 긴 지속 시간은 헤더와 같습니다. 너는 그들을 클릭 할 수 없다.

감사합니다.

참고 : 여기

Label ab = new Label ("Short duration");에 대해 이야기하고 있지 않다 내 코드 (내가 콤보 상자에서 옵션으로 라벨을 삽입했지만, 당신이 그것을 선택할 수 있습니다)

ComboBox combobox_print_options = new ComboBox(); 
combobox_print_options.setPromptText("Choose the button you wish to click"); 

Label table = new Label("Table"); 
combobox_print_options.getItems().addAll(
table, 
"a", 
"b"); 

답변

4

하기위한 항목 클래스를 만들기 당신의 선택 가능한 항목인지 아닌지를 선언하는 콤보 상자. (당신은 또한이 나타내는 시간에 편리한 접근,이에 다른 유용한 API를 추가 할 수 있습니다.)

는 그런 선택하지 않은 항목을 나타내는 세포 비활성화 셀 공장 사용

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.ListCell; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

public class ComboBoxWithSections extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     ComboBox<ComboBoxItem> combo = new ComboBox<>(); 
     combo.getItems().addAll(
      new ComboBoxItem("Short Duration", false), 
      new ComboBoxItem("Last Hour",  true), 
      new ComboBoxItem("Last 2 hours", true), 
      new ComboBoxItem("Last 24 hours", true), 
      new ComboBoxItem("",    false), 
      new ComboBoxItem("Long Duration", false), 
      new ComboBoxItem("Last Month",  true), 
      new ComboBoxItem("Last Year",  true)    
     ); 

     combo.setCellFactory(listView -> new ListCell<ComboBoxItem>() { 
      @Override 
      public void updateItem(ComboBoxItem item, boolean empty) { 
       super.updateItem(item, empty); 
       if (empty) { 
        setText(null); 
        setDisable(false); 
       } else { 
        setText(item.toString()); 
        setDisable(! item.isSelectable()); 
       } 
      } 
     }); 

     BorderPane root = new BorderPane(null, combo, null, null, null); 
     primaryStage.setScene(new Scene(root, 250, 400)); 
     primaryStage.show(); 
    } 

    public static class ComboBoxItem { 
     private final String name ; 
     private final boolean selectable ; 

     public ComboBoxItem(String name, boolean selectable) { 
      this.name = name ; 
      this.selectable = selectable ; 
     } 

     public String getName() { 
      return name ; 
     } 

     public boolean isSelectable() { 
      return selectable ; 
     } 

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

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

업데이트elsewhere 답했지만 나는 CSS PseudoClass와 외부 CSS 파일을 사용하여 헤더의 스타일을 변경합니다 :

추가

,745, start(...) 방법

, 다음과 같이 셀 공장의 updateItem(...) 방법을 변경 :

scene.getStylesheets().add("combo-box-with-sections.css"); 

및 CSS 파일은

처럼 보일 수 :

 public void updateItem(ComboBoxItem item, boolean empty) { 
      super.updateItem(item, empty); 
      if (empty) { 
       setText(null); 
       setDisable(false); 
       pseudoClassStateChanged(header, false); 
      } else { 
       setText(item.toString()); 
       setDisable(! item.isSelectable()); 
       pseudoClassStateChanged(header, ! item.isSelectable()); 
      } 
     } 

이제 Scene에 CSS 파일을 첨부

combo-box-with-sections.css :

.combo-box-popup .list-cell { 
    -fx-padding: 4 0 4 20 ; 
} 

.combo-box-popup .list-cell:section-header { 
    -fx-font: italic 10pt sans-serif ; 
    -fx-padding: 4 0 4 5 ; 
} 
+1

그래, 내가 원했던 바로 그거야! 그것은 꽤 오래지만 : – Indigo

+1

예,하지만 'String'을 사용하는 대신 아이템에 대한 실제 클래스의 추가 이점을 얻습니다. 이제는 올바른 시간 범위에 있는지 여부를 판단하기 위해 '문자열'을 켜야하는 대신 필요한 API를 추가 할 수 있습니다. 즉, 오버 헤드의 대부분은 어쨌든 실제 앱에서 수행하고자하는 작업입니다. –

+1

나는 당신의 요점을 본다! 나는 대부분의 코드를 이해했지만, 코드를 읽는 사람이 누구인지 이해할 수 있도록 대답에 약간의 설명을 추가하면 좋을 것이다. 나도 솔직히 .setCellFactory를 잘 이해하지 못했지만 원하는 방식으로 작동합니다. – Indigo

관련 문제