JavaFx如何在tableview中只对齐一个列标题? [英] JavaFx how to align only one column-header in tableview?

查看:316
本文介绍了JavaFx如何在tableview中只对齐一个列标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在tableview中只对齐一列的标题?

以下css对齐所有列标题,但我想只对齐一列:

How to align title of only one column in tableview?
The following css aligns all column-header but I want to align just one column:

.table-view .column-header .label{
    -fx-alignment:CENTER
}


推荐答案

您现在可以使用最新版本的JavaFX执行此操作。

You can do that now with recent versions of JavaFX.

Java 1.7

这适用于Java:1.7.0_45 / JavaFX:2.2.45-b18)。它应该是最新版本,因为它需要 RT-14909

This worked for me with Java: 1.7.0_45 / JavaFX: 2.2.45-b18). It should be a recent version because it requires RT-14909

列标题将自动获得与TableColumn相同的Id!因此,您的TableColumn需要一个(CSS-)Id,您可以直接在Scenebuilder中为TableColumn设置或使用某些代码,如 tableColumn.setId(my-special-column)

The column-header will automatically get the same "Id" as the TableColumn! Therefore your TableColumn(s) need an (CSS-) Id that you can either set in Scenebuilder for the TableColumn directly or use some code like tableColumn.setId("my-special-column").

然后你可以使用Id直接设置列标题的样式:

Then you can style the column-header directly using the Id:

.table-view .column-header#my-special-column .label {
  -fx-alignment: CENTER_RIGHT;
  -fx-text-fill: red;
}

PS:这让我有点令人沮丧的1.5小时才算清楚。希望现在能帮助其他人!

PS: This took me some frustrating 1,5 hours to figure out. Hope this helps others now!

Java 8

由于我不喜欢的原因不明白id技巧不再适用于Java 8。但是,我们可以做的是直接为列设置一个styleClass(内部传播到TableColumnHeader)并在css文件中更改此styleClass:

For reasons that I don't understand the id trick does not work for Java 8 anymore. However, what we can do is to set a styleClass for the column directly (which internally is propagated to the TableColumnHeader) and change this styleClass in the css file:

在Java中:

firstNameCol.getStyleClass().add("my-special-column-style");

在CSS中:

.my-special-column-style .label {
  -fx-alignment: CENTER_RIGHT;
  -fx-text-fill: red;
}

在Mac OS X上使用jdk1.8.0_05为我工作。

Works for me here with jdk1.8.0_05 on Mac OS X.

这篇关于JavaFx如何在tableview中只对齐一个列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆