我正在嘗試調整TableView
中某個特定cell
的大小,特定爲。增加特定行的單元格大小-JAVAFX TableView
我使用的代碼,
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TableColors extends Application {
TableView<String> tView = new TableView<>();
@Override
public void start(Stage primaryStage) {
TableColumn<String, String> tcName = new TableColumn("name");
TableColumn<String, String> tcName1 = new TableColumn("name1");
ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "Three", "Four");
tView.setItems(items);
tView.getColumns().add(tcName);
tView.getColumns().add(tcName1);
tView.setColumnResizePolicy((param) -> true);
Platform.runLater(() -> customResize());
tcName.setCellValueFactory((p) -> new SimpleStringProperty(p.getValue()));
tcName.setCellFactory(new Callback<TableColumn<String, String>, TableCell<String, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TableCell<String, String>() {
@Override
public void updateItem(final String item, final boolean empty) {
super.updateItem(item, empty);//*don't forget!
if (item != null) {
setText(item);
setAlignment(Pos.CENTER);
} else {
setText(null);
}
}
};
}
});
Scene scene = new Scene(tView, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
private void customResize() {
try {
TableColumn<?, ?> col = tView.getColumns().get(0);
col.setPrefWidth(150);
} catch (Exception r) {
r.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
當我運行這個完整
name
柱大小
增加(如預期)。
相反,我只想增加包含「三個」的單元格的大小。
我不確定,我應該整合哪些代碼才能使其以這種方式工作。
我該如何解決這個問題?
你想在不同寬度的單元格在同一列嗎?對不起,但這不是'TableView'支持的。這需要對'TableView'' Skin'進行大量修改才能達到這種效果......當然,您可以在'TableView'或類似的東西中顯示一個與其他單元格大小不同的'graphic',但是'TableView'將會仍然確保一排中的所有單元具有相同的高度,並且一列中的所有單元具有相等的寬度... – fabian
但您希望如何工作?該特定列將具有最長單元格的寬度?所以你甚至不會注意到與其他人一樣 – Elltz
@Elltz我試圖通過增加單元格寬度到表格寬度和縮小其他列單元格的寬度來合併單元格。我期待它能夠像excel合併兩個或更多單元格一樣工作。 – user3164187