JavaFX中如何为TreeTableView列设置自定义对齐方式?
为TreeTableView列实现自定义右侧缩进对齐
我需要给TreeTableView的列设置自定义对齐方式,目前测试了CSS样式:
-fx-alignment: CENTER-RIGHT;
能实现右对齐效果,但希望可以自主设置右侧缩进,比如让文本右侧留出1、2或4个字符的空白。试过-fx-text-alignment的各种预设值,但没找到动态调整的办法,请问怎么实现这种自定义缩进的对齐?
可行解决方案
方法1:通过CSS设置右侧内边距(Padding)
直接给列的单元格添加右侧内边距,实现类似缩进的效果。推荐使用字符单位ch(1ch等于当前字体下数字"0"的宽度),能保证缩进宽度与字符一致。
示例CSS:
.table-column.custom-indent .table-cell { -fx-alignment: CENTER-RIGHT; -fx-padding: 0 2ch 0 0; /* 右侧缩进2个字符宽度,可按需修改数值 */ }
在Java代码中给目标列添加样式类:
column2.getStyleClass().add("custom-indent");
方法2:自定义TableCell动态控制缩进
如果需要根据业务逻辑动态调整缩进值,可自定义TableCell,在更新单元格内容时设置padding。
示例代码:
column2.setCellFactory(col -> new TableCell<RecordFileName, String>() { @Override protected void updateItem(String item, boolean empty) { super.updateItem(item, empty); if (empty || item == null) { setText(null); setPadding(Insets.EMPTY); } else { setText(item); // 此处设置右侧缩进为2倍字体大小,可替换为变量实现动态调整 setPadding(new Insets(0, 2 * getFont().getSize(), 0, 0)); setAlignment(Pos.CENTER_RIGHT); } } });
方法3:文本后添加空格(不推荐)
在数据字符串末尾添加空格模拟缩进,但这种方式依赖等宽字体,非等宽字体下缩进宽度不一致,且会污染原始数据,仅作为临时方案。
修改后的测试代码
测试初始化方法(CSS实现方式)
public void test_initTestTableView() { TableView<RecordFileName> testTableView = controller.getTestTableView(); testTableView.setPlaceholder(new Label(" ")); TableColumn<RecordFileName, String> column1 = (TableColumn) testTableView.getColumns().get(0); column1.setCellValueFactory(new PropertyValueFactory<>("fileName")); TableColumn<RecordFileName, String> column2 = (TableColumn) testTableView.getColumns().get(1); column2.setCellValueFactory(new PropertyValueFactory<>("fileSize")); // 绑定自定义缩进样式 column2.getStyleClass().add("custom-indent"); ObservableList<RecordFileName> list_rows = testTableView.getItems(); list_rows.add(new RecordFileName("name1", "1")); list_rows.add(new RecordFileName("name2", "2")); list_rows.add(new RecordFileName("name3", "3")); list_rows.add(new RecordFileName("name4", "4")); }
RecordFileName.java(无需修改)
import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleObjectProperty; public class RecordFileName { private final ObjectProperty<String> fileName; private final ObjectProperty<String> fileSize; public RecordFileName(String name, String size) { this.fileName = new SimpleObjectProperty<>(this, "fileName"); this.fileName.set(name); this.fileSize = new SimpleObjectProperty<>(this, "fileSize"); this.fileSize.set(size); } public final ObjectProperty<String> fileNameProperty() { return this.fileName; } public final ObjectProperty<String> fileSizeProperty() { return this.fileSize; } }
main.fxml(无需修改)
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (c) 2015, 2019, Gluon and/or its affiliates. All rights reserved. Use is subject to license terms. This file is available and licensed under the following license: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Oracle Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SeparatorMenuItem?> <?import javafx.scene.control.TableColumn?> <?import javafx.scene.control.TableView?> <?import javafx.scene.layout.Pane?> <?import javafx.scene.layout.VBox?> <VBox fx:id="vbox" prefHeight="277.0" prefWidth="1025.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mycompany.stackexp.GUI.Controller_MainGUI"> <children> <MenuBar VBox.vgrow="NEVER"> <menus> <Menu mnemonicParsing="false" text="File"> <items> <MenuItem fx:id="menuItemStart" mnemonicParsing="false" onAction="#onMenuItemClick_StartStop" text="Stop" /> <MenuItem fx:id="menuItemClear" mnemonicParsing="false" onAction="#onMenuItemClick_Clear" text="Clear" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem fx:id="menuItemExit" mnemonicParsing="false" onAction="#onMenuItemClick_Exit" text="Exit" /> </items> </Menu> <Menu mnemonicParsing="false" text="Settings"> <items> <MenuItem mnemonicParsing="false" onAction="#onMenuItemClick_Configuration" text="Configuration" /> </items> </Menu> </menus> </MenuBar> <Pane prefHeight="239.0" prefWidth="1025.0"> <children> <TableView fx:id="testTableView" layoutX="44.0" layoutY="8.0" prefHeight="212.0" prefWidth="281.0"> <columns> <TableColumn fx:id="testColumn1" prefWidth="135.0" text="test_FileName" /> <TableColumn fx:id="testColumn2" prefWidth="127.0" text="test_FileSize" /> </columns> </TableView> </children> </Pane> </children> </VBox>
内容的提问来源于stack exchange,提问作者Kipoti
相关产品推荐
相关产品推荐

