2013-05-24 1 views
5

TableView에 중첩 된 열의 내용을 "현명하게"약식/자르는 {String} 시도하고 있습니다. UNCONSTRAINED_RESIZE_POLICY을 사용해 보았지만 실제로는 창 크기에 따라 열의 크기를 자동으로 조정하므로 CONSTRAINED_RESIZE_POLICY을 선호합니다.JavaFX TableView Column Width Contents auto-truncating

내가 달성하기 위해 노력하고있어 :

String one = "SomeVeryLongString"; 

// Displayed in the column where user has shrunk it too narrow 

+-------------+ 
| Some...ring | 
+-------------+ 

// and if the user shrunk it a bit more 

+-----------+ 
| Som...ing | 
+-----------+ 

// and if they stretch it a bit 

+---------------+ 
| SomeV...tring | 
+---------------+ 

// etc... 

내가 String 길이를 읽고 내 자신의 절단 단계를 수행의 가능성을 탐구했지만, 다음은 사용자가 축소로이 동적으로 업데이트해야 할 수 없습니다/gui를 뻗는다. 내 직감은 TableView과 밀접하게 연결되어 있기 때문에 JavaFX Classes으로 처리해야한다고 말합니다. 그러나 방법을 찾지 못했습니다.

JavaFX을 사용하여 어떻게하면됩니까?

답변

7

솔루션

표준 TableCell의 기본 오버런 행동은 문자열이 잘 렸습니다 나타 내기 위해 문자열의 끝에 문자열 표시 ...의 절단하는 것입니다. JavaFX의 모든 Labeled 컨트롤은 이러한 방식으로 작동합니다.

질문에있는 예제는 문자열 중간에 ... 줄임표가 있습니다. 적절한 세포 공장에 의해 생성 된 셀이, set the overrun style을 달성하기 :

ellipsises에 대해 표시되는 텍스트 문자열은 해당 셀 공장에 의해 생성 된 세포에 setting a new ellipsis string에 의해 약간 변형 될 수
setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS); 

:

setEllipsisString(ellipsisString); 
이 방법을 보여줍니다

간단한 테이블 셀은 다음과 같습니다

class CenteredOverrunTableCell extends TableCell<Person, String> { 
    public CenteredOverrunTableCell() { 
     this(null); 
    } 

    public CenteredOverrunTableCell(String ellipsisString) { 
     super(); 
     setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS); 
     if (ellipsisString != null) { 
      setEllipsisString(ellipsisString); 
     } 
    } 

    @Override protected void updateItem(String item, boolean empty) { 
     super.updateItem(item, empty); 
     setText(item == null ? "" : item); 
    } 
} 

샘플 응용 프로그램

CenteredOverrunTableCell은 다음 샘플 응용 프로그램에서 사용됩니다.

  1. 첫 번째 이름 열은 중앙에 텍스트를 elides 및 성 열은 중앙에 텍스트를 elides하지만 사용자 정의 줄임표 문자열을 제공하지 <--->
  2. 의 사용자 지정 생략 문자열을 사용합니다.
  3. 전자 메일 열은 오버런 문자열의 끝에 ... 줄임표를 배치하는 기본 오버런 정책과 줄임표 문자열을 사용합니다.

elidedtableview

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.*; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.VBox; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class ElidedTableViewSample extends Application { 
    private TableView<Person> table = new TableView<>(); 
    private final ObservableList<Person> data = 
     FXCollections.observableArrayList(
      new Person("Jacob", "Smith", "[email protected]"), 
      new Person("Isabella", "Johnson", "[email protected]"), 
      new Person("Ethangorovichswavlowskikaliantayaprodoralisk", "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch", "[email protected]ch.com"), 
      new Person("Emma", "Jones", "[email protected]"), 
      new Person("Michael", "Brown", "[email protected]") 
     ); 

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

    @Override public void start(Stage stage) { 
     stage.setTitle("Table View Sample"); 
     stage.setWidth(470); 
     stage.setHeight(500); 

     final Label label = new Label("Address Book"); 
     label.setFont(new Font("Arial", 20)); 

     TableColumn firstNameCol = new TableColumn("First Name"); 
     firstNameCol.setCellValueFactory(
       new PropertyValueFactory<Person, String>("firstName")); 
     firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() { 
      @Override public TableCell<Person, String> call(TableColumn<Person, String> p) { 
       return new CenteredOverrunTableCell("<--->"); 
      } 
     }); 

     TableColumn lastNameCol = new TableColumn("Last Name"); 
     lastNameCol.setCellValueFactory(
       new PropertyValueFactory<Person, String>("lastName")); 
     lastNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() { 
      @Override public TableCell<Person, String> call(TableColumn<Person, String> p) { 
       return new CenteredOverrunTableCell(); 
      } 
     }); 

     TableColumn emailCol = new TableColumn("Email"); 
     emailCol.setCellValueFactory(
       new PropertyValueFactory<Person, String>("email")); 

     table.setItems(data); 
     table.getColumns().addAll(
      firstNameCol, 
      lastNameCol, 
      emailCol 
     ); 

     final VBox vbox = new VBox(); 
     vbox.setSpacing(5); 
     vbox.setPadding(new Insets(10)); 
     vbox.getChildren().addAll(label, table); 

     table.setColumnResizePolicy(
      TableView.CONSTRAINED_RESIZE_POLICY 
     ); 

     Scene scene = new Scene(vbox); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    class CenteredOverrunTableCell extends TableCell<Person, String> { 
     public CenteredOverrunTableCell() { 
      this(null); 
     } 

     public CenteredOverrunTableCell(String ellipsisString) { 
      super(); 
      setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS); 
      if (ellipsisString != null) { 
       setEllipsisString(ellipsisString); 
      } 
     } 

     @Override protected void updateItem(String item, boolean empty) { 
      super.updateItem(item, empty); 
      setText(item == null ? "" : item); 
     } 
    } 

    public static class Person { 
     private final SimpleStringProperty firstName; 
     private final SimpleStringProperty lastName; 
     private final SimpleStringProperty email; 

     private Person(String fName, String lName, String email) { 
      this.firstName = new SimpleStringProperty(fName); 
      this.lastName = new SimpleStringProperty(lName); 
      this.email = new SimpleStringProperty(email); 
     } 

     public String getFirstName() { 
      return firstName.get(); 
     } 

     public void setFirstName(String fName) { 
      firstName.set(fName); 
     } 

     public String getLastName() { 
      return lastName.get(); 
     } 

     public void setLastName(String fName) { 
      lastName.set(fName); 
     } 

     public String getEmail() { 
      return email.get(); 
     } 

     public void setEmail(String fName) { 
      email.set(fName); 
     } 
    } 
} 
+0

'setTextOverrun는()'내가 찾던 정확히 무엇이다! 아주 잘 답변 해 주셔서 대단히 감사합니다! 내가 할 수 있다면 +10! – SnakeDoc