[英]How to change TreeView Color in JavaFX programmatically without using css?
我正在嘗試實現暗模式功能。 為此,我需要將 treeView 的顏色更改為深灰色。 以下代碼不起作用。
treeView.setStyle("-fx-background-color:#242424;");
我沒有單獨使用 CSS。 所以像上面的代碼這樣的解決方案將受到高度贊賞。
您需要更改單元格的背景 colors。 在 Java 代碼中執行此操作很棘手,因為它會強制您使用樹上的自定義cellFactory
來訪問單元格。 相反,我建議使用外部樣式表。 你可以做
.tree-cell {
-fx-background-color:#242424;
}
默認情況下,樹單元格(和許多其他控件)的背景設置為名為-fx-background
的“查找顏色”(本質上是 CSS 變量)。 樹單元格中的文本(以及許多其他控件中的文本)設置為取決於該值的顏色,因此如果背景是暗的,它將自動變為淺色(反之亦然)。 所以這可能會更好
.tree-cell {
-fx-background:#242424;
}
通常,對於您的應用程序“主題化”,默認樣式表modena.css定義了所有 colors 以查找 colors 的小集合。 那些 colors 及其默認值是
/* A light grey that is the base color for objects. Instead of using
* -fx-base directly, the sections in this file will typically use -fx-color.
*/
-fx-base: #ececec;
/* A very light grey used for the background of windows. See also
* -fx-text-background-color, which should be used as the -fx-text-fill
* value for text painted on top of backgrounds colored with -fx-background.
*/
-fx-background: derive(-fx-base,26.4%);
/* Used for the inside of text boxes, password boxes, lists, trees, and
* tables. See also -fx-text-inner-color, which should be used as the
* -fx-text-fill value for text painted on top of backgrounds colored
* with -fx-control-inner-background.
*/
-fx-control-inner-background: derive(-fx-base,80%);
/* Version of -fx-control-inner-background for alternative rows */
-fx-control-inner-background-alt: derive(-fx-control-inner-background,-2%);
/* A bright blue for highlighting/accenting objects. For example: selected
* text; selected items in menus, lists, trees, and tables; progress bars */
-fx-accent: #0096C9;
/* Default buttons color, this is similar to accent but more subtle */
-fx-default-button: #ABD8ED;
/* A bright blue for the focus indicator of objects. Typically used as the
* first color in -fx-background-color for the "focused" pseudo-class. Also
* typically used with insets of -1.4 to provide a glowing effect.
*/
-fx-focus-color: #039ED3;
-fx-faint-focus-color: #039ED322;
因此,為整個應用程序“主題化”的一個好策略是更改其中一些的定義並將其應用於場景的根。 例如,“暗模式”可能只用
.root {
-fx-base: #242424 ;
-fx-control-inner-background: derive(-fx-base,20%);
-fx-control-inner-background-alt: derive(-fx-control-inner-background,-10%);
-fx-accent: #006689;
-fx-focus-color: #036E83;
-fx-faint-focus-color: #036E8322;
}
這是一個完整的例子:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
TreeView<String> tree = createRandomTree();
BorderPane root = new BorderPane(tree);
HBox controls = new HBox(5,
new Button("Click"),
new Label("Choose one:"),
new ComboBox<>(FXCollections.observableArrayList("One", "Two", "Three")),
new TextField()
);
controls.setAlignment(Pos.CENTER);
controls.setPadding(new Insets(5));
root.setTop(controls);
ListView<String> list = new ListView<>();
IntStream.range(1, 51).mapToObj(i -> "Item "+i).forEach(list.getItems()::add);
root.setLeft(list);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("darkmode.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
private TreeView<String> createRandomTree() {
Random rng = new Random();
List<TreeItem<String>> items = new ArrayList<>();
TreeItem<String> root = new TreeItem<>("Item 1");
root.setExpanded(true);
items.add(root);
TreeView<String> tree = new TreeView<>(root);
for (int i = 2 ; i <=20 ; i++) {
TreeItem<String> item = new TreeItem<>("Item "+i);
item.setExpanded(true);
items.get(rng.nextInt(items.size())).getChildren().add(item);
items.add(item);
}
return tree ;
}
public static void main(String[] args) {
launch();
}
}
其中darkmode.css
是上面的 CSS 文件。 這給出了:
請注意,如果您真的想在沒有外部 CSS 文件的情況下執行此操作,則可以直接在根節點上設置那些內聯 styles :
Pane root = ... ;
root.setStyle(
" -fx-base: #242424 ;\n" +
" -fx-control-inner-background: derive(-fx-base,20%);\n" +
" -fx-control-inner-background-alt: derive(-fx-control-inner-background,-10%);\n" +
" -fx-accent: #006689;\n" +
" -fx-focus-color: #036E83;\n" +
" -fx-faint-focus-color: #036E8322;");
您甚至可以將它們設置為TreeView
,這會將它們僅應用於樹:
TreeView<String> tree = new TreeView<>();
tree.setStyle(
" -fx-base: #242424 ;\n" +
" -fx-control-inner-background: derive(-fx-base,20%);\n" +
" -fx-control-inner-background-alt: derive(-fx-control-inner-background,-10%);\n" +
" -fx-accent: #006689;\n" +
" -fx-focus-color: #036E83;\n" +
" -fx-faint-focus-color: #036E8322;");
但一般來說,使用和外部樣式表在許多方面都更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.