如何更改 JavaFX TableView 中占位符文本的颜色?
How to change the color of the placeholder text in JavaFX TableView?
我相信这很简单,虽然我还没有弄明白:
如何更改 JavaFX TableView 中占位符文本的颜色?
这是占位符文本,在 table 为空时显示:
有(至少)两种方法可以解决这个问题。
通过Css
查阅 JavaFx Css Reference,您会看到 TableView 有一个内部 占位符 ,您可以使用 Css.
如果文档不足或您需要有关场景图形结构的更多信息,请使用 ScenicView 进行探索。
通过占位符节点
咨询 JavaFx 8 Api documentation 会发现,有一个 placeholder 属性,允许您将自定义节点设置为占位符。
演示
此演示展示了两种方法:
TableViewPlaceholderFill.java:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class TableViewPlaceholderFill extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
TableView<String> tableViaCss = new TableView<>();
tableViaCss.getStyleClass().add("my-little-pony");
TableView<String> tableWithCustomPlaceholder = new TableView<>();
final Label placeholderLabel = new Label
("Hello, Kitty!");
placeholderLabel.setFont(Font.font("monospace", FontWeight.BLACK, 16));
placeholderLabel.setTextFill(Color.HOTPINK);
tableWithCustomPlaceholder.setPlaceholder(new StackPane(placeholderLabel));
Scene scene = new Scene(new HBox(4,tableViaCss,
tableWithCustomPlaceholder));
scene.getStylesheets().add(TableViewPlaceholderFill.class
.getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
application.css:
.my-little-pony {
-fx-background-color: palevioletred;
}
.my-little-pony .placeholder .label {
-fx-text-fill: linen;
-fx-font-family: 'serif';
-fx-font-size: 1.666em;
-fx-font-weight: bold;
}
我相信这很简单,虽然我还没有弄明白:
如何更改 JavaFX TableView 中占位符文本的颜色?
这是占位符文本,在 table 为空时显示:
有(至少)两种方法可以解决这个问题。
通过Css
查阅 JavaFx Css Reference,您会看到 TableView 有一个内部 占位符 ,您可以使用 Css.
如果文档不足或您需要有关场景图形结构的更多信息,请使用 ScenicView 进行探索。
通过占位符节点
咨询 JavaFx 8 Api documentation 会发现,有一个 placeholder 属性,允许您将自定义节点设置为占位符。
演示
此演示展示了两种方法:
TableViewPlaceholderFill.java:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class TableViewPlaceholderFill extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
TableView<String> tableViaCss = new TableView<>();
tableViaCss.getStyleClass().add("my-little-pony");
TableView<String> tableWithCustomPlaceholder = new TableView<>();
final Label placeholderLabel = new Label
("Hello, Kitty!");
placeholderLabel.setFont(Font.font("monospace", FontWeight.BLACK, 16));
placeholderLabel.setTextFill(Color.HOTPINK);
tableWithCustomPlaceholder.setPlaceholder(new StackPane(placeholderLabel));
Scene scene = new Scene(new HBox(4,tableViaCss,
tableWithCustomPlaceholder));
scene.getStylesheets().add(TableViewPlaceholderFill.class
.getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
application.css:
.my-little-pony {
-fx-background-color: palevioletred;
}
.my-little-pony .placeholder .label {
-fx-text-fill: linen;
-fx-font-family: 'serif';
-fx-font-size: 1.666em;
-fx-font-weight: bold;
}