javafx - Can I set custom alignment in a column?

Viewed 47

I need to make custom alignment in my TreeTableView column.

I test css style:

-fx-alignment: CENTER-RIGHT;

and my column has alignment:

start picture

But I need to be able to choose the alignment myself, for example by setting it so that in the end the text is not in the middle or right or left, but has an indent on the right, similar to this:

1) picture1 - before changes

2) picture2 - after changes

I know there are many different types of alignment, I have tested these:

-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 ]

But I never found something similar to dynamic alignment change, tell me please, is it possible to adjust the alignment in such a way that you can, for example, indent 1, 2 or 4 characters on the right or do something similar?

my test:

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>

1 Answers

If you have a similar problem, where you need to be able to control the alignment in a column from the desired table, then this solution can help you:

/**
* 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;");
   }
 })
);

If you need to change the style of a column when a vertical scroll bar appears in this table, then this may be one of the solutions to the problem.

Related