2012-12-13 2 views
2

JavaFX 2에서 어떻게 자동 줄 바꾸기 ListView (텍스트가 너무 길 때 여러 줄)를 만들 수 있습니까? 문자열에 \ n을 입력하면 다중 행이되지만 내용이 너무 동적이라는 것을 알고 있습니다. 아니면 모든 'xyz'픽셀 길이 후에 문자열에 \ n 넣을 수있는 좋은 방법이 있습니다.자동 줄 바꿈 JavaFX 2 ListView

감사합니다.

답변

4

ListCell.graphicProperty()에 TextArea를 넣을 수 있습니다. 이것은 일반적으로 목록 셀에 아이콘을 설정하는 데 사용되지만 노드 서브 클래스로 설정하기 쉽습니다.

+1

또한 호출 할 수 있습니다 [setEditable (거짓)] (http://docs.oracle.com/javafx/2/api/javafx/scene/control/TextInputControl.html#setEditable%28boolean%29)에 필요한 경우에만 읽기를 가능하게하는'TextArea'. – jewelsea

+0

고마워요! –

3

여기 내가 마침내 어떻게했는지는 정확한 코드입니다.

ListView<String> messages = new ListView<>(); 
    messages.relocate(10, 210); 
    messages.setPrefSize(this.getPrefWidth() - 20, this.getPrefHeight() - 250); 
    messages.setCellFactory(new Callback<ListView<String>, ListCell<String>>() { 
     @Override 
     public ListCell<String> call(ListView<String> list) { 
      final ListCell cell = new ListCell() { 
       private Text text; 

       @Override 
       public void updateItem(Object item, boolean empty) { 
        super.updateItem(item, empty); 
        if (!isEmpty()) { 
         text = new Text(item.toString()); 
         text.setWrappingWidth(messages.getPrefWidth()); 
         setGraphic(text); 
        } 
       } 
      }; 

      return cell; 
     } 
    }); 
+0

이 코드는 JavaFX8에서 의도하지 않은 동작을 일으켰습니다. 이 버전에서'if (! isEmpty)'를'if (empty || item == null)'로 약간 수정하면됩니다. – boatSoap