我可以在列中设置自定义对齐方式吗



我需要在TreeTableView列中进行自定义对齐。

我测试css样式:

-fx-alignment: CENTER-RIGHT;

我的专栏有对齐方式:

启动图片

但我需要能够自己选择对齐方式,例如,通过设置它,最终文本不在中间、右侧或左侧,而是在右侧有缩进,类似于以下内容:

1( 图1-更改之前

2( 图2-更改后

我知道有很多不同类型的对齐,我测试过这些:

-fx-text-alignment: [ top-left | top-center | top-right | center-left | center | center-right bottom-left | bottom-center | bottom-right | baseline-left | baseline-center | baseline-right ]

但我从未发现类似于动态对齐更改的内容,请告诉我,是否可以调整对齐方式,例如,在右侧缩进1、2或4个字符或执行类似的操作?

我的测试:

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.setStyle("-fx-alignment: CENTER-RIGHT;");
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>

如果您有类似的问题,需要能够控制所需表中列的对齐,那么此解决方案可以帮助您:

/**
* Get scrollBar from current tableView or treeTableView
*/
Set<Node> scrollBars = treeTableViewFileName.lookupAll(".scroll-bar");
Optional<Node> verticalScrollBar = scrollBars.stream()
.filter(node ->((ScrollBar) node).getOrientation().equals(Orientation.VERTICAL))
.findAny();
verticalScrollBar.ifPresent(node -> 
node.visibleProperty().addListener((observable, oldValue, newValue) -> {
if(newValue) { //If scrollBar is visible?
//column FileSize
((TreeTableColumn) ThreadTreeTableViewFileName.treeTableViewFileName.getColumns().get(1)).setStyle("-fx-alignment: CENTER-RIGHT; -fx-padding: 0 90 0 0;");
} else { //Else scrollBar is hide
//column FileSize
((TreeTableColumn) ThreadTreeTableViewFileName.treeTableViewFileName.getColumns().get(1)).setStyle("-fx-alignment: CENTER-RIGHT; -fx-padding: 0 5 0 0;");
}
})
);

如果在该表中出现垂直滚动条时需要更改列的样式,则这可能是解决该问题的方法之一。

最新更新