TableView
다음 TableColumn
과 같이 포함하는 경우 :
private TableColumn<Foo, ObservableList<Foo2>> col;
목록을 멋지게 표시 할 수 있습니다. 그러나 해당 목록의 크기가 증가하면 셀 (및 행)의 크기가 일정하게 유지되므로 일부 셀이 잘립니다. 열의 크기를 수동으로 조정하면 테이블이 행의 높이를 올바르게 변경하여 정상적으로 표시됩니다.
행 / 셀의 높이를 ObservableList
어떻게 든 크기에 바인딩해야 합니까? 아니면 사용자 개입없이 테이블이 자동으로 조정되도록하는 더 좋은 방법이 있습니까?
포함 된 예 :
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class CellResizing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
ObservableList<MyFoo> list = FXCollections.observableArrayList();
list.add(new MyFoo());
list.get(0).foosProperty().get().add(new Foo2("First Item"));
TableView<MyFoo> table = new TableView<MyFoo>();
TableColumn<MyFoo, ObservableList<Foo2>> col = new TableColumn<MyFoo, ObservableList<Foo2>>("List");
col.setCellValueFactory(new PropertyValueFactory<MyFoo, ObservableList<Foo2>>("foos"));
Callback<TableColumn<MyFoo, ObservableList<Foo2>>, TableCell<MyFoo, ObservableList<Foo2>>> toolTipFactory =
new Callback<TableColumn<MyFoo, ObservableList<Foo2>>, TableCell<MyFoo, ObservableList<Foo2>>>() {
@Override
public TableCell<MyFoo, ObservableList<Foo2>> call(final TableColumn<MyFoo, ObservableList<Foo2>> param) {
final TableCell<MyFoo, ObservableList<Foo2>> cell = new TableCell<MyFoo, ObservableList<Foo2>>() {
@Override
public void updateItem(ObservableList<Foo2> item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setTooltip(null);
} else {
GridPane grid = new GridPane();
int modulus;
if (item.size() < 4) {
modulus = 1;
}
else if (item.size() < 9) {
modulus = 2;
}
else {
modulus = 3;
}
for (int i = 0; i < item.size(); ++i) {
Label label = new Label();
label.setText(item.get(i).toString());
grid.add(label, i % modulus, (i % item.size()) / modulus);
}
setGraphic(grid);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
};
return cell;
}
};
col.setCellFactory(toolTipFactory);
table.getColumns().add(col);
Button button = new Button("Add to list");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
list.get(0).foosProperty().get().add(new Foo2("Text"));
}
});
table.setItems(list);
root.add(button, 0, 0);
root.add(table, 0, 1);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
클래스 MyFoo + Foo2 :
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
public class MyFoo {
private ListProperty<Foo2> foos;
public MyFoo() {
foos = new SimpleListProperty<Foo2>(FXCollections.observableArrayList());
}
public ListProperty<Foo2> foosProperty() {
return foos;
}
}
class Foo2 {
private StringProperty text;
public Foo2(String string) {
text = new SimpleStringProperty(string);
}
public StringProperty textProperty() {
return text;
}
@Override
public String toString() {
return text.get();
}
}
목록 크기가 변경 될 때마다 updateItem이 호출되는 observablelist에 무효화 리스너를 배치하여이 문제를 해결했습니다.
@Override
public void updateItem(ObservableList<Foo2> item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setTooltip(null);
} else {
GridPane grid = new GridPane();
int modulus;
if (item.size() < 4) {
modulus = 1;
}
else if (item.size() < 9) {
modulus = 2;
}
else {
modulus = 3;
}
for (int i = 0; i < item.size(); ++i) {
Label label = new Label();
label.setText(item.get(i).toString());
grid.add(label, i % modulus, (i % item.size()) / modulus);
}
item.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
updateItem(item, empty);
}
});
setGraphic(grid);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다